Open System Services Programmer's Guide
Keeping Application Design Simple
Because of the inherent complexity of multithreaded programming, application designers should
follow these recommendations:
• Write programs with a single application thread when possible.
• Keep multiple application threads independent of each other when possible. If this is not
possible, keep the interdependencies to a minimum.
Threading Considerations
Application designers should be aware of the following general threading considerations when
developing a Standard POSIX Threads application:
• “Sharing Process Resources” (page 328)
• “Using Standard Libraries” (page 328)
• “Addressing the Thread Stack” (page 329)
• “Making Process-Blocking Calls” (page 329)
• “Using Nowait Input or Output” (page 329)
• “Yielding Control” (page 330)
• “Using Signals” (page 330)
• “Spawning a New Process” (page 331)
• “Isolating Faults” (page 331)
Sharing Process Resources
Access by threads to shared process resources such as global data, the heap, and files must be
synchronized using mutexes and condition variables. Table 54 (page 313) lists global and private
process resources.
Using Standard Libraries
No Need for Jacket Routines or Special Header Files
Standard POSIX Threads execute until they relinquish control. A thread can never be preempted
while executing code in a standard library. Therefore, an application does not need to protect
standard libraries with global mutexes or use special header files. Standard libraries do not require
jacket routines or other modifications to make them thread-safe or reentrant.
The thread library does provide jacket routines for calls to Pathway servers (SERVERCLASS_SEND
and SERVERCLASS_SEND_INFO) and for TMF transactions, as described in “Jacket Routines”
(page 333).
No Asynchronous Cancels
Standard POSIX Threads does not support the asynchronous receipt of cancels; a thread can never
exit prematurely from a standard library due to cancellation. Therefore, an application does not
need to protect the run-time state of standard libraries by disabling cancelability before making a
call into the libraries.
Beware of Pointers to Static Library Data
Some libraries contain functions that return pointers to static data. In this case, one thread could
invalidate another thread’s pointer. For example, the getpwuid() function returns a pointer to
information about a user, contained in a static variable in the library. This static variable is overlaid
on the next call to this procedure by any thread.
328 Using the Standard POSIX Threads Library