public void Enqueue_Dequeue_IsEmpty()
        {
            var spscq = new SingleProducerSingleConsumerQueue <int>();

            Assert.True(spscq.IsEmpty);

            for (int i = 0; i < 10; i++)
            {
                spscq.Enqueue(i);
                Assert.False(spscq.IsEmpty);

                int j;
                Assert.True(spscq.TryDequeue(out j));
                Assert.Equal(i, j);
                Assert.True(spscq.IsEmpty);
            }

            const int Count = 100000;

            for (int i = 0; i < Count; i++)
            {
                spscq.Enqueue(i);
                Assert.False(spscq.IsEmpty);
            }

            for (int i = 0; i < Count; i++)
            {
                Assert.False(spscq.IsEmpty);
                int j;
                Assert.True(spscq.TryDequeue(out j));
                Assert.Equal(i, j);
            }

            Assert.True(spscq.IsEmpty);
        }
        public void Enqueue_Dequeue_IsEmpty()
        {
            var spscq = new SingleProducerSingleConsumerQueue<int>();
            Assert.True(spscq.IsEmpty);

            for (int i = 0; i < 10; i++)
            {
                spscq.Enqueue(i);
                Assert.False(spscq.IsEmpty);

                int j;
                Assert.True(spscq.TryDequeue(out j));
                Assert.Equal(i, j);
                Assert.True(spscq.IsEmpty);
            }

            const int Count = 100000;

            for (int i = 0; i < Count; i++)
            {
                spscq.Enqueue(i);
                Assert.False(spscq.IsEmpty);
            }

            for (int i = 0; i < Count; i++)
            {
                Assert.False(spscq.IsEmpty);
                int j;
                Assert.True(spscq.TryDequeue(out j));
                Assert.Equal(i, j);
            }

            Assert.True(spscq.IsEmpty);
        }
Exemplo n.º 3
0
        public void PriorityChannelBenchmarkSlowWriterTryThenBlock2()
        {
            var count = 1_000_000;
            // var cts = new CancellationTokenSource();
            var pc = new SingleProducerSingleConsumerQueue <int>();

            var rt = Task.Run(() =>
            {
                using (Benchmark.Run("Read", count, true))
                {
                    var c       = 0;
                    var spinner = new SpinWait();
                    while (c < count)
                    {
                        if (pc.TryDequeue(out var i))
                        {
                            c++;
                            continue;
                        }

                        if (pc.TryDequeue(out i))
                        {
                            c++;
                            continue;
                        }
                    }
                }
            });

            var wt = Task.Run(() =>
            {
                using (Benchmark.Run("Write", count, true))
                {
                    for (var i = 0; i < count; i++)
                    {
                        var priority = false; //  i % 10 == 0;
                        pc.Enqueue(i);
                        //if (i % 100 == 0)
                        //{
                        //    if (!Thread.Yield())
                        //    {
                        //        Thread.Sleep(0);
                        //    }
                        //}
                        //else
                        //{
                        Thread.SpinWait(10);
                        //}
                    }
                }
            });

            rt.Wait();

            wt.Wait();

            Benchmark.Dump();
        }