c# 多線程環(huán)境下控制對(duì)共享資源訪問(wèn)的解決方法
c# 多線程環(huán)境下控制對(duì)共享資源訪問(wèn)的辦法
- Monitor:
- 定義:
Monitor是 C# 中最基本的同步機(jī)制,通過(guò)Enter和Exit方法來(lái)控制對(duì)共享資源的訪問(wèn)。它提供了排他鎖的功能,確保在任何時(shí)刻只有一個(gè)線程可以訪問(wèn)共享資源。 - 優(yōu)點(diǎn):簡(jiǎn)單易用,適合對(duì)臨界區(qū)進(jìn)行粗粒度的同步控制。
- 缺點(diǎn):只能實(shí)現(xiàn)排它鎖,不能實(shí)現(xiàn)讀寫(xiě)鎖,性能相對(duì)較低。
- 定義:
class Program
{
static readonly object _lock = new object();
static int _counter = 0;
static void Main()
{
for (int i = 0; i < 10; i++)
{
new Thread(IncrementCounter).Start();
}
Console.ReadKey();
}
static void IncrementCounter()
{
Monitor.Enter(_lock);
try
{
_counter++;
Console.WriteLine($"Counter: {_counter}");
}
finally
{
Monitor.Exit(_lock);
}
}
}
Monitor- Mutex:
- 定義:
Mutex是一個(gè)操作系統(tǒng)對(duì)象,用于在進(jìn)程間共享,通過(guò)WaitOne和ReleaseMutex來(lái)控制對(duì)共享資源的訪問(wèn)。它提供了進(jìn)程間的同步能力。 - 優(yōu)點(diǎn):可跨進(jìn)程使用,適合在進(jìn)程間進(jìn)行同步。
- 缺點(diǎn):相比
Monitor,性能開(kāi)銷(xiāo)較大,因?yàn)樯婕暗较到y(tǒng)調(diào)用。
- 定義:
class Program
{
static Mutex _mutex = new Mutex();
static int _counter = 0;
static void Main()
{
for (int i = 0; i < 10; i++)
{
new Thread(IncrementCounter).Start();
}
Console.ReadKey();
}
static void IncrementCounter()
{
_mutex.WaitOne();
_counter++;
Console.WriteLine($"Counter: {_counter}");
_mutex.ReleaseMutex();
}
}
Mutex- ReaderWriterLockSlim:
- 定義:
ReaderWriterLockSlim實(shí)現(xiàn)了讀寫(xiě)分離鎖,允許多個(gè)線程同時(shí)讀取共享資源,但只允許一個(gè)線程寫(xiě)入共享資源。這種機(jī)制適用于讀多寫(xiě)少的場(chǎng)景。 - 優(yōu)點(diǎn):適合讀多寫(xiě)少的場(chǎng)景,提高了并發(fā)性能。
- 缺點(diǎn):相對(duì)復(fù)雜,可能會(huì)引起死鎖,不支持遞歸鎖。
- 定義:
class Program
{
static ReaderWriterLockSlim _rwLock = new ReaderWriterLockSlim();
static int _counter = 0;
static void Main()
{
for (int i = 0; i < 5; i++)
{
new Thread(ReadCounter).Start();
new Thread(IncrementCounter).Start();
}
Console.ReadKey();
}
static void ReadCounter()
{
_rwLock.EnterReadLock();
Console.WriteLine($"Counter: {_counter}");
_rwLock.ExitReadLock();
}
static void IncrementCounter()
{
_rwLock.EnterWriteLock();
_counter++;
Console.WriteLine($"Counter incremented to: {_counter}");
_rwLock.ExitWriteLock();
}
}
ReaderWriterLockSlim- Semaphore:
- 定義:
Semaphore是一個(gè)信號(hào)量,用于控制同時(shí)訪問(wèn)共享資源的線程數(shù)量。通過(guò)WaitOne和Release方法,可以控制訪問(wèn)資源的線程數(shù)量。 - 優(yōu)點(diǎn):可以控制多個(gè)線程同時(shí)訪問(wèn)共享資源的數(shù)量,靈活性較高。
- 缺點(diǎn):相對(duì)于其他機(jī)制,使用起來(lái)較為復(fù)雜,需要謹(jǐn)慎處理信號(hào)量的釋放。
- 定義:
class Program
{
static Semaphore _semaphore = new Semaphore(2, 2); // Allow 2 threads to access the resource simultaneously
static int _counter = 0;
static void Main()
{
for (int i = 0; i < 5; i++)
{
new Thread(IncrementCounter).Start();
}
Console.ReadKey();
}
static void IncrementCounter()
{
_semaphore.WaitOne();
_counter++;
Console.WriteLine($"Counter: {_counter}");
_semaphore.Release();
}
}
Semaphore- SemaphoreSlim:
- 定義:
SemaphoreSlim是輕量級(jí)的信號(hào)量,與Semaphore類(lèi)似,用于控制同時(shí)訪問(wèn)共享資源的線程數(shù)量,但相比Semaphore更輕量級(jí)。 - 優(yōu)點(diǎn):相比
Semaphore,SemaphoreSlim的開(kāi)銷(xiāo)更小,適用于資源訪問(wèn)頻繁的場(chǎng)景。 - 缺點(diǎn):與
Semaphore相比,功能上略有限制,例如沒(méi)有Release(Int32)方法,只能遞增信號(hào)量一個(gè)單位。
- 定義:
class Program
{
static SemaphoreSlim _semaphore = new SemaphoreSlim(2, 2); // Allow 2 threads to access the resource simultaneously
static int _counter = 0;
static void Main()
{
for (int i = 0; i < 5; i++)
{
new Thread(IncrementCounter).Start();
}
Console.ReadKey();
}
static void IncrementCounter()
{
_semaphore.Wait();
_counter++;
Console.WriteLine($"Counter: {_counter}");
_semaphore.Release();
}
}
SemaphoreSlim- lock:
- 定義:
lock是 C# 中的關(guān)鍵字,用于在代碼塊級(jí)別實(shí)現(xiàn)互斥鎖,保護(hù)共享資源不被多個(gè)線程同時(shí)訪問(wèn)。 - 優(yōu)點(diǎn):簡(jiǎn)單易用,適合對(duì)臨界區(qū)進(jìn)行細(xì)粒度的同步控制,編寫(xiě)起來(lái)比較方便。
- 缺點(diǎn):只能用于單線程內(nèi)部的同步,不能跨越線程或進(jìn)程,如果不小心使用會(huì)導(dǎo)致死鎖。
- 定義:
class Program
{
static readonly object _lock = new object();
static int _counter = 0;
static void Main()
{
for (int i = 0; i < 5; i++)
{
new Thread(IncrementCounter).Start();
}
Console.ReadKey();
}
static void IncrementCounter()
{
lock (_lock)
{
_counter++;
Console.WriteLine($"Counter: {_counter}");
}
}
}
lock到此這篇關(guān)于c# 多線程環(huán)境下控制對(duì)共享資源訪問(wèn)的辦法的文章就介紹到這了,更多相關(guān)c# 共享資源訪問(wèn)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#循環(huán)與循環(huán)控制的表達(dá)式樹(shù)實(shí)現(xiàn)
這篇文章介紹了C#循環(huán)與循環(huán)控制的表達(dá)式樹(shù)實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-01-01
C#如何控制IIS動(dòng)態(tài)添加刪除網(wǎng)站詳解
這篇文章主要給大家介紹了關(guān)于C#如何控制IIS動(dòng)態(tài)添加刪除網(wǎng)站的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用C#具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
C#實(shí)現(xiàn)把dgv里的數(shù)據(jù)完整的復(fù)制到一張內(nèi)存表的方法
這篇文章主要介紹了C#實(shí)現(xiàn)把dgv里的數(shù)據(jù)完整的復(fù)制到一張內(nèi)存表的方法,需要的朋友可以參考下2014-08-08
C#實(shí)現(xiàn)Windows Form調(diào)用R進(jìn)行繪圖與顯示的方法
眾所周知R軟件功能非常強(qiáng)大,可以很好的進(jìn)行各類(lèi)統(tǒng)計(jì),并能輸出圖形。下面介紹一種R語(yǔ)言和C#進(jìn)行通信的方法,并將R繪圖結(jié)果顯示到WinForm UI界面上的方法,文中介紹的很詳細(xì),需要的朋友可以參考下。2017-02-02
WPF+Canvas實(shí)現(xiàn)平滑筆跡的示例代碼
這篇文章主要介紹了如何利用WPF+Canvas實(shí)現(xiàn)平滑筆跡效果,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)或工作有一定幫助,需要的可以參考一下2022-09-09

