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.

FunctionLimiting factors for use in threads
Math functions setting errno:

acos, acosf, acosl

acosh, acoshf, acoshl

asin, asinf, asinl

asinh, asinhf, asinhl

atanh, atanhf, atanhl

cosh, coshf, coshl

erf, erff, erfl

erfc, erfcf, erfcl

exp, expf, expl

exp2, exp2f, exp2l

exp1m, expm1f, expm1l

lgamma, lgammf, lgammal

log, logf, logl

log2, log2f, log2l

log10, log10f, log10l

log1p, log1pf, log1pl

pow, powf, powl

sqrt, sqrtf, sqrtl

sinh, sinhf, sinhl

tanh, tanhf, tanhl

Use of shared errno object
File functions setting errno

fgetpos, fsetpos

ftell

remove

fopen, freopen

Use of shared errno object
IO functions setting errno

vswprintf, vsnprintf, vfwprintf, vfprintf, vsnprintf, vsnprintf, snprintf

fgetwc, fputwc

vfscanf, scanf, sscanf, vscanf, vssscanf

Use of shared errno object
Multibyte and wide string functions setting errno

mbrtowc, mbtowc, mbsrtowcs

wcrtomb, wcsrtombs

wcstoimax, wcstoumax

wcstod, wcstof, wcstold

wcstol, wcstoll, wcstoul, wcstoull

Use of shared errno object
Time functions setting errno

gmtime, mktime, localtime

Use of shared errno object
String functions setting errno

strtoimax, strtoumax

strtod, strtof, strtold

strtol, strtoll, strtoul, strtoull

Use of shared errno object
System functions setting errno

signal, system

Use of shared errno object
Memory allocation functions using errno

malloc, calloc, realloc

Use of shared errno object
Error functions setting errno

perror, strerror

Use of shared errno object
Formatted IO functions with MPLAB XC8:

printf, vprintf, vsprintf, snprintf, sprintf, vsnprintf

sscanf, vsscanf, scanf, vscanf

Use of shared object
asctime, localtimeUse of shared object
exit, atexit, abortRegistered functions called before exiting
lgamma, lgammaf, lgammalUse of shared object
mbrlen, mbrtowcUse of shared object; Use of an initial conversion state object
mbsrtowcsUse of an initial conversion state object

malloc, calloc, realloc

Use of shared object
rand, srandUse of shared object
setlocaleUse of shared object
strtokUse of shared object
wcrtomb, wcsrtombsUse 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 the errno 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 the atexit() function. If any registered functions are not thread safe, their execution might affect other threads and lead to program failure.