Race Condition C#

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. To fully understand a race condition we will first talk about shared resources and than discuss about what is a race condition in threading.


Shared Resources

Not all resources are meant to be used concurrently. Resources like integers and collection must be handled carefully when accessed through multiple threads, resources that are accessed and updated within multiple threads are known as Shared Resources. Let’s see an example,

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading;
 6 using System.Threading.Tasks;
 8 namespace _01_Shared_Resources
 9 {
10     class Program
11     {
12         private static int sum;
14         static void Main(string[] args)
15         {
16             //create thread t1 using anonymous method
17             Thread t1 = new Thread(() => {
18                 for (int i = 0; i < 10000000; i++)
19                 {
20                     //increment sum value
21                     sum++;
22                 }
23             });
25             //create thread t2 using anonymous method
26             Thread t2 = new Thread(() => {
27                 for (int i = 0; i < 10000000; i++)
28                 {
29                     //increment sum value
30                     sum++;
31                 }
32             });
35             //start thread t1 and t2
36             t1.Start();
37             t2.Start();
39             //wait for thread t1 and t2 to finish their execution
40             t1.Join();
41             t2.Join();
43             //write final sum on screen
44             Console.WriteLine("sum: " + sum);
46             Console.WriteLine("Press enter to terminate!");
47             Console.ReadLine();
48         }
49     }
50 }


There is really some problem with the code above because every time we run it we see different output.

To truly understand the problem we must first understand what is a Race condition.

What Is Race Condition?

Race Condition is a scenario where the outcome of the program is affected because of timing.

A race condition occurs when two or more threads can access shared data and they try to change it at the same time. Because the thread scheduling algorithm can swap between threads at any time, you don’t know the order in 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.

In our case, the line which is causing race condition is sum++, though this line seems to single line code and must not affect with concurrency but this single line of code gets transformed into multiline processor level instructions by JIT at the time of execution, below is the example

1 mov eax, dword ptr [sum]
2 inc eax
3 mov dword ptr [sum], eax

So what happens when our multiple threads execute this part of the code.

Let’s assume there is this thread X and thread Y.

Suppose thread X reads the value of some variable and store in register X.eax for increment but after doing increment from value 0 to 1, X thread got suspended by Thread scheduler and Y thread start executing this part of the code where Y thread also reads the value of variable sum in register Y.eax and does the increment from value 0 to 1 and now after doing this increment both thread will update sum variable to 1 thus its value will be 1 even though both the threads incremented the value.

So in simple words, it’s just the race between threads X and Y to read and update the value of variable sum and thus cause the race condition.

But we can overcome this kind of problems using some of the thread synchronization techniques that are,

  • Atomic Update
  • Data Partitioning
  • Wait-Based Technique

In the next post C# Thread Synchronization, we will learn more about these thread synchronization techniques .

Race Condition C#
Share this

Subscribe to Code with Shadman