// TODO: Step 2: add semaphore public Cat(string name, Fork left, Fork right, SemaphoreSlim bowlAvailable) { //this.bowlAvailable = bowlAvailable; Name = name; CurrentState = State.Sleeping; firstFork = (left.Id < right.Id) ? left : right; secondFork = (left.Id < right.Id) ? right : left; }
public DiningRoom() { int count = Names.Length; Fork[] forks = new Fork[count]; for (int n = 0; n < count; n++) { forks[n] = new Fork(n, this); } // TODO: Step 1 - add semaphore, pass to Cat. SemaphoreSlim bowlAvailable = new SemaphoreSlim(count / 2); Cats = Enumerable.Range(0, count) .Select(n => new Cat(Names[n], forks[n], forks[(n + 1) % count], bowlAvailable)) .ToList(); }
public void RaiseCatfight(Cat currentOwner, Cat otherCat, Fork fork) { CatFightDetected(currentOwner, otherCat, fork); }