Exemplo n.º 1
0
        public override void Run()
        {
            try
            {
                // we use a Choice to listen 
                // on the Channel.
                Choice choice = new Choice(customers);

                switch (choice.TryFairSelect())
                {
                    case 0:
                        // We got a seat => sit down
                        customers.Write(this);
                        break;
                    case -1:
                        // no seats => leave shop
                        LeaveShop();
                        break;
                }

            }
            catch (PoisonException)
            {
                // if the channel has been
                // poisoned the shop must be closed
                Console.WriteLine(name + ": Too late! Shop is closed.");
            }
        }
Exemplo n.º 2
0
        public override void Run()
        {            
            // Here the desired number of taxi tasks are created and started.
            // Note that by using AsyncParallel the program can proceed
            // even though the taxi tasks are running. Had we used Parallel
            // the Run method would have blocked until Run returned.

            for (int i = 0; i < numTaxis; i++)
                new Taxi(taxiChan.ChannelReader, ackChannel.ChannelWriter).Start();

            // We create a choice that monitors two alternatived:
            // 1. A reader has read from the taxiChan - in this case meaning 
            // at least one vacant taxi
            // 2. A writer has written to the achChannel - meaning that
            // a taxi has picked up a customer.
            Choice choice = new Choice(taxiChan.ChannelWriter, ackChannel.ChannelReader);

            
            while (true)
            {
                // We make a fair selection over the two alternatives meaning 
                // that we cycle through the alternatives if they are ready. 
                switch(choice.FairSelect())
                {
                    case 0:
                        Tuple<int, int , string> t = customerChan.Read();
                        taxiChan.Write(t);
                        vacantTaxis--;
                        Console.WriteLine("Vacant taxis: " + vacantTaxis);
                        break;
                    case 1:
                        ackChannel.Read();
                        vacantTaxis++;
                        break;
                }
            }
        }
Exemplo n.º 3
0
 internal override AlternativeType Enable(Choice choice)
 {
     lock (lockObject)
     {
         if (!pending)
         {
             currentChoice = choice;
             return AlternativeType.False;
         }
         return AlternativeType.Timer;
     }
 }
Exemplo n.º 4
0
 internal override AlternativeType Enable(Choice choice)
 {
     return(channel.Enable(choice));
 }
Exemplo n.º 5
0
 internal abstract AlternativeType Enable(Choice choice);