C# 多線程系列之Mutex使用

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   使用   io   ar   for   

互斥量是一個核心對象,它用來確保一個線程獨佔一個資源的訪問,並且互斥量可以用於不同進程中的線程互斥訪問資源。

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

Msdn地址:http://msdn.microsoft.com/en-us/library/system.threading.mutex(v=vs.110).aspx

舉MSDN上的例子說明:

using System;using System.Threading;class Example{    // Create a new Mutex. The creating thread does not own the mutex.     private static Mutex mut = new Mutex();    private const int numIterations = 1;    private const int numThreads = 3;    static void Main()    {        // Create the threads that will use the protected resource.         for(int i = 0; i < numThreads; i++)        {            Thread newThread = new Thread(new ThreadStart(ThreadProc));            newThread.Name = String.Format("Thread{0}", i + 1);            newThread.Start();        }        // The main thread exits, but the application continues to         // run until all foreground threads have exited.    }    private static void ThreadProc()    {        for(int i = 0; i < numIterations; i++)        {            UseResource();        }    }    // This method represents a resource that must be synchronized     // so that only one thread at a time can enter.     private static void UseResource()    {        // Wait until it is safe to enter, and do not enter if the request times out.        Console.WriteLine("{0} is requesting the mutex", Thread.CurrentThread.Name);        if (mut.WaitOne(1000)) {           Console.WriteLine("{0} has entered the protected area",                Thread.CurrentThread.Name);           // Place code to access non-reentrant resources here.            // Simulate some work.           Thread.Sleep(5000);           Console.WriteLine("{0} is leaving the protected area",                Thread.CurrentThread.Name);           // Release the Mutex.              mut.ReleaseMutex();           Console.WriteLine("{0} has released the mutex",                              Thread.CurrentThread.Name);        }        else {           Console.WriteLine("{0} will not acquire the mutex",                              Thread.CurrentThread.Name);        }    }}// The example displays output like the following: //       Thread1 is requesting the mutex //       Thread1 has entered the protected area //       Thread2 is requesting the mutex //       Thread3 is requesting the mutex //       Thread2 will not acquire the mutex //       Thread3 will not acquire the mutex //       Thread1 is leaving the protected area //       Thread1 has released the mutex

當一個線程佔有Mutex後,代碼就可以這樣寫:

 mutex.WaitOne(); mutex.WaitOne(); mutex.WaitOne(); mutex.WaitOne();。。。。

WaitOne這個方法被調用時 系統檢查發現mutex沒有被任何線程使用 故而將mutex分配給當前線程
因而就算繼續調用WaitOne也沒有影響 因為系統發現當前線程已經佔有 就直接返回了。換言之,waitOne是擷取
Mutex鎖的方式。如果調用ReleaseMutex那麼當前線程退出Mutex鎖,其它線程便可進來申請。但是如果調用兩次
ReleaseMutex 那麼由於當前線程實際上是不佔有鎖的 那麼會拋出異常。

所以不能這樣寫:

mutex.ReleaseMutex();//如果已經佔有 那麼OKmutex.ReleaseMutex();//調用第二次直接異常

Mutex是一個核心對象,所以,它是可以用作跨進程線程同步的。

A進程可以如此寫:

Mutex mutex = new Mutex(true,"mutex1");

B進程則可以如此寫:

Mutex mutex = Mutex.OpenExisting("mutex1")
OpenExisting這個方法簽名我們後面也可以經常看到,作用就是擷取一個已經存在的核心對象。
擷取到之後的線程同步代碼是跟單進程是一致的。

完畢。

 

C# 多線程系列之Mutex使用

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.