这两天看多线程的时候发现的一些问题【修改Mutex多线程同步的问题】

//发现其实网上百度文库那个版本主要是每个线程获得资源过后没有释放资源,因此后面的线程不能正常的获取资源。

#region Mutex实现多线程同步问题

/*

参考内容如下:

1:http://wenku.baidu.com/link?url=6YwFlRXmjFebUINlq2A71Gs__RMEq67MxK_mxtDOikjI4YdoMUVVjVsaVPwysJldoQy4qe8Fc-N-u9rxU8EXLaOLVqkbDxjbHITop4h3SkK

2:http://blog.csdn.net/heshengfen123/article/details/8212627

*/

public class MutexSample

    {
        static Mutex gM1;
        static Mutex gM2;
        const int 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);
        public static void Main(string[] args)
        {
            Console.WriteLine("Mutex sample....");
            //创建一个Mutex对象
            gM1=new Mutex(true,"MyMutex");
            gM2 = new Mutex(true);
            AutoResetEvent[] evs = new AutoResetEvent[4];
            evs[0] = Event1;
            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();
            t2.Start();
            t3.Start();
            t4.Start();
            Thread.Sleep(2000);
            Console.WriteLine("-Main release gM1");
            gM1.ReleaseMutex();//释放线程
            Thread.Sleep(1000);
            Console.WriteLine("-Main release gM2");
            gM2.ReleaseMutex();//
            WaitHandle.WaitAll(evs);//等待4个线程结束
            Console.WriteLine("...Mutex Sample end");
            Console.ReadLine();
        }
        public void t1Start()
        {
            Console.WriteLine("t1Start started ,Mutex.WaitAll(Mutex[])");
            Mutex[] gMs = new Mutex[2];
            gMs[0] = gM1;
            gMs[1] = gM2;
            Mutex.WaitAll(gMs);//等待gM1和gM2都释放
            Thread.Sleep(2000);
            Console.WriteLine("t1Start finished,Mutex.WaitAll(Mutex[]) saticfied");
            for (int i = 0; i < gMs.Length; i++)//释放资源
            {

                gMs[i].ReleaseMutex();

            }
            Event1.Set();//线程结束
           
        }
        public void t2Start()
        {
            Console.WriteLine("t2Start started,gM1.WaitOne()");
            gM1.WaitOne();//等待gM1的释放
            Console.WriteLine("t2Start finished,gM1.WaitOne() satified");
            gM1.ReleaseMutex();//释放资源
            Event2.Set();//结束线程

        }
        public void t3Start()
        {
            Console.WriteLine("t3Start started,Mutex.WaitAny(Mutex[]");
            Mutex[] gMs = new Mutex[2];
            gMs[0] = gM1;
            gMs[1] = gM2;
            int index=Mutex.WaitAny(gMs);//等待任意一个释放
           //等待数组中任意一个Mutex对象被释放

            Console.WriteLine("t3Start finished,Mutex.WaitAny(Mutex[])");

            gMs[index].ReleaseMutex();//释放资源
            Event3.Set();//结束线程

        }
        public void t4Start()
        {
            Console.WriteLine("t4Start started,gM2.WaitOne()");
            gM2.WaitOne();//等待gM2被释放
            Console.WriteLine("t4Start finished,gM2.WaitOne()");
            gM2.ReleaseMutex();//释放资源
            Event4.Set();//线程结束
           
        }

    }


#endregion

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值