Esempio n. 1
0
        public Producer(SafeRing buffer, int numItemsToProduce, Random rand, int numRetries = 5, int backoffTime = 1000, int timeout = -1)
        {
            this.buffer            = buffer;
            this.numItemsToProduce = numItemsToProduce;
            this.rand        = rand;
            this.timeout     = timeout;
            this.numRetries  = numRetries;
            this.backoffTime = backoffTime;

            // Set to not complete when initialized.
            completeEvent = new ManualResetEvent(false);
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            // Instantiates a SafeRing queue with capacity of qCapacity.
            SafeRing buffer = new SafeRing(qCapacity);

            // Instantiates and starts nProducers producers, each to produce nItems items
            List <Producer> producers = new List <Producer>();

            WaitHandle[] completeEvents = new WaitHandle[nProducers];
            Random       rand           = new Random();

            for (int i = 0; i < nProducers; i++)
            {
                Producer p = new Producer(buffer, nItems, rand, numRetries, backoffTime, timeout);
                completeEvents[i] = p.Complete;
                p.Start();
                producers.Add(p);
            }

            // Instantiates and starts nConsumers consumers
            List <Consumer> consumers = new List <Consumer>();

            for (int i = 0; i < nConsumers; i++)
            {
                Consumer c = new Consumer(buffer, rand, timeout);
                c.Start();
                consumers.Add(c);
            }


            // Wait for all producers to complete then signal consumers to stop
            WaitHandle.WaitAll(completeEvents);
            buffer.WaitUntilEmpty();
            consumers.ForEach(c => { c.Stop(); });

            Console.WriteLine("Program is Done!");
            Console.ReadLine();
        }
Esempio n. 3
0
 public Consumer(SafeRing buffer, Random rand, int timeout = -1)
 {
     this.buffer  = buffer;
     this.rand    = rand;
     this.timeout = timeout;
 }