11

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.

Khaled Alshaya
  • 94,250
  • 39
  • 176
  • 234
  • 2
    [Note: the answer is now "yes", in C++11](http://stackoverflow.com/a/6374525/87234). – GManNickG Feb 05 '13 at 22:46
  • @GManNickG: I use Visual Studio 2013 C++ and cout still isn't thread safe. outputs on the screen are mostly corrupted/intermixed. – Mehmet Fide Dec 16 '13 at 09:48
  • @MehmetFide: Thread-safety is different from synchronisation (mixed/interleaved strings); C++11 standard guarentees that the implementation doesn't lead to data races upon using these objects from different threads, while it's upto you to make sure that multiple `operator<<` calls are synchronized. – legends2k Jun 06 '14 at 11:51

3 Answers3

9

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.

Warren Young
  • 40,875
  • 8
  • 85
  • 101
  • That what I think too :) but the article claims that the code is portable too! – Khaled Alshaya Sep 27 '09 at 11:51
  • 2
    I would guess that the article's author is simply saying that it works everywhere he's tried it. He probably hasn't tried it on, say, Keil C with HomeGrownRTOS v1.2, or any number of other uncommon combinations. – Warren Young Sep 27 '09 at 11:57
  • The article isn't talking about C++ streams. – Daniel Earwicker Sep 27 '09 at 11:57
  • @Warren Young - the author of the article isn't that dumb, as it happens. They specifically say what standard must be supported for their code to work. – Daniel Earwicker Sep 27 '09 at 11:58
  • @Earwicker, this has nothing todo with being dumb or not, i think. The author claims that it's portable posix code. The pure C function `fprintf` is still not thread safe. – Johannes Schaub - litb Sep 27 '09 at 12:38
  • @litb - I think Warren Young attributed a certain naivety to the author of that article, that they were claiming portability based on a few experiments. That's what I took exception to. As you've noticed, the author clearly states that their program is valid on POSIX environments. – Daniel Earwicker Sep 27 '09 at 13:46
9

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.

Daniel Earwicker
  • 114,894
  • 38
  • 205
  • 284
2

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.

sbi
  • 219,715
  • 46
  • 258
  • 445