Thread safety

Thread Safety (English thread safety ) is a property of software components and has an important role in software development. It states that a component can be run from different program areas multiple times simultaneously without them interfering with each other.

Parts of a computer program can be executed at the same time more than once. It often is a component or just a function of the program. For the purpose of multiple design provide operating systems to so-called threads. Each thread operates independently of the others from doing this part of the program. Often, the program has to simultaneously access a common memory area (shared memory ) of your computer. Changes in the individual threads have to be coordinated in order to prevent a disarray of the memory. This is also called synchronization. For the purpose of thread synchronization are several concepts, such as Locks (English for lock: A thread locks a storage area for the exclusive processing, other threads have meanwhile not have access to this memory area, until it is released ), mutexes and semaphores.

Is the simultaneous access of multiple threads possible, refers to the component as reentrant or reentrant.

Inconsistencies

The simultaneous manipulation of data by multiple threads can lead to so-called inconsistent data. This happens when a thread modifies data, other threads but did not know this and still accept the old status of the data. Ongoing calculations are now based on no more current data and are therefore wrong. An example of this problem is the race condition.

Examples

Content data

A component is used to manage data content. The program can be used simultaneously by two staff employees. Suppose employee A increases the employee salary of 1000,00 euros to compensate for inflation of 100.00 euros. Almost simultaneously increased Employee B 's salary also to 200,00 euros because of special power. Overall, the content is expected to increase to 1300.00 euros.

When using non-thread- safe software, it may come to the following processing sequence:

  • A staff reads the current content ( 1000.00 EUR ).
  • Employee B reads the current content ( 1000.00 EUR ).
  • Employee A increases the current salary by 100.00 € 1100,00 € and stores.
  • Employee B increases the current salary to 200,00 € 1200,00 € and stores.

The reason is that the first one was not known when the second amendment. It will be added to the original content only 200,00 EUR. The first change is overwritten and is lost (so-called "Lost Update" problem).

Synchronization in programming languages

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

774079
de