public void Should_process_multithreaded_workload()
 {
     var atomicCounter = new AtomicCounter(0);
     using (var threadPool = new DedicatedThreadPool(new DedicatedThreadPoolSettings(2)))
     {
         for (var i = 0; i < 1000; i++)
         {
             threadPool.QueueUserWorkItem(() => atomicCounter.GetAndIncrement());
         }
         SpinWait.SpinUntil(() => atomicCounter.Current == 1000, TimeSpan.FromSeconds(1));
     }
     Assert.Pass(string.Format("Passed! Final counter value: {0} / Expected {1}", atomicCounter.Current, 1000));
 }
Ejemplo n.º 2
0
        public void Should_process_multithreaded_workload()
        {
            var atomicCounter = new AtomicCounter(0);

            using (var threadPool = new DedicatedThreadPool(new DedicatedThreadPoolSettings(2)))
            {
                for (var i = 0; i < 1000; i++)
                {
                    threadPool.QueueUserWorkItem(() => atomicCounter.GetAndIncrement());
                }
                SpinWait.SpinUntil(() => atomicCounter.Current == 1000, TimeSpan.FromSeconds(1));
            }
            Assert.Pass(string.Format("Passed! Final counter value: {0} / Expected {1}", atomicCounter.Current, 1000));
        }
        public void Should_use_all_threads_for_many_tasks()
        {
            var    threadIds     = new ConcurrentBag <int>();
            var    atomicCounter = new AtomicCounter(0);
            Action callback      = () =>
            {
                atomicCounter.GetAndIncrement();
                threadIds.Add(Thread.CurrentThread.ManagedThreadId);
            };

            for (var i = 0; i < 1000; i++)
            {
                Factory.StartNew(callback);
            }
            //spin until work is completed
            SpinWait.SpinUntil(() => atomicCounter.Current == 1000, TimeSpan.FromSeconds(1));

            Assert.True(threadIds.Distinct().Count() <= Pool.Settings.NumThreads);
        }
        public void Should_use_all_threads_for_many_tasks()
        {
            var threadIds = new ConcurrentBag<int>();
            var atomicCounter = new AtomicCounter(0);
            Action callback = () =>
            {
                atomicCounter.GetAndIncrement();
                threadIds.Add(Thread.CurrentThread.ManagedThreadId);
            };

            for (var i = 0; i < 1000; i++)
            {
                Factory.StartNew(callback);
            }
            //spin until work is completed
            SpinWait.SpinUntil(() => atomicCounter.Current == 1000, TimeSpan.FromSeconds(1));

            Assert.AreEqual(Pool.Settings.NumThreads, threadIds.Distinct().Count());
        }
        public void Should_process_workload_across_exactly_DedicatedThreadPoolSettings_NumThreads()
        {
            var numThreads = Environment.ProcessorCount;
            var threadIds = new ConcurrentBag<int>();
            var atomicCounter = new AtomicCounter(0);
            Action callback = () =>
            {
                atomicCounter.GetAndIncrement();
                threadIds.Add(Thread.CurrentThread.ManagedThreadId);
            };
            using (var threadPool = new DedicatedThreadPool(new DedicatedThreadPoolSettings(numThreads)))
            {
                for (var i = 0; i < 1000; i++)
                {
                    threadPool.QueueUserWorkItem(callback);
                }
                //spin until work is completed
                SpinWait.SpinUntil(() => atomicCounter.Current == 1000, TimeSpan.FromSeconds(1));
            }

            Assert.AreEqual(numThreads, threadIds.Distinct().Count());
        }
Ejemplo n.º 6
0
        public void Should_process_workload_across_AtMost_DedicatedThreadPoolSettings_NumThreads()
        {
            var    numThreads    = Environment.ProcessorCount;
            var    threadIds     = new ConcurrentBag <int>();
            var    atomicCounter = new AtomicCounter(0);
            Action callback      = () =>
            {
                atomicCounter.GetAndIncrement();
                threadIds.Add(Thread.CurrentThread.ManagedThreadId);
            };

            using (var threadPool = new DedicatedThreadPool(new DedicatedThreadPoolSettings(numThreads)))
            {
                for (var i = 0; i < 1000; i++)
                {
                    threadPool.QueueUserWorkItem(callback);
                }
                //spin until work is completed
                SpinWait.SpinUntil(() => atomicCounter.Current == 1000, TimeSpan.FromSeconds(1));
            }

            Assert.True(threadIds.Distinct().Count() <= numThreads);
        }
        public void Should_process_workload_across_exactly_DedicatedThreadPoolSettings_NumThreads()
        {
            var    numThreads    = 3;
            var    threadIds     = new ConcurrentBag <int>();
            var    atomicCounter = new AtomicCounter(0);
            Action callback      = () =>
            {
                atomicCounter.GetAndIncrement();
                threadIds.Add(Thread.CurrentThread.ManagedThreadId);
            };

            using (var threadPool = new DedicatedThreadPool(new DedicatedThreadPoolSettings(numThreads)))
            {
                for (var i = 0; i < numThreads; i++)
                {
                    threadPool.EnqueueWorkItem(callback);
                }
                //spin until work is completed
                SpinWait.SpinUntil(() => atomicCounter.Current == 1000, TimeSpan.FromSeconds(1));
            }

            Assert.AreEqual(numThreads, threadIds.Distinct().Count());
        }