[Return to Bookshelf] [Contents] [Previous Section] [Next Section] [Index] [Help]


3.2.1 Thread Reentrant

A routine is thread-reentrant when it functions normally despite being called simultaneously or sequentially by different threads. Again, taking the hypothetical example of strtok, the traditional interface could most efficiently be made thread- reentrant by adding an argument that specifies a context for the sequence of tokens. Thus, multiple threads could simultaneously parse different strings without interfering with each other.

The ideal thread-reentrant routine has no dependency on static data. Because static data must be synchronized using mutexes and condition variables, there is always a performance penalty due to the time required to lock and unlock the mutex and also in the loss of potential parallelism throughout the program. A routine that does not use any data that would be shared between threads can proceed without locking.

If you are developing new interfaces, make sure that any persistent context information (like the last-token-returned pointer in strtok) is passed explicitly so that multiple threads can process independent streams of information independently. Return information to the caller through routine values, output parameters (where the caller passes the address and length of a buffer), or by allocating dynamic memory and requiring the caller to free that memory when finished. Try to avoid using errno for returning error or diagnostic information; use routine return values instead.