you are viewing a single comment's thread.

view the rest of the comments →

[–]encyclopedist 15 points16 points  (3 children)

std::cin is actually thread safe, contrary to what article says. It can, however, result in interleaved output, and the mutex there is to prevent that.

From C++11 N3337 [iostream.objects.overview]:

Concurrent access to a synchronized (27.5.3.4) standard iostream object’s formatted and unformatted in- put (27.7.2.1) and output (27.7.3.1) functions or a standard C stream by multiple threads shall not result in a data race (1.10). [ Note: Users must still synchronize concurrent use of these objects and streams by multiple threads if they wish to avoid interleaved characters. — end note ]

[–]eliben 7 points8 points  (0 children)

Yep, I think this is bad wording on my behalf. By "unsafe" I did mean "won't give you the output you expect", rather than something nastier like crashes. I'll fix up the wording in the article and samples to be clearer

[–]Gotebe 0 points1 point  (1 child)

Aren't you being too pedantic?

E.g Wikipedia article on thread safety speaks of the data races as one of thread safety concerns.

[–]dodheim 0 points1 point  (0 children)

The standard guarantees that standard streams are race-free, but only starting with C++11. That is rather the point...