Both this answer on Stack Overflow and cppreference.com suggest turning off stream synchronization to improve performance, arguing that stream synchronization would disable buffering.
Now this is what I do not understand. Why can't synchronized streams simply share buffers? I imagined that if the streams are synchronized, std::fputc(stdout, c);
could simply be implemented in terms of std::cout << c;
or the other way round (or using a common primitive). So whenever C I/O is mixed with C++ I/O, synchronized streams would even have an advantage over non-synchronized ones! Fewer buffers, fewer cache misses.
The current C++ standard draft seems to be with me here. In the footnote where sync_with_stdio()
is specified, it says "In practical terms, synchronization usually means that a standard iostream object and a standard stdio object share a buffer." Is it possible that the links I posted above merely document some imperfect implementations and their performance implications?
Also, because I do not see any theoretical disadvantage of non-synchronized streams, I am wondering why these exist in the first place.