How does `std::osyncstream` manage the out stream?
Solution 1:
Does it lock some mutex?
Yes, indirectly. The std::basic_osyncstream
class, of which osyncstream
is a specialization of the form basic_osyncstream<char>
, is derived from std::basic_ostream
and will typically have just one 'extra' member, of the std::basic_syncbuf
class. From cppreference:
Typical implementation of std::basic_osyncstream holds only one member: the wrapped std::basic_syncbuf.
It is that basic_syncbuf
object that implements the output synchronization, preventing data races. Again, from cppreference (bolding mine):
Typical implementation of std::basic_syncbuf holds a pointer to the wrapped std::basic_streambuf, a boolean flag indicating whether the buffer will transmit its contents to the wrapped buffer on sync (flush), a boolean flag indicating a pending flush when the policy is to not emit on sync, an internal buffer that uses Allocator (such as std::string), and a pointer to a mutex used to synchronize emit between multiple threads accessing the same wrapped stream buffer (these mutexes may be in a hash map with pointers to basic_streambuf objects used as keys).