Hi
It there a way to detect non reentrant or thread unsafe function
usage at compile time? Any option in gcc?
The compiler might be able to prove that some functions are thread-safe:
for example, it can know that a function is pure if it doesn't reference
static or global variables (only automatic variables, which are kept
within its stack frame), doesn't use pointers, and only uses pure
standard library functions.
Similarly, it can know that some functions are thread-unsafe: for
example, a function that modifies a global variable without first
locking a mutex.
But for most functions, it will be completely impossible for the
compiler to know whether they are thread-safe or not. As soon as a
function calls another function that's defined in some external library
(which may not even be written in C, even if the source code is
available), what possible hope is there for the compiler to decide
whether it's thread-safe?
I don't know how sophisticated static analysis tools have become in
trying to decide thread-safety, if that's your question, but they
certainly can't always succeed.