A race condition or race hazard is a scenario in an electronic processing system where the result of a calculation might be affected by an unforeseen or uncontrolled sequence of events. The underlying concept is that the results of a process should never be affected by one of the operations "winning a race" (finishing first).
Race condition example
Consider the following set of operations:
- variable a = 1
- variable a = a * 5
- variable b = a - 1
If these operations are processed in the proper sequence, variable b should equal 4. But if operation 3 is executed before operation 2 has completed, b would equal 0.
Race conditions in Processors
Race conditions are an important problem in CPU design that use both parallel and concurrent processing techniques to execute multiple instructions in an overlapping time frame. To ensure that the output of calculations is not corrupted, race conditions must always be anticipated and avoided.
Race conditions in Software
It's also an important problem for software developers, who must handle any race conditions that may occur when their code is used in real-world situations. Famously, an improperly handled race condition in the software of NASA's Spirit Explorer Rover nearly resulted in the rover being lost shortly after it landed on Mars.