Why does MSDN sample from Threading Tutorial crash?

懵懂的女人 提交于 2019-12-01 05:49:52

After waiting on a Mutex you have to release it, using

Mutex.ReleaseMutex()

before the threads exits.

fixed t1start - t4start

public void t1Start()
{
    Console.WriteLine("t1Start started,  Mutex.WaitAll(Mutex[])");
    Mutex[] gMs = new Mutex[2];
    gMs[0] = gM1;  // Create and load an array of Mutex for WaitAll call 
    gMs[1] = gM2;
    Mutex.WaitAll(gMs);  // Waits until both gM1 and gM2 are released  
    Thread.Sleep(2000);
    Console.WriteLine("t1Start finished, Mutex.WaitAll(Mutex[]) satisfied");
    Event1.Set();      // AutoResetEvent.Set() flagging method is done
    gM1.ReleaseMutex();
    gM2.ReleaseMutex();
}
public void t2Start()
{
    Console.WriteLine("t2Start started,  gM1.WaitOne( )");
    gM1.WaitOne();    // Waits until Mutex gM1 is released ---errors is here---    
    Console.WriteLine("t2Start finished, gM1.WaitOne( ) satisfied");

    gM1.ReleaseMutex();
    Event2.Set();     // AutoResetEvent.Set() flagging method is done

}
public void t3Start()
{
    Console.WriteLine("t3Start started,  Mutex.WaitAny(Mutex[])");
    Mutex[] gMs = new Mutex[2];
    gMs[0] = gM1;  // Create and load an array of Mutex for WaitAny call  
    gMs[1] = gM2;

    int result = Mutex.WaitAny(gMs);  // Waits until either Mutex is released  
    gMs[result].ReleaseMutex();
    Console.WriteLine("t3Start finished, Mutex.WaitAny(Mutex[])"); Event3.Set();       // AutoResetEvent.Set() flagging method is done
}
public void t4Start()
{
    Console.WriteLine("t4Start started,  gM2.WaitOne( )");
    gM2.WaitOne();   // Waits until Mutex gM2 is released   
    Console.WriteLine("t4Start finished, gM2.WaitOne( )");
    Event4.Set();    // AutoResetEvent.Set() flagging method is done
    gM2.ReleaseMutex();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!