예제 #1
0
 public Consumer(SafeRing queue, Random rand, int timeout = -1)
 {
     this.queue   = queue;
     this.rand    = rand;
     thread       = new Thread(ThreadCon);
     this.timeout = timeout;
 }
예제 #2
0
        static void Main(string[] args)
        {
            // get a random number
            Random            rand              = new Random();
            SafeRing          ring              = new SafeRing(BUFFER_SIZE);
            List <Producer>   producers         = new List <Producer>();
            List <WaitHandle> producersComplete = new List <WaitHandle>();
            List <Consumer>   consumers         = new List <Consumer>();


            for (int i = 0; i < N_Producers; i++)
            {
                Producer p = new Producer(ring, PRODUCE_COUNT, rand, TIMEOUT);
                producers.Add(p);
                producersComplete.Add(p.Complete);
                p.Start();
            }

            for (int i = 0; i < N_Consumers; i++)
            {
                Consumer c = new Consumer(ring, rand, TIMEOUT);
                consumers.Add(c);
                c.Start();
            }

            // wait for all producers to complete and stop consumers
            WaitHandle.WaitAll(producersComplete.ToArray());
            foreach (Consumer c in consumers)
            {
                c.Stop();
            }
        }
예제 #3
0
 public Producer(SafeRing queue, int itemNum, Random rand, int timeout = -1)
 {
     this.queue    = queue;
     this.rand     = rand;
     this.numItems = itemNum;
     complete      = new ManualResetEvent(false);
     thread        = new Thread(ThreadProc);
     this.timeout  = timeout;
 }