A function is not considered "thread safe" if it CAN NOT be interrupted in the middle of its execution and then safely called again ("re-entered") before its previous invocations complete execution.
In the C standard library, some functions fall into this category. You do not need a tool such as Valgrind to check for thread safety, instead you should read the documentation (or man page) for the particular function you are concerned about.
Usually, but not always, C offers a thread safe counterpart if a function is not thread safe.
For example, the string tokenizer function strtok
has a re-entrant version strtok_r
char *strtok(char *str, const char *delim);
char *strtok_r(char *str, const char *delim, char **saveptr);
with the difference being, your code (thread) maintains a pointer to the last tokenized string (the work in progress) instead of the function maintaining it. This allows multiple threads to call strtok_r
in parallel.
In addition, here is another link on SO discussing Threadsafe vs re-entrant behavior.
--
EDIT: More directly related to the original question. I do not believe such a tool exists that can tell you if a function is re-entrant. Tools such as ltrace
may help with this. My comments above were illustrating that documentation for the library should exist and I used the C standard library as an example.
Regarding Valgrind
, there is a tool for it called Helgrind
that can test for synchronization errors (see: http://valgrind.org/docs/manual/hg-manual.html, section 7.1)