Originally Posted by
brewbuck
The definition of thread safe is "safe to call from multiple threads." I'm not sure what else you are looking for. Another term with a similar meaning is "reentrant" which simply means that a region of code can safely be activated multiple times. That's more encompassing than "thread safe" because it also implies that the function is recursion-safe.
Ok, so what if we have
Code:
char *foo() {
return asctime();//not a thread-safe function
}
char *goo() {
return ascitime();//still not thread-safe
}
int main() {
StartThread(Thread);
fprintf(file0, "goo(): %s\n", goo());
...
}
void Thread() {
fprintf(file1, "foo(): %s\n", foo());
}
Both foo() and goo() were labeled as not thread-safe, so we were careful not to have one of them called in 2 threads, but both call the same function that isn't thread-safe, so we have a concurrency problem, even though we've respected "thread-safety".
Another important ramification is if you have 2 3rd party libraries, and both are not thread-safe, you can't trust them in a multi-threaded program, even if you confine all your calls to each library to its own thread.