public static void reader() { while (true) { Console.WriteLine("reader waiting"); RMutex.WaitOne(); RCMutex.WaitOne(); Rcount++; if (Rcount == 1) { WMutex.WaitOne(); } RCMutex.Release(); Console.WriteLine("read-" + Rcount); Thread.Sleep(1000); RCMutex.WaitOne(); Rcount--; if (Rcount == 0) { WMutex.Release(); } RCMutex.Release(); RMutex.Release(); } }
public static void writer() { while (true) { Console.WriteLine("Writer waiting"); RMutex.WaitOne(); WMutex.WaitOne(); Console.WriteLine("write"); Thread.Sleep(1000); WMutex.Release(); RMutex.Release(); } }