Thread safety

from Wikipedia, the free encyclopedia

Thread Safety ( English thread safety ) is a property of software components and is an important component in the software development . It states that a component can be executed multiple times by different program areas at the same time without these interfering with one another.

Parts of a computer program can be executed several times at the same time. This is often a component or just a function of the program. Operating systems offer so-called threads for the purpose of multiple execution . Each thread processes this part of the program independently of the others. The program often has to access a shared memory area of the computer at the same time . Changes to the individual threads must be coordinated in order to prevent the memory from becoming chaotic. One speaks here of synchronization . There are several concepts for thread synchronization, such as: B. Locks (English for lock : a thread locks a memory area for exclusive processing, while other threads have no access to this memory area until it is released again), mutexes and semaphores .

If multiple threads can be accessed at the same time, the component is also called entry-invariant or reentrant .

Inconsistencies

The simultaneous manipulation of data by several threads can lead to so-called inconsistent data. This happens when a thread changes data, but other threads do not find out and continue to assume the old state of the data. Continuous calculations are now based on outdated data and are therefore incorrect. An example of this problem is the race condition .

Examples

Salary data

One component is used to manage salary data. The program can be used by two HR employees at the same time. Let us assume that employee A increases the salary of an employee of 1000.00 euros by an inflation adjustment of 100.00 euros. Almost at the same time, employee B increases the salary by 200 euros due to special performance. Overall, the salary should increase to 1300.00 euros.

When using non-thread-safe software, the following processing sequence can occur:

  • Employee A reads the current salary (1000.00 euros).
  • Employee B reads the current salary (1000.00 euros).
  • Employee A increases the current salary by 100.00 euros and saves 1100.00 euros.
  • Employee B increases the current salary by 200.00 euros and saves 1200.00 euros.

The reason is that when the second change was made, the first was not known. So only 200.00 euros are added to the original salary. The first change is overwritten and lost (so-called "lost update " problem).

Changes to a MediaWiki page

An example of a thread-safe application is MediaWiki. It is possible for two authors to change the same page at the same time. The author, who saves the page second, receives a warning from the software and a message that a change has already been made. It is now his responsibility to resolve this conflict without overwriting the previous post.

Synchronization in programming languages

Some elementary functions of programming languages that only process local variables are always thread-safe, for example most mathematical functions that always store intermediate results on the stack and always work on copies of the original variables .

See also