I know that errno is thread-safe. And in Linux, there are another 2 similar functions, namely strerror and strerror_r. According to the book Linux System Programming by Robert Lover, strerror is not thread-safe but strerror_r is. So I am wondering if perror is thread-safe or not.
In POSIX systems (like Linux), perror is thread-safe.
perror is not listed as non-thread safe here:
All functions defined by this volume of POSIX.1-2008 shall be thread-safe, except that the following functions1 need not be thread-safe. [...]
http://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html
Quote from C11 (earlier standards did not acknowledge multi-threading at all):
7.1.4 Use of library functions
[...]
4 The functions in the standard library are not guaranteed to be reentrant and may modify objects with static or thread storage duration.188)
5 Unless explicitly stated otherwise in the detailed descriptions that follow, library functions shall prevent data races as follows: A library function shall not directly or indirectly access objects accessible by threads other than the current thread unless the objects are accessed directly or indirectly via the function’s arguments. A library function shall not directly or indirectly modify objects accessible by threads other than the current thread unless the objects are accessed directly or indirectly via the function’s non-const arguments.189) Implementations may share their own internal objects between threads if the objects are not visible to users and are protected against data races.
If none of the arguments to perror can be concurrently accessed by other threads (there thus cannot be data-races on them), perror is thread-safe, just like all other C11 standard library functions where no explicit exception is listed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With