2

Consider a function that takes array of 100 data elements does something prints those to console.

Now say I launch two threads of the function on a multicore machine. Since it's a multicore machine there is good probability that two threads run on two cores. Since both of them need to display data there's a race for console window.

Now how is the console shared between the threads.?

The one which reaches the std::cout first gets to display entire data, stalling other thread .? or Is the console window time shared between threads.? if so what determines the amount of time sharing by the threads .?

pvkc
  • 89
  • 6

4 Answers4

6

No, cout is not threadsafe in C++11. You have to arrange that yourself.

[To be accurate, cout itself is threadsafe, but only for the actual duration of the call itself, and a typical line of cout << x << y << endl; will be three different calls to member-function within the cout object. So you don't need mutex protection for the cout functionality itself, but you do need it to guarantee that a "line" of calls to cout are performed as one unit].

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
  • 2
    Hmmm, yes, it is. All output is serialised. (It may not be in the order you expect, but a program that inserts into `cout` from multiple threads does not have undefined behaviour) – R. Martinho Fernandes Sep 18 '13 at 11:09
  • Right, so `cout << 123 << 456 << endl` in parallel will with `cout << 321 << 654 << endl;` will produce one line of `123456` followed or preceeded by `321456`, or can they be arbitrarily mixed any way they happen to be executed? Since, as far as I understand one such line translates to `cout.operator<<(123), cout.operator<<(456), cout.operator<<(endl);` - where perhaps each call to `operator<<` may be synchronized, but certainly not the entire "line" of code. – Mats Petersson Sep 18 '13 at 11:18
  • Yeah, you can get something like 123321654\n456. I am merely objecting to the use of "not threadsafe" as I think it implies it corrupts the data structures or something. – R. Martinho Fernandes Sep 18 '13 at 11:23
  • @R.MartinhoFernandes: Yes, I added another paragrap to cover that. – Mats Petersson Sep 18 '13 at 11:25
2

It depends on how you access input/output stream:

§ 27.2.3 Thread safety [iostreams.threadsafety]

1 Concurrent access to a stream object (27.8, 27.9), stream buffer object (27.6), or C Library stream (27.9.2) by multiple threads may result in a data race (1.10) unless otherwise specified (27.4). [ Note: Data racesresult in undefined behavior (1.10). —end note ]

2 If one thread makes a library call a that writes a value to a stream and, as a result, another thread reads this value from the stream through a library call b such that this does not result in a data race, then a’s write synchronizes with b’s read.

also § 27.4.1

Concurrent access to a synchronized (27.5.3.4) standard iostream object’s formatted and unformatted input (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 ]

Community
  • 1
  • 1
billz
  • 44,644
  • 9
  • 83
  • 100
0

If you use a function like this:

void justam(){
   std::cout << "One " << "two ";
}

and you call it from 2 threads you will get a more or less ordered mix of "One " and "two ". It is like you would write it in 2 separate std::cout calls and while you can be lucky and 1 thread can be scheduled for both outputs, you can also be not so lucky and end up with something like: "one " "one " "two " "one ". Since the others' answers are covering C++/the standard and since C++ is more or less dependent on the OS and the thread scheduling is the task of the operating system, bellow is a very good short description of how it works:

http://en.wikipedia.org/wiki/Scheduling_%28computing%29

Furthermore, please check this article to understand the 3 models of threading if you are really curious what's going on:

http://en.wikipedia.org/wiki/Thread_%28computing%29#Models

These 2 articles will give you an idea of how multithreading stuff works, including output buffer which in the end is an I/O resource and along with the other answers should form a complete picture of what's going on and what to expect.

asalic
  • 949
  • 4
  • 10
0

It is usually not recommended to use cout in large programs, people prefer to use a dedicated logging where you can redirect the output where you like.

As often, Boost is a natural candidate for that, supporting thread safe and non-thread safe loggers, and being almost standard.

http://www.boost.org/doc/libs/1_55_0/libs/log/doc/html/log/tutorial/sources.html

Gabriel
  • 3,564
  • 1
  • 27
  • 49