本筆記摘抄自:https://www.cnblogs.com/zhili/archive/2012/07/23/Mutex_And_Semaphore.html,記錄一下學習程序以備后續查用,
一、信號量(Semaphore)
信號量(Semaphore)是由內核物件維護的int變數,當信號量為0時,在信號量上等待的執行緒會堵塞;信號量大于0時,就解除堵塞,當在一個信號量上等待
的執行緒解除堵塞時,內核自動會將信號量的計數減1,在.NET下通過Semaphore類來實作信號量同步,
Semaphore類限制可同時訪問某一資源或資源池的執行緒數,執行緒通過呼叫 WaitOne方法將信號量減1,并通過呼叫Release方法把信號量加1,
先說下建構式:
public Semaphore(int initialCount,int maximumCount);通過兩個引數來設定信號的初始計數和最大計數,
下面代碼演示信號量同步的使用:
class Program { //共享資源 public static int number = 0; //初始信號量計數為0,最大計數為10, public static Semaphore semaphore = new Semaphore(0, 10);
static void Main(string[] args) { #region 執行緒同步:使用信號量實作同步 for (int i = 0; i < 10; i++) { Thread thread = new Thread(new ParameterizedThreadStart(SemaphoreMethod)); thread.Start(i); } //每次增加2個信號量,即每次釋放2個執行緒, for (int j = 0; j < 5; j++) { Console.WriteLine("紅燈轉綠燈……"); semaphore.Release(2); Thread.Sleep(1000); } Console.Read(); #endregion } /// <summary> /// Semaphore方法 /// </summary> public static void SemaphoreMethod(object parameter) { while ((int)parameter != number) { Thread.Sleep(100); } //信號量計數減1 semaphore.WaitOne(); Console.WriteLine("The current value of number is:{0}", ++number); } }
運行結果如下:

與上一篇AutoResetEvent類似,信號量也可以實作跨行程間的執行緒同步,通過呼叫public Semaphore(int initialCount,int maximumCount,string name);
建構式,傳入一個信號量名來實作此功能,
下面代碼演示跨行程間的執行緒同步:
第一個行程代碼:
class Program { //共享資源 public static int number = 0; //初始信號量計數為0,最大計數為10, public static Semaphore semaphore1 = new Semaphore(0, 10, "Semaphore1"); public static Semaphore semaphore2 = new Semaphore(0, 10, "Semaphore2"); static void Main(string[] args) { #region 執行緒同步:使用信號量實作跨行程之間的執行緒同步 for (int i = 0; i < 10; i++) { Thread thread = new Thread(new ParameterizedThreadStart(Semaphore1Method)); thread.Start(i); } //為了有時間去啟動另外一個行程 Thread.Sleep(15000); //每次增加2個信號量,即每次釋放2個執行緒, for (int j = 0; j < 5; j++) { Console.WriteLine("信號燈1紅燈轉綠燈……"); semaphore1.Release(2); Console.WriteLine("信號燈2紅燈轉綠燈……"); semaphore2.Release(2); Thread.Sleep(1000); } Console.Read(); #endregion } /// <summary> /// Semaphore1方法 /// </summary> public static void Semaphore1Method(object parameter) { while ((int)parameter != number) { Thread.Sleep(100); } //信號量計數減1 semaphore1.WaitOne(); Console.WriteLine("Semaphore1:The current value of number is:{0}", ++number); } }
第二個行程代碼:
class Program { //共享資源 public static int number = 0; //創建物件 public static Semaphore semaphore2 = new Semaphore(0, 10, "Semaphore2"); static void Main(string[] args) { #region 通過信號量實作跨行程間的執行緒同步 for (int i = 0; i < 10; i++) { Thread thread = new Thread(new ParameterizedThreadStart(Semaphore2Method)); thread.Start(i); } Console.Read(); #endregion } /// <summary> /// Semaphore2方法 /// </summary> public static void Semaphore2Method(object parameter) { while ((int)parameter != number) { Thread.Sleep(100); } //信號量計數減1 semaphore2.WaitOne(); Console.WriteLine("Semaphore2:The current value of number is:{0}", ++number); } }
運行結果如下:

從結果可以看出,第一個行程的semaphore2.Release(2);信號發出后,第二個行程可以收到并釋放執行緒,
二、互斥體(Mutex)
Mutex物件是一個同步基元,當某一個執行緒占用Mutex物件時,其他也需要占用Mutex的執行緒將處于掛起狀態,
下面代碼演示互斥體同步的使用:
class Program { //共享資源 public static int number = 0; //互斥體 public static Mutex mutex = new Mutex(); static void Main(string[] args) { #region 執行緒同步:使用互斥體實作同步 for (int i = 0; i < 10; i++) { Thread thread = new Thread(MutexMethod); thread.Start(); } Console.Read(); #endregion } /// <summary> /// Mutex方法 /// </summary> public static void MutexMethod(object parameter) { mutex.WaitOne(); Thread.Sleep(500); Console.WriteLine("The current value of number is:{0}", ++number); mutex.ReleaseMutex(); } }
運行結果如下:

下面代碼演示跨行程間的執行緒同步:
第一個行程代碼:
class Program { //共享資源 public static int number = 0; //互斥體 public static Mutex mutex1 = new Mutex(false, "Mutex1"); public static Mutex mutex2 = new Mutex(false, "Mutex2"); static void Main(string[] args) { #region 執行緒同步:使用互斥體實作跨行程之間的執行緒同步 mutex1.WaitOne(); mutex2.WaitOne(); for (int i = 0; i < 10; i++) { Thread thread = new Thread(new ParameterizedThreadStart(Mutex1Method)); thread.Start(i); } //為了有時間去啟動另外一個行程 Thread.Sleep(15000); mutex1.ReleaseMutex(); mutex2.ReleaseMutex(); Console.Read(); #endregion } /// <summary> /// Mutex1方法 /// </summary> public static void Mutex1Method(object parameter) { mutex1.WaitOne(); Thread.Sleep(500); Console.WriteLine("Mutex1:The current value of number is:{0}", ++number); mutex1.ReleaseMutex(); } }
第二個行程代碼:
class Program { //共享資源 public static int number = 0; //創建物件 public static Mutex mutex2 = new Mutex(false, "Mutex2"); static void Main(string[] args) { #region 通過互斥體實作跨行程之間的執行緒同步 for (int i = 0; i < 10; i++) { Thread thread = new Thread(new ParameterizedThreadStart(Mutex2Method)); thread.Start(i); } Console.Read(); #endregion } /// <summary> /// Mutex2方法 /// </summary> public static void Mutex2Method(object parameter) { mutex2.WaitOne(); Thread.Sleep(500); Console.WriteLine("Mutex2:The current value of number is:{0}", ++number); mutex2.ReleaseMutex(); } }
運行結果如下:

從結果可以看出,第一個行程的mutex2.ReleaseMutex();信號發出后,第二個行程可以收到并釋放執行緒,
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/91514.html
標籤:C#
