示例#1
0
 // 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;
 }
示例#2
0
        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();
        }
示例#3
0
 public void RaiseCatfight(Cat currentOwner, Cat otherCat, Fork fork)
 {
     CatFightDetected(currentOwner, otherCat, fork);
 }