ThreadAとThreadBの両方が、同じAutoResetEventでこの順にWaitOne()
を呼び出すとします。イベントが設定されると、なぜThreadAの代わりにThreadBが解放されるのですか?2番目のスレッドは、WaitOne()を呼び出してAutoResetEventによって解放されたときに、最初のスレッドが最初のスレッドの前に解放されるのはなぜですか?
私はあなたがみとめスレッドが待機している上AutoResetEventを設定するときに何が起こるかを調べるためにテストを実行しました:
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();
}
コードはもちろん有用ではないのです。それは単なるミッキーマウスの例です。そして、これは重要ではない/緊急です。しかし、私はとにかくもっと知りたいと思うでしょう...
autoEvent1.Set()を呼び出してみます。もう一方のスレッドが解放されるかどうかを再度確認します。私はスイッチを2回回す必要があると思います。 –
こんにちは - 私はこれを直後に試しました - 私はSet()への2番目の呼び出しを続行するために2番目のスレッドを得ることができます。 Thread.Sleep()を呼び出して –
私は現在、解放されたスレッドがランダムであることを誰かが実際に知っているかどうかを調べることにもっと興味があります。ドキュメンテーションによると、スレッドは何らかの並べ替えを示すためにキューに入れられています(最初のスレッドが最初にリリースされる予定だった理由です)。 –