Home » What is Race Condition

What is Race Condition

by Online Tutorials Library

What is Race Condition

Race Condition or Race Hazard is an undesirable situation of software, electronics, or other systems. When the output of the system or program depends on the sequence or timing of other uncontrolled events, this condition is called Race Condition.

This condition occurs mainly in the logic circuits, distributed and multithreaded software programs.

A race condition is categorized as either a critical or non-critical race condition. The critical race conditions are those conditions that occur when the order of the internal variable regulates the last state of the machine. On the other hand, the non-critical race conditions are those conditions which occur when the order of internal variables does not regulate the last state of the machine.

Race Condition in Electronics

The following example describes the race condition in electronic systems:

In this example, we use a logic gate which handles the Booleans values.

Let’s take an AND logic gate, which accepts two A and B inputs, and gives only one output in the result.

When the inputs of A and B are TRUE, this gate will give the TRUE output, and when the value of one or both inputs are false, then AND gate will give Boolean FALSE.

Here, the race condition occurs when the program checks the output of the logic gate before the insertion of value in both A and B variables.

The correct set of operations for finding the output of AND gate is mentioned below:

  1. Input value in Variable A,
  2. Input value in Varibale B,
  3. Output of AND logic gate.

The race condition occurs in the following set of operations:

  1. Input value in Variable A,
  2. Output of AND logic gate.
  3. Input value in Varibale B,

Race Condition in Software

A race condition occurs in the software when the computer program depends on the threads or processes of a program.

In software, we cannot debug the race condition because the final result is non-deterministic and based on the timing of multiple threads.

Now, we take the following example to describe how the race condition occurs in the process:

Suppose two threads Thread 1 and Thread 2reduce the value of the global integer variable by 2.

The following table shows the consecutive order of operations:

Thread 1 Thread 2 Integer value
10
Read the Value 10
Reduce the Value 10
Write the value by 2 8
Read the value 8
Reduce the Value by2 8
Write the value 6

In the above table, the final value is 6, as expected.

However, if the operations of these two threads execute concurrently without the concept of locking or synchronization, the outcome of the operations could be wrong.

The alternative sequence of the operations is shown in the following scenario:

Thread 1 Thread 2 Integer value
Read the Value 10
Read the value 10
Reduce the Value by 2 10
Reduce the Value by 2 10
Write the value 8
Write the value 8

In this scenario, the final value is 8 instead of 6. This happens because the decrement operations of Thread 1 and Thread 2 are not mutually exclusive.

Those operations which cannot create interruptions while accessing some resources are called mutually exclusive operations.


Next TopicSQL COUNT DISTINCT

You may also like