Thread-safe |
Thread-safety is a computer programming concept applicable in the context of multi-threaded programs. A piece of code is thread-safe if it functions correctly during simultaneous execution by multiple threads.
Common ways of creating thread-safe code include writing Reentrant code, using Thread Local Storage to localize data to each thread, guarding shared data with mutual exclusion so that only one thread uses it at a time, and modifying shared data with atomic operations.
Thread-safety is a key challenge in multi-threaded programming. It was once only a concern of the operating system programmer but has of late become a commonplace issue to be tackled by the everyday programmer. In a multi-threaded program, several thread (computer programming)s execute simultaneously in a shared address space. Every thread has access to virtually all the computer storage of every other thread. Thus the flow of control and the sequence of accesses to data often have little relation to what would be reasonably expected by looking at the text of the program. This violates the principle of least astonishment. Thread-safety is a property aimed at minimizing surprising behaviour by re-establishing some of the correspondences between the actual flow of control and the text of the program.
The requirement for thread-safety highlights the inherent tension in multi-threaded programming: the need for multiple threads to access the same shared data, and the need for a shared piece of data to be accessed by only one thread at any given time.
It is not easy to determine if a piece of code is thread-safe or not. However, there are several indicators that suggest the need for careful examination to see if it is unsafe:
A Subroutine that only uses variables from the stack, depends only on the arguments passed in, and calls other subroutines with similar properties (a so-called pure function in C compiler circles) is Reentrant, and thus thread-safe.
As seen in the definition, there are a few ways to achieve thread-safety:
A commonly used idiom combines these approaches:
The concept of exception handling is closely related, since it again deals with (synchronous) flows of control not directly correlated to the text of a program. Several of the idioms used are similar.
=See also=
*Control flow analysis *Priority inversion *Concurrency control
=External links=
*[http://whatis.techtarget.com/definition/0,,sid9_gci331590,00.html Short description] *[http://davinci01.man.ac.uk/aix433/aixprggd/genprogc/writing_reentrant_thread_safe_code.htm Writing Reentrant and Thread-Safe Code] *Article [http://javalobby.org/articles/thread-safe/index.jsp Thread-safe webapps using Spring] by Steven Devijver *[http://www.javaworld.com/javaworld/javaqa/1999-04/01-threadsafe.html Thread-safe design] *Article [http://www.javaworld.com/javaworld/jw-08-1998/jw-08-techniques.html Design for thread safety] by Bill Venners *Article [http://www.javaworld.com/javaworld/jw-07-2004/jw-0712-threadsafe.html Write thread-safe servlets] by Phillip Bridgham *Article [http://jelovic.com/articles/smart_pointer_thread_safety.htm Smart Pointer Thread Safety] by Dejan Jelovic *[http://citeseer.org/csq=thread+safety Citations from CiteSeer]|
|