1

I know that we will never see something like this

aabbABABaabb

as the output of the following code

Thread1

std::cout << "aa";

Thread2

std::cout << "bb";

By the way, will the writes be atomic if we add an endline to the previous calls like this?

Thread1

std::cout << "aa" << std::endl;

Thread2

std::cout << "bb" << std::endl;

Anyway, the single writes are atomic. So my question. Will it always hold that if thread1 requested a write before thread2, then we will see message "aa" before "bb"? If we know this always holds, it makes sense to log entries about the state of a multi-threaded application to be able to examine deadlocks and etc.

Other words, is it possible that Thread1 calls cout first, but the actual message it requested to appear in the stream appeared only after Thread2 got its version of writing to stream executed. To me, this scenario is quite possible, if, for example, right after cout was called, but actually before it was completely executed,there happened a context switch, and Thread2 was given enough time for both requesting cout and having low-level os-dependent library actually execute Thread2's request. Correct me please if I am wrong and this scenario is not possible

  • Also here: https://stackoverflow.com/questions/15033827/multiple-threads-writing-to-stdcout-or-stdcerr – unlut Nov 28 '19 at 12:42
  • No, `std::endl` won't help, it's still two separate calls. I'm not sure if there's any utility to guarantee that two threads will not get I/O intertwined (you'd have to lock it manually). Your best bet might be printing timestamps together with your actual message. – Yksisarvinen Nov 28 '19 at 12:42
  • how do you know that "thread1 requested a write **before** thread2" without synchronisation mechanism? with some synchronisation the question is moot, because then the order is well defined – 463035818_is_not_an_ai Nov 28 '19 at 12:43
  • @formerlyknownas_463035818 That's the point of this question I think? "*If* Thread1 reaches `std::cout <<` line first, is it guaranteed that it will also output first"? – Yksisarvinen Nov 28 '19 at 12:45
  • @Yksisarvinen Yes, but the notion of "thread1 reaches the line first" only makes sense when it can be determined which thread comes first and I wouldnt know how to do that without using a mutex or similar – 463035818_is_not_an_ai Nov 28 '19 at 12:48
  • 1
    My question was about being able to actually **tell who's first** with the help of logging. I know that the order of threads reaching `cout` is not defined in general, because the threads that are not in sync with each other are said to be racing and it is not certain who reaches `cout` first. But there is always someone who is THE first, so I am asking if I can tell who is first with the help of the output messages? I mean, is it possible that thread1 **called** `cout` first, but the actual message was delivered after thread2's message? – Artyom Gevorgyan Nov 28 '19 at 12:53
  • In a race scenario, the notion of "called `cout` first" doesn't make too much sense. It's always possible that the function call _started_ earlier in thread 1 but then got preempted immediately, thread 2 runs the whole function and then thread 1 completes. – Max Langhof Nov 28 '19 at 13:02
  • @MaxLanghof, this totally makes sense, thanks, this answers my question, as I have now updated it. Can you write an answer? – Artyom Gevorgyan Nov 28 '19 at 13:03
  • 1
    Btw, according to [this comment](https://stackoverflow.com/questions/6374264/is-cout-synchronized-thread-safe#comment7469304_6374525), you might actually get abab. A given C++ implementation might not allow that (and I don't know whether the comment is actually correct), but you should be careful. In any case, it's at least not a data race since C++11 so no UB. – Max Langhof Nov 28 '19 at 13:06

0 Answers0