- Multithreading in C# 5.0 Cookbook
- Eugene Agafonov
- 491字
- 2021-07-21 18:10:00
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.
- Redis Applied Design Patterns
- Objective-C應用開發全程實錄
- 軟件測試項目實戰之性能測試篇
- Git高手之路
- C/C++常用算法手冊(第3版)
- CouchDB and PHP Web Development Beginner’s Guide
- 組態軟件技術與應用
- Python極簡講義:一本書入門數據分析與機器學習
- Babylon.js Essentials
- Swift 4從零到精通iOS開發
- 持續集成與持續交付實戰:用Jenkins、Travis CI和CircleCI構建和發布大規模高質量軟件
- 深入理解BootLoader
- 算法設計與分析:基于C++編程語言的描述
- 創意UI Photoshop玩轉移動UI設計
- Building Business Websites with Squarespace 7(Second Edition)