About

A Race condition is the only concurrent problem that can happen when two threads manipulate the same state (value) in the same time-lapse, the last thread to write the state will overwrite the state modification of the first thread.

same as Concurrency - Thread Interference (Interleave on shared data) ??

Race conditions have a reputation of being difficult to reproduce and debug, since the end result is non-deterministic and depends on the relative timing between interfering threads.

Problems occurring in production systems can therefore disappear when running in debug mode, when additional logging is added, or when attaching a debugger, often referred to as a wiki/Heisenbug. It is therefore better to avoid race conditions by careful software design rather than attempting to fix them afterwards.

If two threads run simultaneously without locking or synchronization, the outcome of the operation could be wrong.

Example

  • An object with a counter property with the state 1
  • The thread 1 enters an object and see the state 1
  • The thread 2 enters also the method and see the state 1
  • The thread 1 adds 1 to the counter, the state is 2
  • The thread 2 adds 1 to the counter, the state is 2 (whereas it should be 3)

Resolution

Data Problem

Raise conditions lead to data problem called phenomena.

Documentation / Reference