Data Races in Programming

And how to prevent them

Steven Curtis
4 min readJan 27, 2023
Photo by Lars Kienle on Unsplash

When a programmer creates software they typically wish that software to be predictable.

Many pieces of software are deterministic so given a set of inputs the same output is always given. Although non-deterministic algorithms have their uses (for example to approximate answers) this focuses on deterministic functions.

Determinism and Data Races

In the following java class, we would generally wish the output to always be the same for the same input. In the example below, we would expect the sum of 4 and 5 to be 9.

public static int add(int number, int num){
return number + num;
}

int result = add(4, 5); // 9

This helps testing, as the input wholly predicts the output. More generally we can see the difference between deterministic and non-deterministic functions in the diagram below:

--

--