Share data between Tasks using locks in .NET C#
January 28, 2014 5 Comments
You need to be careful when sharing mutable data across several tasks. If you don’t restrict the access to a shared variable then the tasks will all modify it in an unpredictable way. It’s because you cannot be sure how the tasks will be started and managed by the operating system: it depends on a range of parameters, such as the CPU usage, available memory, etc. If each task is supposed to modify a variable then maybe the first task won’t finish its work before the second task reads the same variable.
Consider the following:
class BankAccount { public int Balance { get; set; } }
The following code builds a list of tasks that each increase the balance by 1:
BankAccount account = new BankAccount(); List<Task> tasks = new List<Task>(); for (int i = 0; i < 5; i++) { tasks.Add(Task.Factory.StartNew(() => { for (int j = 0; j < 1000; j++) { account.Balance = account.Balance + 1; } })); } Task.WaitAll(tasks.ToArray()); Console.WriteLine(string.Concat("Expected value 5000", ", actual value ",account.Balance));
We expect the final balance to be 5000. Run the code and you will most likely get something less, such as 3856 or 4652. At times you get exactly 5000 but it’s unpredictable.
In such cases you need to identify the volatile critical regions in your code. In this example it’s simple:
account.Balance = account.Balance + 1;
It is the account balance that is modified by each thread so we need to lock it for access so that only one thread can modify it at a certain time. You can take the word “lock” almost literally as we use the lock keyword to do that. We take an object which will act as the synchronisation primitive. The lock object must be visible to all tasks:
object lockObject = new object();
You use the lock keyword as follows:
for (int i = 0; i < 5; i++) { tasks.Add(Task.Factory.StartNew(() => { for (int j = 0; j < 1000; j++) { lock (lockObject) { account.Balance = account.Balance + 1; } } })); }
The volatile region of the code is locked using the lock object. The lock is released as soon as the current task has finished working with it.
Run the modified code and you should get the correct result.
View the list of posts on the Task Parallel Library here.
Thank you for sharing.
This is cool. Thanks
This is cool. Thanks 🙂
Really Helpful !!
But when there is Race condition of NanoSec between task,, Lock doesn’t work