3

some functions in linux mark "thread safe" by _r (e.g. gmtime_r ) but most of the syscalls are not be marked and also not mentioned in manpages. So my question is : How can i konw whether a linux syscall is thread safe? Thank you!

Tengchao
  • 157
  • 1
  • 3
  • 7
  • 1
    Of the standard posix functions, all except these http://pubs.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_09.html#tag_02_09_01 , are thread-safe. – nos Aug 19 '12 at 19:03

1 Answers1

7

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.

tbert
  • 2,089
  • 13
  • 14
  • 1
    +1 99.99% system calls on a multithreaded OS must be thread-safe by definition. Any 'exotics', (maybe some debugging stuff, say), may be restricteed to only one thread in a process, but will surely be highlighted as such in the OS user manual/help/man whatever. The default is 'thread-safe' - it just has to be else the OS would explode on boot. – Martin James Aug 19 '12 at 11:06
  • thank you ! you means functions in sys/xxx.h should be thread-safe.and others should refer to manpages ,Right? – Tengchao Aug 19 '12 at 11:56