is std::cout thread safe?

std::cout object is defined as: [Stroustrup says in his book]
1
2
3
4
5
6
namespace std {
class ostream {
//...
ostream& operator<<(const char*);
};
extern ostream cout;


So, as I see in this code, there's actually 1 cout object for each process. If a multi-threaded application tries to use std::cout object, will it be safe?
Yes, however if you run this in two different threads:

cout << "Hello World!" << endl;

then the other thread might get to print something in-between "Hello World" and endl, so you can end up with lines like "Hello World!Hello World!".
So in most cases you still have to synchronize cout access.
Topic archived. No new replies allowed.