4 Functions Not Thread Safe
Some functions in this standard library use permanent storage duration objects that are shared by different instances of those functions or by other library functions. As a result, such functions are not considered to be thread safe, as the concurrent use of these functions in multiple threads, or from interrupt functions and main-line code, could lead to corruption of the shared objects and code failure.
The following table shows those functions that are not thread safe, with a brief explanation of why. Note that some functions appear more than once for different reasons. Only use these functions concurrently in multiple threads if you can confirm that any data corruption will not impact on the operation of the program.
Function | Limiting factors for use in threads |
---|---|
Math functions setting
errno :
| Use of shared errno object |
File functions setting
errno
| Use of shared errno object |
IO functions setting
errno
| Use of shared errno object |
Multibyte and wide string functions setting
errno
| Use of shared errno object |
Time functions setting
errno
| Use of shared errno object |
String functions setting
errno
| Use of shared errno object |
System functions setting
errno
| Use of shared errno object |
Memory allocation functions using
errno
| Use of shared errno object |
Error functions setting
errno
| Use of shared errno object |
Formatted IO functions with MPLAB XC8:
| Use of shared object |
asctime , localtime | Use of shared object |
exit , atexit ,
abort | Registered functions called before exiting |
lgamma , lgammaf ,
lgammal | Use of shared object |
mbrlen , mbrtowc | Use of shared object; Use of an initial conversion state object |
mbsrtowcs | Use of an initial conversion state object |
| Use of shared object |
rand , srand | Use of shared object |
setlocale | Use of shared object |
strtok | Use of shared object |
wcrtomb , wcsrtombs | Use of an initial conversion state object |
Notes Pertaining to Limiting Factors
- Use of shared errno object
- The C standard
errno
object used by many math, IO, and string functions is shared and might be overwritten by subsequent calls to any of these library functions in a concurrent thread. Only when the error condition in theerrno
object is not accessed by the program can these functions be considered thread safe. - Use of shared object
- An object or buffer with permanent storage duration is used by the function without any form of locking and might be overwritten by subsequent calls to the same or other library functions in a concurrent thread.
- Use of an initial conversion state object
- Many of the functions associated with multibyte characters and wide strings
take a pointer argument that can indicate an object that describes the
current conversion state of the multibyte character sequence. If this
argument is
NULL
, an internal state object is used by the function. This internal state object has permanent program duration, hence can be corrupted by calls to the same or other multibyte library function in a concurrent thread. - Registered functions called before exiting
- Before exiting, the
exit()
function will call any function registered through previous calls to theatexit()
function. If any registered functions are not thread safe, their execution might affect other threads and lead to program failure.