I have some code compiled under the c++11 standard. I have multiple threads writing to cout. I noticed when writing many lines there would be some cases where some lines were missing (like 1 out of 2000000). I was surprised to see this since my string (outStr
below) was local to each thread and I had a critical section around my writes to stdout. I noticed the problem went away when I flushed the stream.
#pragma omp critical(cout)
{
cout << outStr;
cout.flush();
}
Is this expected behaviour? What really tricked me was the fact that when I wrote a relatively small number of lines (<100000), I would always see the number of expected lines outputted.
Overall I'm not really happy with the critical section in general since I'm noticing in my profiling that it is causing a lot of contention. I'm open to any suggestions to improve my I/O.
*Edit I was under the impression that under c++11 there would be no corruption of my output so long as I synchronized my output (i.e. no interleaving or missing output when I use a critical section) but the missing lines seem to indicate that this is not a guarantee without also flushing the output.