Thread safety

related topics
{math, number, function}
{system, computer, user}
{law, state, case}
{acid, form, water}
{war, force, army}

Thread safety is a computer programming concept applicable in the context of multi-threaded programs. A piece of code is thread-safe if it can be used in a multi-threaded environment.

Thread safety is a key challenge in multi-threaded programming. It was not a concern for most application programmers of little home applications, but since the 1990s, as Windows become multithreading, and with the expansion of BSD and Linux operating systems, it has become a commonplace issue. In a multi-threaded program, several threads execute simultaneously in a shared address space. Every thread has access to virtually all the memory 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, violating the principle of least astonishment. Thread safety is a property that allows code to run in multi-threaded environments by re-establishing some of the correspondences between the actual flow of control and the text of the program, by means of Process synchronization

Contents

Identification

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:

Implementation

There are a few ways to achieve thread safety:

Relation to reentrant functions

A subroutine can be reentrant, and still not thread safe. For example, a routine to open a configuration file for reading, could be reentrant (not using any global or static variable, and using only data passed as arguments) and still, if another thread writes to the file, race conditions and strange behavior may arise.

Examples

In the following piece of C code, the function is thread-safe, but not reentrant

Full article ▸

related documents
E.164
X.500
CDR coding
Computer algebra system
Great Internet Mersenne Prime Search
Document Object Model
Curl (programming language)
International Data Encryption Algorithm
Cepstrum
Randomizer
Information hiding
Adder-subtracter
Command-line interpreter
True BASIC
Jess programming language
Sed
SNOBOL
Kvikkalkul
BASIC09
Roxen (web server)
ActiveX Data Objects
Linker
Bookmarklet
Two-out-of-five code
RenderMan Interface Specification
International Obfuscated C Code Contest
Bootstrapping
Wikipedia:Free On-line Dictionary of Computing/I - K
Semantic network
Painter's algorithm