Ejemplo n.º 1
0
        public void MultiBQTester(int WRITERS)
        {
            int    MAX_WRITES          = 50000;
            object stop                = new object();
            LFBlockingQueue <object> q = new LFBlockingQueue <object>();

            List <Thread>   wthreads = new List <Thread>();
            List <BQWriter> writers  = new List <BQWriter>();

            for (int i = 0; i < WRITERS; i++)
            {
                BQWriter w = new BQWriter(MAX_WRITES, q);
                writers.Add(w);
                Thread t = new Thread(w.Run);
                wthreads.Add(t);
            }
            foreach (Thread t in wthreads)
            {
                t.Start();
            }
            BQReader r           = new BQReader(stop, q);
            Thread   read_thread = new Thread(r.Run);

            read_thread.Start();
            //Now, let's make sure all the writers are done:
            foreach (Thread t in wthreads)
            {
                t.Join();
            }
            //Now put the stop object in:
            q.Enqueue(stop);
            //That should stop all the reader threads:
            read_thread.Join();
            int read_items = 0;

            foreach (object[] o in r.Items)
            {
                read_items++;
                BQWriter w    = (BQWriter)o[0];
                object   data = o[1];
                Assert.AreEqual(w.Items[data], data, "matching data");
                w.Items.Remove(data);
            }
            Assert.AreEqual(read_items, MAX_WRITES * WRITERS, "All writes read");
            foreach (BQWriter w in writers)
            {
                Assert.AreEqual(0, w.Items.Count, "Dequeued all items");
            }
            bool timedout;
            //Take out the stop object:
            object s = q.Dequeue(500, out timedout);

            Assert.IsFalse(timedout, "stop containing queue timed out");
            Assert.AreEqual(s, stop, "Stop removed");
            q.Dequeue(500, out timedout);
            Assert.IsTrue(timedout, "Empty queue timed out");
        }
Ejemplo n.º 2
0
 public BQReader(object stop, LFBlockingQueue <object> q)
 {
     _q    = q;
     Items = new List <object[]>();
     _stop = stop;
 }
Ejemplo n.º 3
0
 public BQWriter(int max, LFBlockingQueue <object> q)
 {
     _q     = q;
     _count = max;
     Items  = new Dictionary <object, object>();
 }