Reputation: 96849
I know that there is no concept of threads in current C++
, but this article is saying:
A typesafe, threadsafe, portable logging mechanism
.....
The
fprintf()
function is threadsafe, so even if this log is used from different threads, the output lines won't be scrambled.
What about cout
, cerr
and clog
?
I think this question is applicable to all kind of stream types in C++ also, like fstream
and stringstream
.
Upvotes: 11
Views: 8254
Reputation: 224049
Since the current C++ standard doesn't even acknowledge that there are things called "threads", it certainly doesn't give any guarantees regarding thread-safety at all.
This is all implementation-defined.
Upvotes: 2
Reputation: 116654
The article makes a claim about the POSIX standard for the fprintf
API. It says nothing about C++ streams. And this is quite correct, as there are no such guarantees on those stream.
Note that although the logging class in that article uses C++ stream syntax, it does this via a std::ostringstream
object that is created and destroyed for every logging event, and so is not shared between threads. It uses fprintf
to actually write the content to the console.
The Microsoft C library makes some claims to be POSIX compliant, and so the code in the article probably is quite widely portable (as many other popular operating systems are POSIX compliant). But this doesn't mean that standard C++ streams are thread-safe.
Upvotes: 9
Reputation: 42333
That would be an implementation-specific detail. You can ask if Compiler X with Run Time Library Y has thread-safe standard streams, but you can't ask if all implementations do, because implementations are allowed to differ with regard to thread safety. This is part of what it means that C++ has no built-in concept of threads. It's all implementation-specific.
Upvotes: 9