예제 #1
0
        public void OneProdOneCons()
        {
            BoundedQueue <int> queue = new BoundedQueue <int>(2);

            ParallelTasks ptasks = new ParallelTasks();

            // PRODUCER thread inserts 4 elements
            ptasks.Add("Producer", () =>
            {
                for (int i = 0; i < 4; i++)
                {
                    queue.Enqueue(i);
                }
            });

            // CONSUMER thread removes 4 elements
            ptasks.Add("Consumer", () =>
            {
                for (int i = 0; i < 4; i++)
                {
                    Assert.AreEqual(i, queue.Dequeue());
                }
            });

            ptasks.Execute();

            // check size: should be zero
            Assert.AreEqual(queue.Size(), 0);
        }
예제 #2
0
        public void OneProdOneClear()
        {
            BoundedQueue <int> queue = new BoundedQueue <int>(2);

            ParallelTasks ptasks = new ParallelTasks();

            // PRODUCER thread inserts 4 elements
            ptasks.Add("Producer", () =>
            {
                for (int i = 0; i < 4; i++)
                {
                    queue.Enqueue(i);
                }
            });

            // CLEAR thread clears queue once it reaches size 2
            ptasks.Add("Clear", () =>
            {
                while (queue.Size() < 2)
                {
                    Thread.Sleep(0);
                }
                queue.Clear();
            });

            ptasks.Execute();
        }