Thread safety

In multi-threaded computer programming, a function is thread-safe when it can be invoked or accessed concurrently by multiple threads without causing unexpected behavior, race conditions, or data corruption.

[3] Different vendors use slightly different terminology for thread-safety,[4] but the most commonly use thread-safety terminology are:[2] Thread safety guarantees usually also include design steps to prevent or limit the risk of different forms of deadlocks, as well as optimizations to maximize concurrent performance.

However, deadlock-free guarantees cannot always be given, since deadlocks can be caused by callbacks and violation of architectural layering independent of the library itself.

The first class of approaches focuses on avoiding shared state and includes: The second class of approaches are synchronization-related, and are used in situations where shared state cannot be avoided: In the following piece of Java code, the Java keyword synchronized makes the method thread-safe: In the C programming language, each thread has its own stack.

This difficult-to-diagnose logic error, which may compile and run properly most of the time, is called a race condition.