c# 多線程環(huán)境下控制對共享資源訪問的解決方法
更新時間:2024年07月30日 09:37:46 作者:我只吃飯不洗碗
這篇文章主要介紹了c# 多線程環(huán)境下控制對共享資源訪問的解決方法,本文通過實例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧
c# 多線程環(huán)境下控制對共享資源訪問的辦法
- Monitor:
- 定義:
Monitor
是 C# 中最基本的同步機(jī)制,通過Enter
和Exit
方法來控制對共享資源的訪問。它提供了排他鎖的功能,確保在任何時刻只有一個線程可以訪問共享資源。 - 優(yōu)點:簡單易用,適合對臨界區(qū)進(jìn)行粗粒度的同步控制。
- 缺點:只能實現(xiàn)排它鎖,不能實現(xiàn)讀寫鎖,性能相對較低。
- 定義:
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
是一個操作系統(tǒng)對象,用于在進(jìn)程間共享,通過WaitOne
和ReleaseMutex
來控制對共享資源的訪問。它提供了進(jìn)程間的同步能力。 - 優(yōu)點:可跨進(jìn)程使用,適合在進(jìn)程間進(jìn)行同步。
- 缺點:相比
Monitor
,性能開銷較大,因為涉及到系統(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
實現(xiàn)了讀寫分離鎖,允許多個線程同時讀取共享資源,但只允許一個線程寫入共享資源。這種機(jī)制適用于讀多寫少的場景。 - 優(yōu)點:適合讀多寫少的場景,提高了并發(fā)性能。
- 缺點:相對復(fù)雜,可能會引起死鎖,不支持遞歸鎖。
- 定義:
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
是一個信號量,用于控制同時訪問共享資源的線程數(shù)量。通過WaitOne
和Release
方法,可以控制訪問資源的線程數(shù)量。 - 優(yōu)點:可以控制多個線程同時訪問共享資源的數(shù)量,靈活性較高。
- 缺點:相對于其他機(jī)制,使用起來較為復(fù)雜,需要謹(jǐn)慎處理信號量的釋放。
- 定義:
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
是輕量級的信號量,與Semaphore
類似,用于控制同時訪問共享資源的線程數(shù)量,但相比Semaphore
更輕量級。 - 優(yōu)點:相比
Semaphore
,SemaphoreSlim
的開銷更小,適用于資源訪問頻繁的場景。 - 缺點:與
Semaphore
相比,功能上略有限制,例如沒有Release(Int32)
方法,只能遞增信號量一個單位。
- 定義:
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)鍵字,用于在代碼塊級別實現(xiàn)互斥鎖,保護(hù)共享資源不被多個線程同時訪問。 - 優(yōu)點:簡單易用,適合對臨界區(qū)進(jìn)行細(xì)粒度的同步控制,編寫起來比較方便。
- 缺點:只能用于單線程內(nèi)部的同步,不能跨越線程或進(jìn)程,如果不小心使用會導(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)境下控制對共享資源訪問的辦法的文章就介紹到這了,更多相關(guān)c# 共享資源訪問內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#判斷一個字符串是否是數(shù)字或者含有某個數(shù)字的方法
這篇文章主要介紹了C#判斷一個字符串是否是數(shù)字或者含有某個數(shù)字的方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2018-06-06