c# 多線程環(huán)境下控制對(duì)共享資源訪問(wèn)的解決方法
更新時(shí)間:2024年07月30日 09:37:46 作者:我只吃飯不洗碗
這篇文章主要介紹了c# 多線程環(huán)境下控制對(duì)共享資源訪問(wèn)的解決方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧
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)銷較大,因?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
類似,用于控制同時(shí)訪問(wèn)共享資源的線程數(shù)量,但相比Semaphore
更輕量級(jí)。 - 優(yōu)點(diǎn):相比
Semaphore
,SemaphoreSlim
的開(kāi)銷更小,適用于資源訪問(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#判斷一個(gè)字符串是否是數(shù)字或者含有某個(gè)數(shù)字的方法
這篇文章主要介紹了C#判斷一個(gè)字符串是否是數(shù)字或者含有某個(gè)數(shù)字的方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-06-06C#通過(guò)DataSet讀寫(xiě)xml文件的方法
這篇文章主要介紹了C#通過(guò)DataSet讀寫(xiě)xml文件的方法,涉及C#使用DataSet操作XML的相關(guān)技巧,方法簡(jiǎn)單實(shí)用,需要的朋友可以參考下2015-05-05C# 實(shí)現(xiàn)截圖軟件功能實(shí)例代碼
這篇文章主要介紹了C# 實(shí)現(xiàn)截圖軟件功能實(shí)例代碼,需要的朋友可以參考下2017-06-06