Esempio n. 1
0
        private static void RunTestBCL(int threadCount, int elemCount, int opCount, int pauseSpin)
        {
            ObjectPoolBCL <PoolElem> pool = new ObjectPoolBCL <PoolElem>();

            for (int i = 0; i < elemCount; i++)
            {
                pool.PutObject(new PoolElem());
            }

            TestBCL(pool, threadCount, opCount, pauseSpin);
        }
Esempio n. 2
0
        private static TimeSpan TestBCL(ObjectPoolBCL <PoolElem> pool, int threadCount, int opCount, int pauseSpin)
        {
            Thread[] threads  = new Thread[threadCount];
            Barrier  startBar = new Barrier(threadCount + 1);

            int opCountPerThread = opCount / threadCount;

            Action thAct = () =>
            {
                startBar.SignalAndWait();
                int execOp = 0;
                while (execOp++ < opCountPerThread)
                {
                    PoolElem el = null;
                    try
                    {
                        el = pool.GetObject();
                        //Thread.Sleep(pauseSpin);
                        SpinWaitHelper.SpinWait(pauseSpin);
                    }
                    finally
                    {
                        pool.PutObject(el);
                    }
                }
            };


            for (int i = 0; i < threads.Length; i++)
            {
                threads[i] = new Thread(new ThreadStart(thAct));
            }

            for (int i = 0; i < threads.Length; i++)
            {
                threads[i].Start();
            }

            startBar.SignalAndWait();
            Stopwatch sw = Stopwatch.StartNew();

            for (int i = 0; i < threads.Length; i++)
            {
                threads[i].Join();
            }

            sw.Stop();

            Console.WriteLine("BCL ObjPool. Elapsed = " + sw.ElapsedMilliseconds.ToString() + "ms");

            return(sw.Elapsed);
        }