Race condition

From Wikipedia, the free encyclopedia
Jump to: navigation, search

A Race condition (also called race hazard) is a problem with the design of a system. With a race condition, the result of a calculation or the behaviour of the system as a whole is dependent on how long a certain calculation takes, or when it is started. Race conditions occur in logic circuits and computer software, especially with multi-threaded or distributed systems.

Example[change | change source]

It is often difficult to explain what a race condition is, but the metaphor of a horse race can be used as an explanation.

A computer program is like a horse race. It does different things at the same time, in a horse race, there are several horses that are running the race. Each horse represents what is usually called a thread of execution. That way, one such thread may handle network communication, another one may be responsible for redrawing the user interface. In the case of a race condition, the application requires that a given horse wins the race. The application may work if horse number five wins, but it will crash if any other horse wins the race. One solution to the problem is to use synchronization. This is like several jockeys teaming up, to make sure horse number five is ahead.

On different computers, or in different situations, the application will vary in speed. Sometimes, it will be faster, other times slower. This may mean that on some systems, the race condition will never show, even though it may be easy to show it on others. Race conditions can be difficult to find. Bugs caused by race conditions are a frequent source of frustration within the profession of software development.