Parallel Task Library (TPL) handles race condition

I'm trying to understand the benefits of a parallel task library using traditional multithreading, and when I think of the situation below, I get stuck thinking that it handles the race condition or do we need to deal with this in our code?

Here is my code:

int depdt = 0; Parallel.For(1, 10, mem => { for (int dep = 1; dep <= 10; dep++) { depdt = dep; Console.WriteLine("MEMBER: " + mem + " " + "Dependent: " + dep); } Console.WriteLine("Dep Value: " + depdt + " " + "mem: " + mem); }); Console.ReadKey(); 

I ran it a couple of times, and I do not see any thread interfering / overwriting the variable "depdt". But I need to confirm this. (or) To make it thread safe, I have to manually create an instance of the class and implement it as the code below to avoid race conditions.

  int depdt = 0; Parallel.For(1, 10, mem => { Worker worker = new Worker(); worker.DoWork(mem); }); Console.ReadKey(); public class Worker { public void DoWork(int mem) { int depdt = 0; for (int dep = 1; dep <= 10; dep++) { depdt = dep; Console.WriteLine("MEMBER: " + mem + " " + "Dependent: " + dep); } Console.WriteLine("Dep Value: " + depdt +" "+ "mem: "+ mem); } } 

Reply on @yms: I mean that when using regular threads, varaible depdt becomes unreliable. Here is my code:

 for (int mem = 1; mem <= 10; mem++) { var t= new Thread(state => { for (int dep = 1; dep <= 10; dep++) { depdt = dep; Console.WriteLine("MEMBER: " + mem + " " + "Dependent: " + dep); } Console.WriteLine("Dep Value: " + depdt + " " + "mem: " + mem); }); t.Start(string.Format("Thread{0}", mem)); } Console.ReadKey(); 

Here is my output screen: Infact and mem and dep variables have become unreliable

enter image description here

+4
source share
2 answers

If you expect your program to always write Dep Value: 10 , then yes, your program is subject to a race condition, which may lead to the printing of other values. To demonstrate this problem, simply enter a delay in the inner loop:

 int depdt = 0; Parallel.For(1, 10, mem => { for (int dep = 1; dep <= 10; dep++) { depdt = dep; Console.WriteLine("MEMBER: " + mem + " " + "Dependent: " + dep); Thread.Sleep(mem * 100); // delay introduced here } Console.WriteLine("Dep Value: " + depdt + " " + "mem: " + mem); }); Console.ReadKey(); 

The reason your program behaves correctly is because the inner loop takes very little time to execute, it may complete within one quantum of time allocated to the thread.

To avoid the race condition, you just need to move the depdt inside the anonymous function passed to Parallel.For . This will cause each thread to have its own variable, avoiding conflicts.

 Parallel.For(1, 10, mem => { int depdt = 0; for (int dep = 1; dep <= 10; dep++) { depdt = dep; Console.WriteLine("MEMBER: " + mem + " " + "Dependent: " + dep); } Console.WriteLine("Dep Value: " + depdt + " " + "mem: " + mem); }); 
+3
source

Not. The parallel task library does not fulfill the default race conditions. You need to take care of synchronizing access to shared resources.

+1
source

All Articles