日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

C#多线程学习(六) 互斥对象

發布時間:2023/12/10 C# 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C#多线程学习(六) 互斥对象 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
C#多線程學習(六) 互斥對象

                              原文鏈接:http://kb.cnblogs.com/page/42533/

本系列文章導航

C#多線程學習(一) 多線程的相關概念

C#多線程學習(二) 如何操縱一個線程

C#多線程學習(三) 生產者和消費者

C#多線程學習(四) 多線程的自動管理(線程池)

C#多線程學習(五) 多線程的自動管理(定時器)

C#多線程學習(六) 互斥對象

如何控制好多個線程相互之間的聯系,不產生沖突和重復,這需要用到互斥對象,即:System.Threading 命名空間中的 Mutex 類。

我們可以把Mutex看作一個出租車,乘客看作線程。乘客首先等車,然后上車,最后下車。當一個乘客在車上時,其他乘客就只有等他下車以后才可以上車。而線程與Mutex對象的關系也正是如此,線程使用Mutex.WaitOne()方法等待Mutex對象被釋放,如果它等待的Mutex對象被釋放了,它就自動擁有這個對象,直到它調用Mutex.ReleaseMutex()方法釋放這個對象,而在此期間,其他想要獲取這個Mutex對象的線程都只有等待。

下面這個例子使用了Mutex對象來同步四個線程,主線程等待四個線程的結束,而這四個線程的運行又是與兩個Mutex對象相關聯的。

其中還用到AutoResetEvent類的對象,可以把它理解為一個信號燈。這里用它的有信號狀態來表示一個線程的結束。

// AutoResetEvent.Set()方法設置它為有信號狀態

// AutoResetEvent.Reset()方法設置它為無信號狀態

Mutex 類的程序示例:

Code
using System;
using System.Threading;

namespace ThreadExample
{
publicclass MutexSample
{
  
static Mutex gM1;
  
static Mutex gM2;
  
constint ITERS =100;
  
static AutoResetEvent Event1 =new AutoResetEvent(false);
  
static AutoResetEvent Event2 =new AutoResetEvent(false);
  
static AutoResetEvent Event3 =new AutoResetEvent(false);
  
static AutoResetEvent Event4 =new AutoResetEvent(false);

  
publicstaticvoid Main(String[] args)
   {
Console.WriteLine(
"Mutex Sample ");
//創建一個Mutex對象,并且命名為MyMutex
gM1 =new Mutex(true,"MyMutex");
//創建一個未命名的Mutex 對象.
gM2 =new Mutex(true);
Console.WriteLine(
" - Main Owns gM1 and gM2");

AutoResetEvent[] evs
=new AutoResetEvent[4];
evs[
0] = Event1; //為后面的線程t1,t2,t3,t4定義AutoResetEvent對象
evs[1] = Event2;
evs[
2] = Event3;
evs[
3] = Event4;

MutexSample tm
=new MutexSample( );
Thread t1
=new Thread(new ThreadStart(tm.t1Start));
Thread t2
=new Thread(new ThreadStart(tm.t2Start));
Thread t3
=new Thread(new ThreadStart(tm.t3Start));
Thread t4
=new Thread(new ThreadStart(tm.t4Start));
t1.Start( );
// 使用Mutex.WaitAll()方法等待一個Mutex數組中的對象全部被釋放
t2.Start( );// 使用Mutex.WaitOne()方法等待gM1的釋放
t3.Start( );// 使用Mutex.WaitAny()方法等待一個Mutex數組中任意一個對象被釋放
t4.Start( );// 使用Mutex.WaitOne()方法等待gM2的釋放

Thread.Sleep(
2000);
Console.WriteLine(
" - Main releases gM1");
gM1.ReleaseMutex( );
//線程t2,t3結束條件滿足

Thread.Sleep(
1000);
Console.WriteLine(
" - Main releases gM2");
gM2.ReleaseMutex( );
//線程t1,t4結束條件滿足

//等待所有四個線程結束
WaitHandle.WaitAll(evs);
Console.WriteLine(
" Mutex Sample");
Console.ReadLine();
   }

  
publicvoid t1Start( )
   {
Console.WriteLine(
"t1Start started, Mutex.WaitAll(Mutex[])");
Mutex[] gMs
=new Mutex[2];
gMs[
0] = gM1;//創建一個Mutex數組作為Mutex.WaitAll()方法的參數
gMs[1] = gM2;
Mutex.WaitAll(gMs);
//等待gM1和gM2都被釋放
Thread.Sleep(2000);
Console.WriteLine(
"t1Start finished, Mutex.WaitAll(Mutex[]) satisfied");
Event1.Set( );
//線程結束,將Event1設置為有信號狀態
   }
  
publicvoid t2Start( )
   {
Console.WriteLine(
"t2Start started, gM1.WaitOne( )");
gM1.WaitOne( );
//等待gM1的釋放
Console.WriteLine("t2Start finished, gM1.WaitOne( ) satisfied");
Event2.Set( );
//線程結束,將Event2設置為有信號狀態
   }
  
publicvoid t3Start( )
   {
Console.WriteLine(
"t3Start started, Mutex.WaitAny(Mutex[])");
Mutex[] gMs
=new Mutex[2];
gMs[
0] = gM1;//創建一個Mutex數組作為Mutex.WaitAny()方法的參數
gMs[1] = gM2;
Mutex.WaitAny(gMs);
//等待數組中任意一個Mutex對象被釋放
Console.WriteLine("t3Start finished, Mutex.WaitAny(Mutex[])");
Event3.Set( );
//線程結束,將Event3設置為有信號狀態
   }
  
publicvoid t4Start( )
   {
Console.WriteLine(
"t4Start started, gM2.WaitOne( )");
gM2.WaitOne( );
//等待gM2被釋放
Console.WriteLine("t4Start finished, gM2.WaitOne( )");
Event4.Set( );
//線程結束,將Event4設置為有信號狀態
   }
}
}

程序的輸出結果:

結果
Mutex Sample
- Main Owns gM1 and gM2
t1Start started, Mutex.WaitAll(Mutex[])
t2Start started, gM1.WaitOne( )
t3Start started, Mutex.WaitAny(Mutex[])
t4Start started, gM2.WaitOne( )
- Main releases gM1
t2Start finished, gM1.WaitOne( ) satisfied
t3Start finished, Mutex.WaitAny(Mutex[])
- Main releases gM2
t1Start finished, Mutex.WaitAll(Mutex[]) satisfied
t4Start finished, gM2.WaitOne( )
Mutex Sample

?

從執行結果可以很清楚地看到,線程t2,t3的運行是以gM1的釋放為條件的,而t4在gM2釋放后開始執行,t1則在gM1和gM2都被釋放了之后才執行。Main()函數最后,使用WaitHandle等待所有的AutoResetEvent對象的信號,這些對象的信號代表相應線程的結束。

posted on 2012-05-06 19:01 Hao_Guo 閱讀(...) 評論(...) 編輯 收藏

轉載于:https://www.cnblogs.com/HaoGuo/archive/2012/05/06/Thread6.html

總結

以上是生活随笔為你收集整理的C#多线程学习(六) 互斥对象的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。