Introduction
A race condition occurs when two or more threads are able to access shared data and they try to change it at the same time. Because the thread scheduling algorithm can swap between threads at any point, we cannot know the order at which the threads will attempt to access the shared data. Therefore, the result of the change in data is dependent on the thread scheduling algorithm, i.e. both threads are 'racing' to access/change the data. The reason for having more race conditions among threads than processes is that threads can share their memory while a process can not.
For example Let's say we have two threads, T1 and T2, where both are accessing a shared variable x. They first read data from the variable and then try to write data on the variable simultaneously. They would race to find which of these threads can write the value last to the variable. In this case, the last written value would be saved.
As another example, we can have these two threads, T1 and T2, trying opposite operations on this variable. Let thread T1 increase the value of the variable and the thread T2 decrease the value of the variable at the same point of time. What is the result? The value remains unchanged.
Race Conditions
Consider the example shown below. We have a variable result and there are 3 threads trying to write a unique value to it
using
System;
using
System.Threading;
namespace
RaceCondition
class Akshay
{
int result = 0;
void Work1() { result = 1; }
void Work2() { result = 2; }
void Work3() { result = 3; }
static void
Main(string[] args)
{
Akshay a =
new Akshay();
Thread worker1 =
new Thread(a.Work1);
Thread worker2 =
new Thread(a.Work2);
Thread worker3 =
new Thread(a.Work3);
worker1.Start();
worker2.Start();
worker3.Start();
Console.WriteLine(a.result);
Console.Read();
}
}
}
output :
Looking at the code it might seem that the result
at the end will be 3. But if you try executing the code you will find out that
it is not always true. The result might be 1, 2 ,3 or even 0 as





Sujit PrabhakaranPosted Mar 28, 2024, 12:14 AM
Even now, I am challenged by this question in an interview yesterday. Dear Akshay, can you share with me more details on WaitHandles. If possible share with me the online resource or ebook where you learnt this concept. I would like to read more as I am still not getting a clear picture.
Mahdi bycheckPosted Jun 26, 2021, 4:46 AM
Thanks for this usfuel text
Navnath KumbharPosted Apr 24, 2018, 12:05 AM
Good Explanation! Thank you.
Nagi APosted Apr 7, 2017, 4:03 AM
Good Explanation. Thank you Akshay
Jeetesh SrivastavaPosted Mar 21, 2012, 7:51 AM
thanks as it was very useful