BDE 4.14.0 Production release
|
Provide a C++20-compatible basic_osyncstream class template.
osyncstream
class.wosyncstream
class.Canonical header: bsl_syncstream.h
This component is for internal use only. Please include <bsl_syncstream.h>
instead.
This component defines a class template, bsl::basic_osyncstream
, that is a convenience wrapper for bsl::basic_syncbuf
. It provides a mechanism to synchronize threads writing to the same stream, or more precisely, to the same streambuf
. It's guaranteed that all output made to the same final destination buffer will be free of data races and will not be interleaved or garbled in any way, as long as every write to that final destination buffer is made through (possibly different) instances of bsl::basic_syncbuf
.
Types bsl::osyncstream
and bsl::wosyncstream
are aliases for bsl::basic_osyncstream<char>
and bsl::basic_osyncstream<wchar_t>
, respectively.
This section illustrates intended use of this component.
In a multi-threaded environment attempts to write from different threads to one ostream
"may result in a data race", according to the ISO C++ Standard. Concurrent access to the special synchronized iostream objects, like cout
, cerr
, etc, does not result in a data race, but output characters from one thread can interleave with the characters from other threads still. osyncstream
solves both problems: prevents data races and characters interleaving.
The following example demonstrates concurrent printing of a standard STL container of values that can be streamed out. The elements are separated by comma and the whole sequence is enclosed in curly brackets. Note that this example requires at least C++11.
Now this function can safely be used in a multi-threaded environment: