2 回目にミューテックスをロックしたときにフローがロックされることを期待して、以下のコードを実行しました。2回実行した後、停止せずに何度もロックできることに気付きました(同じスレッドを想定)。この動作を変更するにはどうすればよいですか?
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace Test {
class Program {
static volatile Mutex mut1 = new Mutex();
static volatile Mutex mut2 = new Mutex();
static void Main(string[] args) {
mut1.WaitOne(); Console.WriteLine("1W");
mut2.WaitOne(); Console.WriteLine("2W");
Thread oThread = new Thread(new ThreadStart(fn2));
oThread.Start();
mut1.WaitOne(); Console.WriteLine("1W");
Console.WriteLine("Second");
mut1.ReleaseMutex(); Console.WriteLine("1R");
}
static void fn2() {
Console.WriteLine("First");
mut2.ReleaseMutex(); Console.WriteLine("2R");
mut1.ReleaseMutex(); Console.WriteLine("1R");
}
}
}