PreviousNext

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.