In: Computer Science
What is a race condition in software?
Race condition usually happens when multiple process/threads tries to modify/access the shared resources at the same time. Order of operations can not be guaranteed. Result of the operation is dependent on scheduling algorithm and not in control of software. i.e. multiple process/thread are racing to read/change the data.
e.g. Consider the following scenario:
if (sharedresource == 15) //step 1
{
anotherresource = sharedresource * 2; //step 2
}
Suppose First thread is on step1 and find the value of sharedresource as 15, but before executing step 2 (between Step1 and Step2), another thread/process came and executing the whole code, so now value of anotherresource would be already 30 and when First thread execute the step 2, it would become 60 but expectation was 30.
//If you want to provide explanation for prevention then use below
To prevent Race conditions, programming languages provide different constructs. For e.g. you can use locks for the code segment used above and release the lock after executing the code, this way second thread will wait till the operation completes.