Exemple #1
0
        static void Main(string[] args)
        {
            const int ringSize          = 4;
            const int itemsToProduce    = 1;
            const int numOfProducers    = 5;
            const int numOfConsumers    = 5;
            var       rand              = new Random();
            var       ring              = new SafeRing(ringSize);
            var       producerList      = new List <Producer>();
            var       consumerList      = new List <Consumer>();
            var       completeEventList = new List <WaitHandle>();

            for (int i = 0; i < numOfProducers; i++)
            {
                var p = new Producer(ring, itemsToProduce, rand);
                producerList.Add(p);
                p.Start();
                completeEventList.Add(p.Complete);
            }

            for (int i = 0; i < numOfConsumers; i++)
            {
                var c = new Consumer(ring, rand);
                consumerList.Add(c);
                c.Start();
            }

            WaitHandle.WaitAll(completeEventList.ToArray());
            consumerList.ForEach(c => c.Stop());
        }
Exemple #2
0
 public Producer(SafeRing ring, int numToProduce, Random random)
 {
     _numToProduce = numToProduce;
     _ring         = ring;
     _rand         = random;
 }
Exemple #3
0
 public Consumer(SafeRing ring, Random random)
 {
     _ring = ring;
     _rand = random;
 }