This article is written by Jean J. Labrosse, RTOS Expert.
Reentrant vs Non-reentrant functions:
If shared functions only use local variables, they are generally reentrant (assuming the compiler itself generates reentrant code).
Conversely, a non-reentrant function is one that uses one or more global variables to keep track of the state of the function. The online documentation for the IAR toolchain provides a list of non-reentrant functions as shown in Figure 1 .
Figure 1, List of non-reentrant functions provided bythe IAR STD library
A good example of a non-reentrant function can be found in the DLIB run-time C library, string.h: strtok(). This function is used to parse an ASCII string for “tokens”. The description of strtok() from the IAR documentation is shown in Figure 2 :
Figure 2, Description of strtok(), a non-reentrant function
The simple (and partial) code example shown in Figure 3 shows the use of strtok(). ptoken is a pointer to the beginning of the next token (i.e.…