Пример #1
0
 public void Run()
 {
     for (int i = 0; i < _count; i++)
     {
         object o = new object();
         Items[o] = o;
         _q.Enqueue(new object[] { this, o });
     }
 }
Пример #2
0
        /** Safe for multiple threads to call simulataneously
         */
        public int Enqueue(T a)
        {
            _lfq.Enqueue(a);
            int count = Interlocked.Increment(ref _count);

            if (count == 1)
            {
                //We just transitioned from 0->1
                _are.Set();
            }
            return(count);
        }
Пример #3
0
            public void Run()
            {
                object val;
                bool   suc;
                bool   stop = false;

                do
                {
                    val = _q.TryDequeue(out suc);
                    if (suc)
                    {
                        Reads++;
                        stop = (val == _stopval);
                    }
                } while(false == stop);
                _q.Enqueue(_stopval);
            }
Пример #4
0
        public void SimpleQueueTest()
        {
            LockFreeQueue <object> lfqo = new LockFreeQueue <object>();
            LockFreeQueue <int>    lfqi = new LockFreeQueue <int>();
            Queue  q = new Queue();
            Random r = _rand;
            //Put in a bunch of random numbers:
            int max = r.Next(2048, 4096);

            for (int i = 0; i < max; i++)
            {
                int k = r.Next();
                lfqo.Enqueue(k);
                lfqi.Enqueue(k);
                q.Enqueue(k);
            }
            //Now verify that everything is okay:
            bool success;
            int  kq;
            int  klfo;
            int  klfi;

            for (int i = 0; i < max; i++)
            {
                klfo = (int)lfqo.TryDequeue(out success);
                Assert.IsTrue(success, "Dequeue<o> success");

                klfi = lfqi.TryDequeue(out success);
                Assert.IsTrue(success, "Dequeue<i> success");

                kq = (int)q.Dequeue();
                Assert.AreEqual(kq, klfo, "LockFreeQueue<object> == Queue");
                Assert.AreEqual(kq, klfi, "LockFreeQueue<int> == Queue");
            }
            try {
                lfqi.Dequeue();
                Assert.IsTrue(false, "LockFreeQueue<int> post-Dequeue exception");
            }
            catch (InvalidOperationException) { Assert.IsTrue(true, "LockFreeQueue<int> exception"); }
            try {
                lfqo.Dequeue();
                Assert.IsTrue(false, "LockFreeQueue<object> post-Dequeue exception");
            }
            catch (InvalidOperationException) { Assert.IsTrue(true, "LockFreeQueue<int> exception"); }
        }
Пример #5
0
            public void Run()
            {
                bool   success;
                object next;

                do
                {
                    next = _q.TryDequeue(out success);
                    if (success)
                    {
                        if (next != _stop)
                        {
                            Items.Add((object[])next);
                        }
                    }
                } while(next != _stop);
                //Put stop back in:
                _q.Enqueue(_stop);
            }
Пример #6
0
        /*
         * This test can be used to make sure the GC can keep up
         * with the allocations in a LockFreeQueue
         */
        //[Test]
        public void LFQMemTest()
        {
            /*
             * We dump a huge amount of data into the queue and
             * make sure memory doesn't blow up
             */
            LockFreeQueue <object> queue = new LockFreeQueue <object>();
            SimpleReader           read  = new SimpleReader(null, queue);
            //We should try about 2^30 so make sure we would fill nearly fill the
            //memory if there was a leak
            int          runs  = 1 << 30;
            SimpleWriter write = new SimpleWriter(runs, new object(), queue);
            Thread       rt    = new Thread(read.Run);
            Thread       wt    = new Thread(write.Run);

            rt.Start();
            wt.Start();
            wt.Join();
            //Put in the stop value:
            queue.Enqueue(null);
            rt.Join();
            //We added one more item (null)
            Assert.AreEqual(write.Writes + 1, read.Reads, "Writes equals reads");
        }
Пример #7
0
        public void MultiTester(int WRITERS, int READERS)
        {
            int    MAX_WRITES        = 50000;
            object stop              = new object();
            LockFreeQueue <object> q = new LockFreeQueue <object>();

            List <Thread> rthreads   = new List <Thread>();
            List <Thread> wthreads   = new List <Thread>();
            List <Thread> allthreads = new List <Thread>();
            List <Writer> writers    = new List <Writer>();
            List <Reader> readers    = new List <Reader>();

            for (int i = 0; i < WRITERS; i++)
            {
                Writer w = new Writer(MAX_WRITES, q);
                writers.Add(w);
                Thread t = new Thread(w.Run);
                wthreads.Add(t);
                allthreads.Add(t);
            }
            for (int i = 0; i < READERS; i++)
            {
                Reader r = new Reader(stop, q);
                readers.Add(r);
                Thread t = new Thread(r.Run);
                rthreads.Add(t);
                allthreads.Add(t);
            }
            //Start them in a random order:
            Shuffle(allthreads);
            foreach (Thread t in allthreads)
            {
                t.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:
            foreach (Thread t in rthreads)
            {
                t.Join();
            }
            int read_items = 0;

            foreach (Reader r in readers)
            {
                foreach (object[] o in r.Items)
                {
                    read_items++;
                    Writer w    = (Writer)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 (Writer w in writers)
            {
                Assert.AreEqual(0, w.Items.Count, "Dequeued all items");
            }
        }