Skip to content

Race condition

A race condition happens when 2 processes try to do something at the same time, and the outcome depends on who finish first, leading to unexpected and buggy results.

Example

2 processes want to increment a global value. The sequence of operation should ideally run like this:

Thread 1Thread 2Integer value
0
read value0
increase value0
write back1
read value1
increase value1
write back2
If both processes work in the same time, they increase the value without synchronisation, leading to a wrong final result:
Thread 1Thread 2Integer value
0
read value0
read value0
increase value0
increase value0
write back1
write back1