You can extend or specialize the functionality of the iostream classes by deriving new classes. If objects instantiated from the derived classes will be used in a multithreaded environment, the classes must be MT-safe.
Considerations when deriving MT-safe classes include:
Making a class object MT-safe by protecting the internal state of the object from multiple-thread modification. To do this, serialize access to member variables in public and protected member functions with mutex locks.
Making a sequence of calls to member functions of an MT-safe base class atomic, using a stream_locker object.
Avoiding locking overhead by using the _unlocked member functions of streambuf within critical regions defined by stream_locker objects.
Locking the public virtual functions of class streambuf in case the functions are called directly by an application. These functions are: xsgetn, underflow, pbackfail, xsputn, overflow, seekoff, and seekpos.
Extending the formatting state of an ios object by using the member functions iword and pword in class ios. However, a problem can occur if more than one thread is sharing the same index to an iword or pword function. To make the threads MT-safe, use an appropriate locking scheme.
Locking member functions that return the value of a member variable greater in size than a char.