Frequently, when iostream objects are used, a sequence of I/O operations must be MT-safe. For example, the code:
cout << " Error message:" << errstring[err_number] << "\n"; |
involves the execution of three member functions of the cout stream object. Since cout is a shared object, the sequence must be executed atomically as a critical section to work correctly in a multithreaded environment. To perform a sequence of operations on an iostream class object atomically, you must use some form of locking.
The libC library now provides the stream_locker class for locking operations on an iostream object. See 11.4.5 Object Locks for information about the stream_locker class.