Open System Services Programmer's Guide

Reentrant functions include the _r suffix in their names. To determine if a function is reentrant, see
the reference page for that function either online or in the Open System Services System Calls
Reference Manual.
Many functions are unsafe in multithreaded applications, often because these functions return a
pointer to a string or structure that is in static storage. A second call to the function overwrites the
static storage (the results from the first call). Other issues for multithreaded applications include the
use of global variables and maintaining context across calls.
In programs that use multiple threads, the same functions and the same resources might be accessed
concurrently by several flows of control. To protect resource integrity, code written for multithreaded
programs must be reentrant and thread safe.
A reentrant function does not hold static data over successive calls and does not return a pointer
to static data. All data is provided by the caller of the function.
Example Program Using Reentrant Functions
Example 94 (page 410) provides an example program that uses the strtok_r() reentrant fuction.
The program results in this ouput:
thread - 0 token - a
thread - 1 token - a
thread - 0 token - b
thread - 1 token - b
thread - 0 token - c
thread - 1 token - c
thread - 0 token - x
thread - 1 token - x
thread - 0 token - y
thread - 1 token - y
thread - 0 token - z
thread - 1 token - z
For comparison, the same program, using strtok(), instead of the reentrant version,
strtok_r(), results in this output:
thread - 0 token - a
thread - 1 token - a
thread - 0 token - b
thread - 1 token - c
thread - 0 token - x
thread - 1 token - y
thread - 0 token - z
thread - 1 token - x
thread - 0 token - y
thread - 1 token - z
Reentrant OSS Functions 409