A race condition is a type of error that can occur when two or more separate threads attempt to access the same piece of data. That piece of data is called the critical section.

Race conditions can occur in electrical engineering as well, but in CPSC 213, they’re generally a result of concurrent code.

Example

An example of a race condition could be something like the following:

int x = 0;
 
void thread1(){
	x = 42;
}
 
void thread2(){
	x = 31;
}

If both functions run concurrently on two separate threads, what is the result of x? The answer is that it’s impossible to know. It can either be 42 or 31, but there’s no way of knowing. Whichever instruction stores at x last will be the final result. Because the result of this operation is dependent on the order of operations rather than coded logic, it is a race condition.