OSF DCE Application Development Guide--Core Components
OSF DCE Application Development Guide—Core Components
By using the global lock, you ensure that only one thread executes in outside libraries,
which may call each other, and in unknown code. Using a single global lock is safer
than using multiple local locks because it is difficult to be aware of everything a library
may be doing or of the interactions that library can have with other libraries.
8.3.2 Making Nonthreaded Code Thread-Reentrant
Thread-reentrant code is code that works properly while multiple threads execute it
concurrently. Thread-reentrant code is thread-safe, but thread-safe code may not be
thread-reentrant. Document your code as being thread-safe or thread-reentrant.
More work is involved in making code thread-reentrant than in making code thread-safe.
To make code thread-reentrant, do the following:
1. Use proper locking protocols to access global or static variables.
2. Use proper locking protocols when you use code that is not thread-safe.
3. Store thread-specific data on the stack or heap.
4. Ensure that the compiler produces thread-reentrant code.
5. Document your code as being thread-reentrant.
8.4 Avoiding Nonreentrant Software
The following subsections discuss two methods to help you avoid the pitfalls of
nonreentrant software. These methods are as follows:
• Global lock
• Thread-specific storage
8.4.1 Global Lock
Use a global lock, which has the characteristics of a recursive mutex, instead of a regular
mutex when calling routines that you think are nonreentrant. (When in doubt, assume the
code is nonreentrant.)
The pthread_lock_global_np( ) routine is a locking protocol that is used to call
nonreentrant routines, often found in existing library packages that were not designed to
run in a multithreaded environment.
The way to call a library function that is not reentrant from a multithreaded program is to
protect the function with a mutex. If every function that calls a library locks a particular
mutex before the call and releases the mutex after the call, then the function completes
8− 10 Tandem Computers Incorporated 124245