Thread (un)safety
4 types of thread-unsafe functions
Easy to fix, add lock
Function that keeps state across multiple invocation.
Such as rand. solution: stop using state via static variable. Only solution is to change the function such that it doesn’t use static and provide that value via variable.
function that return pointer to static
function that use thread-unsafe function
Thread reentrant
Thread-reentrant is thread function that doesn’t rely on any shared data.
Not the same as thread safe. A function can be thread-safe but not reentrant.
Race
Race condition occurs when the correctness of the program depends on the order of execution.
Deadlock
A pair (or multiple) threads that are blocked because they need resources from other threads.
For mutex, we can prevent it by locking it in one order and unlocking it in reverse order.