Beispiel #1
0
        //Demonstration that this queue is SPSC
        public void SPSCConcurrencyTest()
        {
            var q     = UnsafeMPMCQueue.Allocate <ComplexType>(16);
            int count = 10000;


            Thread reader = new Thread(() =>
            {
                for (int i = 0; i < count;)
                {
                    if (UnsafeMPMCQueue.TryDequeue(q, out ComplexType num))
                    {
                        Assert.IsTrue(num.Equals(new ComplexType((ushort)i)));
                        i++;
                    }
                }
            });

            reader.Start();

            for (int i = 0; i < count;)
            {
                if (UnsafeMPMCQueue.TryEnqueue(q, new ComplexType((ushort)i)))
                {
                    i++;
                }
            }

            reader.Join();

            UnsafeMPMCQueue.Free(q);
        }
Beispiel #2
0
        private static void SplitQueue(UnsafeMPMCQueue *q)
        {
            //Wrap tail back to 0
            for (int i = 0; i < 5; i++)
            {
                UnsafeMPMCQueue.TryEnqueue(q, 111);
            }

            //First half
            for (int i = 0; i < 5; i++)
            {
                UnsafeMPMCQueue.TryEnqueue(q, i);
            }

            //Move head by 5
            for (int i = 0; i < 5; i++)
            {
                UnsafeMPMCQueue.TryDequeue <int>(q, out int num);
            }

            //Second half (head and tail are now both 5)
            for (int i = 5; i < 10; i++)
            {
                UnsafeMPMCQueue.TryEnqueue(q, i);
            }

            //Circular buffer now "ends" in the middle of the underlying array
        }
Beispiel #3
0
        public void TryActionTest()
        {
            var q = UnsafeMPMCQueue.Allocate <int>(16);

            //Inserts 10 items.
            SplitQueue(q);

            //Insert 6 more to fill the queue
            for (int i = 0; i < 6; i++)
            {
                UnsafeMPMCQueue.TryEnqueue(q, 999);
            }

            Assert.IsTrue(UnsafeMPMCQueue.TryPeek(q, out int result));
            Assert.AreEqual(0, result);

            for (int i = 0; i < 10; i++)
            {
                Assert.IsTrue(UnsafeMPMCQueue.TryDequeue(q, out int val));
                Assert.AreEqual(i, val);
            }

            //Empty 6 last items
            for (int i = 0; i < 6; i++)
            {
                Assert.IsTrue(UnsafeMPMCQueue.TryDequeue(q, out int val));
            }

            //Empty queue
            Assert.IsFalse(UnsafeMPMCQueue.TryPeek(q, out int res));

            UnsafeMPMCQueue.Free(q);
        }
Beispiel #4
0
        public void DequeueTest()
        {
            var q = UnsafeMPMCQueue.Allocate <int>(10);

            for (int i = 0; i < 10; i++)
            {
                UnsafeMPMCQueue.TryEnqueue(q, i * i);
            }


            for (int i = 0; i < 10; i++)
            {
                UnsafeMPMCQueue.TryDequeue(q, out int num);
                Assert.AreEqual(i * i, num);
            }

            UnsafeMPMCQueue.Free(q);
        }
 public bool TryDequeue(out T result)
 {
     return(UnsafeMPMCQueue.TryDequeue <T>(m_inner, out result));
 }