Thread Safety and USL I/O Streaming

z/OS The USL I/O Stream Library provides thread safety at the object level. This means that it is safe to have multiple threads manipulate the same object. This library provides streaming operators for the built in C++ types. With object level thread safety, the output from one streaming operator will be streamed in entirety before the next.

AIX OS/400 It is not safe to access a stream object in one thread while modifying it in another thread.

In a multi-threaded environment, there is no guarantee that the output from one streaming operator on the same thread will appear immediately after the output from the preceding streaming operator. For example, given the following scenario, either result may occur:

Scenario:

thread 1 cout << anInt1 << aString1;
thread 2 cout << anInt2 << aString2;

Result:

Desired anInt1 aString1 anInt2 aString2
Possible anInt1 anInt2 aString1 aString2

If order of output from separate threads is important, then explicit programmer serialization is required.

Related Concepts