Write thread-safe code

While threads allow an application to execute different tasks concurrently, they can also complicate the program logic. You must code you multithreaded programs so that they are thread-safe. A thread-safe program satisfies the following conditions:

  1. Access to shared data (such as global variables) must be serialized so that data reads and writes are consistent and atomic. For more information, see “Serializing access to shared data and shared resources.”

  2. Access to shared resources (such as file descriptors) must be serialized so that the resource maintains a consistent state. For more information, see “Serializing access to shared data and shared resources.”

  3. Dependent actions in different threads must be synchronized so that they are performed in the required order. For more information, see “Synchronizing dependent actions.”

  4. Calls to thread-unsafe system routines must be serialized so that only one call is active at one time. For more information, see “Calling thread-unsafe system routines.”

  5. Thread serialization primitives must be used in a way that avoids deadlock. For more information, see “Avoiding deadlock.”

  6. Calls to CS-Library, Client-Library, and Bulk-Library routines must satisfy the restrictions explained in “Client-Library restrictions for multithreaded programs.”

Program code that does not meet these restrictions is thread-unsafe. In general, thread-unsafe code does not yield predictable behavior when executed in a multithreaded program. Restrictions 1-5 are the general rules for making any application thread safe. Restriction 6 is specific to Client-Library applications. The following sections explain each restriction in more detail.

Note This explanation is not intended to replace the documentation for your system’s thread interface. Please read and understand your system documentation before attempting to use Client-Library in a multithreaded environment.

NoteWhen using multithreaded libraries included with Client-Library, fork() without exec() is not supported. This is because PC Net Library threads required for I/O operations, are not duplicated in the child by the fork() system call.