"are functions in the c standard library thread safe?" Code Answer


you can find that list here, at chapter 2.9.1 thread-safety : http://pubs.opengroup.org/onlinepubs/9699919799/functions/v2_chap02.html#tag_15_09_01

that is, this is a list over functions that posix does not require to be thread safe. all other functions are required to be thread safe. posix includes the standard c library and the typical "unix" interfaces. (full list here, http://pubs.opengroup.org/onlinepubs/9699919799/functions/contents.html)

memcpy() is specified by posix, but not part of the list in 2.9.1, and can therefore be considered thread safe.

the various environments on linux at least tries to implement posix to the best of its abilities - the functions on linux/glibc might be thread-safe even if posix doesn't require it to be - though this is rarely documented. for other functions/libraries than what posix covers, you are left with what their authors have documented...

from what i can tell, posix equates thread safety with reentrancy, and guarantees there is no internal data races. you, however, are responsible for the possible external data races - such as protecting yourself from calling e.g. memcpy() with memory that might be updated concurrently.

By Donald Taylor on April 6 2022

Answers related to “are functions in the c standard library thread safe?”

Only authorized users can answer the Search term. Please sign in first, or register a free account.