多線程學習筆記4 互斥體

來源:互聯網
上載者:User

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

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

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

其中還用到AutoResetEvent類的對象,可以把它理解為一個號誌。這裡用它的有訊號狀態來表示一個線程的結束。
// AutoResetEvent.Set()方法設定它為有訊號狀態

// AutoResetEvent.Reset()方法設定它為無訊號狀態

代碼using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace ConsoleApplication1
{
    public class MyMutex
    {
        Mutex m1;
        Mutex m2;

        AutoResetEvent aut1 = new AutoResetEvent(false);
        AutoResetEvent aut2 = new AutoResetEvent(false);
        AutoResetEvent aut3 = new AutoResetEvent(false);
        AutoResetEvent aut4 = new AutoResetEvent(false);

        public MyMutex()
        {
            m1 = new Mutex(true,"mutex1");
            m2 = new Mutex(true,"mutex2");
        }

        public void start1()
        {
            Console.WriteLine("Thread1 start");
            m1.WaitOne();
            Console.WriteLine("Thread1 end");
            aut1.Set();
            m1.ReleaseMutex();
        }
        public void start2()
        {
            Console.WriteLine("Thread2 start");
            m2.WaitOne();
            Console.WriteLine("Thread2 end");
            aut2.Set();
            m2.ReleaseMutex();
        }
        public void start3()
        {
            Console.WriteLine("Thread3 start");
            Mutex[] list = new Mutex[2];
            list[0] = m1;
            list[1] = m2;
            WaitHandle.WaitAll(list,Timeout.Infinite,true);
            Console.WriteLine("Thread3 end");
            aut3.Set();
           foreach (Mutex m in list)
           {
               m.ReleaseMutex();
           }
        }

        public void start4()
        {
            Console.WriteLine("Thread4 start");
            Mutex[] list = new Mutex[2];
            list[0] = m1;
            list[1] = m2;
            int k = Mutex.WaitAny(list);
            Console.WriteLine("Thread4 end");
            list[k].ReleaseMutex();
        }

        public void Test()
        {
            Thread th1 = new Thread(new ThreadStart (start1));
            Thread th2 = new Thread(new ThreadStart (start2));
            Thread th3 = new Thread(new ThreadStart (start3));
            Thread th4 = new Thread(new ThreadStart (start4));

            th1.Start();
            th2.Start();
            th3.Start();
            th4.Start();

            AutoResetEvent[] autolist = new AutoResetEvent[4];
            autolist[0] = aut1;
            autolist[1] = aut2;
            autolist[2] = aut3;
            autolist[3] = aut4;

            Console.WriteLine("m2 ReleaseMutex");
            m2.ReleaseMutex();
            Console.WriteLine("m1 ReleaseMutex ");
            m1.ReleaseMutex();

            WaitHandle.WaitAll(autolist);
            Console.WriteLine("all Set");
            Console.ReadLine();
        }
    }
}

注意每次要釋放互斥體 不然會引發AbandonedMutexException 異常

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.