lundi 30 juillet 2018

Is a flush required for cout in a critical section?

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.

Aucun commentaire:

Enregistrer un commentaire