私は質問を理解しようとしています(そして、それぞれに対する答え):
以下のコードを添付して
コード内のコメントは次のことを示しています。
// set the event - I thought this would mean both waiting threads are allowed to continue
// BUT thread2 runs and thread1 stays blocked indefinitely
しかし、スレッドのいずれかが無期限にブロックされる方法と理由がわかりません...
コードを実行しようとしましたが、無期限のブロックを監視しませんでした...
この質問(およびそれぞれの回答)で何が欠けていますか?
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();
}
更新:
このコードを次のように起動していました:
using System;
using System.Threading;
namespace bothCallWaitOne
{
class Program
{
static void Main(string[] args)
{
// 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);
WriteSomeMessageToTheConsole();
Thread thread1 = new Thread(new ThreadStart(WriteSomeMessageToTheConsole));
thread1.Name = "1111111111";
thread1.Start(); // this will now block until we set the event
//Thread thread2 = new Thread(new ThreadStart(WriteSomeOtherMessageToTheConsole));
Thread thread2 = new Thread(new ThreadStart(WriteSomeOtherMessageToTheConsole));
thread2.Name = "222222222222";
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?
autoEvent.Set();
Console.ReadLine();
}
static void WriteSomeMessageToTheConsole()
{
Console.WriteLine(Thread.CurrentThread.Name);
}
static void WriteSomeOtherMessageToTheConsole()
{
Console.WriteLine(Thread.CurrentThread.Name);
}
}
}
また、無期限のブロックは見られません。
コードを実行した結果の出力は次のとおりです。
Doing stuff...
222222222222
1111111111
Stuff done.
(スレッドが無期限にブロックされるという) 議論されたトピックを再現するために、コードを実行 (更新、変更、インクリメント) するにはどうすればよいですか?