private Thread CreatePhilosopher(LockCounter eatCounter, object left, object right) { var t1 = new Thread(() => { while (eatCounter.Get() < MAX_EAT_COUNT) { lock (left) { var lockTaken = false; try { Monitor.TryEnter(right, 100, ref lockTaken); if (lockTaken) { Thread.Sleep(100); eatCounter.IncrementAndGet(); } } finally { if (lockTaken) { Monitor.Exit(right); } } } Thread.Sleep(100); } }); return(t1); }
private static Thread StartPhilosopher(LockCounter eatCounter, TMVar <bool> left, TMVar <bool> right) { var t1 = new Thread(() => { while (eatCounter.Get() < MAX_EAT_COUNT) { STMSystem.Atomic(() => { if (!left || !right) { STMSystem.Retry(); } left.Value = false; right.Value = false; }); Console.WriteLine("Thread: " + Thread.CurrentThread.ManagedThreadId + " eating."); Thread.Sleep(100); Console.WriteLine("Eat count: " + eatCounter.IncrementAndGet()); STMSystem.Atomic(() => { left.Value = true; right.Value = true; }); Thread.Sleep(100); } }); t1.Start(); return(t1); }