当第二个线程都调用WaitOne()并由AutoResetEvent释放时,为什么第二个线程却在第一个线程之前释放? [英] Why does the second thread get released before the first one, when they both called WaitOne() and were released by an AutoResetEvent?
问题描述
假设ThreadA和ThreadB都在同一AutoResetEvent上以此顺序调用WaitOne()
.设置事件后,为什么要释放ThreadB而不是ThreadA?
Suppose that ThreadA and ThreadB both call WaitOne()
in that order on the same AutoResetEvent. When the event is set, why does ThreadB get released instead of ThreadA?
我进行了一项测试,以了解设置多个线程正在等待的AutoResetEvent时会发生什么:
I ran a test to find out what happens when you set an AutoResetEvent on which mutiple threads are waiting:
private static void Test()
{
// two threads - waiting for the same autoreset event
// start it unset i.e. closed i.e. anything calling WaitOne() will block
AutoResetEvent autoEvent = new AutoResetEvent(false);
Thread thread1 = new Thread(new ThreadStart(WriteSomeMessageToTheConsole));
thread1.Start(); // this will now block until we set the event
Thread thread2 = new Thread(new ThreadStart(WriteSomeOtherMessageToTheConsole));
thread2.Start(); // this will now also block until we set the event
// simulate some other stuff
Console.WriteLine("Doing stuff...");
Thread.Sleep(5000);
Console.WriteLine("Stuff done.");
// set the event - I thought this would mean both waiting threads are allowed to continue
// BUT thread2 runs and thread1 stays blocked indefinitely
// So I guess I was wrong and that Set only releases one thread in WaitOne()?
// And why thread2 first?
autoEvent1.Set();
}
代码当然是没有用的;这只是一个米老鼠的例子.这并不重要/紧急.但是我还是想知道更多...
The code is of course not useful; it's just a mickey mouse example. And this is not important/urgent. But I'd be interested to know more anyway...
推荐答案
IIRC(由自动重置事件释放的线程)未指定.正如其他所有人所提到的,如果要广播条件,则需要手动重置事件.如果您想释放一个确切的数字(比如说n中的3个),那么您可能想要使用一个信号量.
IIRC, which thread is released by an auto-reset event is unspecified. As everyone else mentioned, you want a manual reset event if you want to broadcast a condition. If you want to release an exact number (say exactly 3 of n), then you probably want to use a semaphore.
如果您真的想弄清楚为什么顺序可能与您期望的不同,请查看"Windows Internals"或Mark Russinovich编写的任何内容.很有可能他解释了某处高管资源的等待顺序.
If you really want to dig into why the order might be different than you would expect, take a look at "Windows Internals" or anything that Mark Russinovich has written. Chances are that he explains the wait order on executive resources somewhere.
这篇关于当第二个线程都调用WaitOne()并由AutoResetEvent释放时,为什么第二个线程却在第一个线程之前释放?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!