Locking with a C# lock keyword
This recipe will describe how to ensure that if one thread uses some resource, another does not simultaneously use it. We will see why this is needed and what the thread safety concept is all about.
Getting ready
To work through this recipe, you will need Visual Studio 2012. There are no other prerequisites The source code for this recipe can be found at BookSamples\Chapter1\Recipe9
.
How to do it...
To understand how to use the C# lock keyword, perform the following steps:
- Start Visual Studio 2012. Create a new C# Console Application project.
- In the
Program.cs
file, add the followingusing
directives:using System; using System.Threading;
- Add the following code snippet below the
Main
method:static void TestCounter(CounterBase c) { for (int i = 0; i < 100000; i++) { c.Increment(); c.Decrement(); } } class Counter : CounterBase { public int Count { get; private set; } public override void Increment() { Count++; } public override void Decrement() { Count--; } } class CounterWithLock : CounterBase { private readonly object _syncRoot = new Object(); public int Count { get; private set; } public override void Increment() { lock (_syncRoot) { Count++; } } public override void Decrement() { lock (_syncRoot) { Count--; } } } abstract class CounterBase { public abstract void Increment(); public abstract void Decrement(); }
- Add the following code snippet inside the
Main
method:Console.WriteLine("Incorrect counter"); var c = new Counter(); var t1 = new Thread(() => TestCounter(c)); var t2 = new Thread(() => TestCounter(c)); var t3 = new Thread(() => TestCounter(c)); t1.Start(); t2.Start(); t3.Start(); t1.Join(); t2.Join(); t3.Join(); Console.WriteLine("Total count: {0}",c.Count); Console.WriteLine("--------------------------"); Console.WriteLine("Correct counter"); var c1 = new CounterWithLock(); t1 = new Thread(() => TestCounter(c1)); t2 = new Thread(() => TestCounter(c1)); t3 = new Thread(() => TestCounter(c1)); t1.Start(); t2.Start(); t3.Start(); t1.Join(); t2.Join(); t3.Join(); Console.WriteLine("Total count: {0}", c1.Count);
- Run the program.
How it works...
When the main program starts, it first creates an object of the class Counter
. This class defines a simple counter that can be incremented and decremented. Then we start three threads that share the same counter instance and perform an increment and decrement in a cycle. This leads to nondeterministic results. If we run the program several times, it will print out several different counter values. It could be zero, but mostly won't be.
This happens because the Counter
class is not thread safe. When several threads access the counter at the same time, the first thread gets the counter value 10
and increments it to 11. Then a second thread gets the value 11 and increments it to 12. The first thread gets the counter value 12, but before a decrement happens, a second thread gets the counter value 12 as well. Then the first thread decrements 12 to 11 and saves it into the counter, and the second thread simultaneously does the same. As a result, we have two increments and only one decrement, which is obviously not right. This kind of a situation is called race condition and is a very common cause of errors in a multithreaded environment.
To make sure that this does not happen, we must ensure that while one thread works with the counter, all other threads must wait until the first one finishes the work. We can use the lock
keyword to achieve this kind of behavior. If we lock
an object, all the other threads that require an access to this object will be waiting in a blocked state until it is unlocked. This could be a serious performance issue and later, in Chapter 2, Thread Synchronization, we will learn more about this.