public void ParallelAddTake()
        {
            int size = 100000;
            int[] correct = new int[size];

            var queue = new NoWaitProducerConsumerCollection<int>();
            ThreadPool.QueueUserWorkItem(
                x =>
                    {
                        foreach (var i in Range(size).AsParallel())
                        {
                            queue.Add(i);
                        }
                    });

            foreach (var i in Range(size).AsParallel())
            {
                while (true)
                {
                    int result;
                    if (queue.TryTake(out result))
                    {
                        correct[result]++;
                        break;
                    }
                }
            }
            foreach (var i in Range(size))
            {
                Assert.AreEqual(1, correct[i], "Invalid value at" + i + " = " + correct[i]);
            }
        }
        public void ParallelAddTake()
        {
            int size = 100000;

            int[] correct = new int[size];

            var queue = new NoWaitProducerConsumerCollection <int>();

            ThreadPool.QueueUserWorkItem(
                x =>
            {
                foreach (var i in Range(size).AsParallel())
                {
                    queue.Add(i);
                }
            });

            foreach (var i in Range(size).AsParallel())
            {
                while (true)
                {
                    int result;
                    if (queue.TryTake(out result))
                    {
                        correct[result]++;
                        break;
                    }
                }
            }
            foreach (var i in Range(size))
            {
                Assert.AreEqual(1, correct[i], "Invalid value at" + i + " = " + correct[i]);
            }
        }
        public void ParallelAddSequentialTake()
        {
            int size = 1000;

            var queue = new NoWaitProducerConsumerCollection<int>();
            foreach (var i in Range(size).AsParallel())
            {
                queue.Add(i);
            }
            for (int i = 0; i < size; i++)
            {
                int result;
                Assert.IsTrue(queue.TryTake(out result));
                Assert.AreEqual(i, result);
            }
        }
        public void ParallelAddSequentialTake()
        {
            int size = 1000;

            var queue = new NoWaitProducerConsumerCollection <int>();

            foreach (var i in Range(size).AsParallel())
            {
                queue.Add(i);
            }
            for (int i = 0; i < size; i++)
            {
                int result;
                Assert.IsTrue(queue.TryTake(out result));
                Assert.AreEqual(i, result);
            }
        }
示例#5
0
文件: Barrier.cs 项目: ArturD/Daemons
 internal void JoinSync(Action action)
 {
     if (Interlocked.Decrement(ref _counter) < 0)
     {
         throw new InvalidOperationException("Joined more than expected (counter below 0).");
     }
     _continuations.Add(action);
     if (Interlocked.Decrement(ref _counter2) == 0)
     {
         while (true)
         {
             Action continuation;
             if (!_continuations.TryTake(out continuation))
             {
                 break;
             }
             continuation();
         }
     }
 }