clinuxmultithreadingthread-safetysystem-calls

How can I know whether a Linux syscall is thread safe?


Some functions in linux are marked "thread safe" by _r (e.g. gmtime_r ). But most of the syscalls are not be marked and also not mentioned in the man pages. So my question is: How can I know whether a linux syscall is thread safe? Thank you!


Solution

  • I think you mean "library functions"; syscalls should, by virtue of operating on the thread's kernel-side data, be thread-safe.

    And the answer is: check the manual pages for the functions in question. The "_r" variants are provided specifically for functions which were non-reentrant, meaning that the extra parameters passed to them were statically declared and modified in the non-"_r" versions.

    Most of glibc should be, IIRC, thread-safe, but you always need to check manual pages; or, if you don't trust those, the code itself. There's no silver bullet that will remove from you the responsibility of understanding the interfaces which you are programming against.