I've looked everywhere for an example and checked the C++ manual (I learn best by example).
What I need is a method that can write to standard out with blocking for a concurrent assignment.
I was suggested to use "protected cout" but I have no idea what is meant by that. Originally I've been using's C's write but I lose a few points for doing this.
Other solutions I thought of was using a semaphore to protect cout, so it can only print for one thread at a time. But I get the feeling that there's a built in one for C++ somewhere out there...
Help will be greatly appreciated. And please don't link me to anything from http://www.cplusplus.com/ without giving me an example. I'm rather new to C++ and if I was a pro at reading the api at cplusplus.com, I wouldn't be asking this question.
Edit: More info pertaining to my question. No C++11 is not allowed. I am not allowed any 3rd party libraries. So boost is a no go. The machine this has to perform on is a Unix machine.
Final Edit: itwasntpete was the closest to the correct answer, but I can't choose comments. Semaphores is the way I have to go. @Casey true, I'm using a 3rd party library the prof wrote that simplifies concurrency for us. But we're not allowed to use other libraries. It was easier to make that as a rule for people trying to help. Sorry!
I don't think there's any synchronization built in for streams. In C++03 cout is even not necessarily thread-safe. In c++11 it is but still not synchronized.
See this question: Is cout synchronized/thread-safe?