Race Condition

A race condition occurs when a software program depends on the timing of one or more processes to function correctly. If a thread runs or finishes at an unexpected time, it may cause unpredictable behavior, such as incorrect output or a program deadlock.

Most software programs are multithreaded, meaning they can process several threads at once. A well-programmed application will ensure the results of each thread are processed in the expected order. If a program relies on threads that run in an unpredictable sequence, a race condition may occur.

A simple example is a logic gate that handles boolean values. The AND logic gate has two inputs and one output. If inputs A and B are true, the AND gate produces TRUE. If one or both inputs are false, it produces FALSE. A race condition may happen if a program checks the logic gate result before variables A and B are loaded. The correct process would be:

  1. Load variable A
  2. Load variable B
  3. Check result of the AND logic gate

An incorrect sequence would be:

  1. Load variable A
  2. Check result of the AND logic gate
  3. Load variable B

The result of the second example above may or may not be the same as the first example. For instance, variable B may be FALSE before and after it is loaded, which would not change the result. If A is FALSE, it does not matter whether or not B is TRUE or FALSE. However, if both A and B are true, the result should be TRUE. Loading variable B after checking the result of the logic gate would produce an incorrect result of FALSE.

The inconsistent output produced by race conditions may cause bugs that are difficult to detect. Programmers can avoid these issues by ensuring threads are processed in a consistent sequence.

Updated January 24, 2020 by Per C.

quizTest Your Knowledge

What is the purpose of a flag in computer programming?

It marks the beginning of a function.
It acts as a signal for a function.
It returns one or more values from a function.
It is passed as a parameter between functions.
Correct! Incorrect!     View the Flag definition.
More Quizzes →

The Tech Terms Computer Dictionary

The definition of Race Condition on this page is an original definition written by the TechTerms.com team. If you would like to reference this page or cite this definition, please use the green citation links above.

The goal of TechTerms.com is to explain computer terminology in a way that is easy to understand. We strive for simplicity and accuracy with every definition we publish. If you have feedback about this definition or would like to suggest a new technical term, please contact us.

Sign up for the free TechTerms Newsletter

How often would you like to receive an email?

You can unsubscribe or change your frequency setting at any time using the links available in each email.

Questions? Please contact us.