Пример #1
0
        public void SpinLock_Benchmark()
        {
            var sl    = new SpinLock();
            var model = new DummyModelLock();


            void Action()
            {
                for (int i = 0; i < Size; i++)
                {
                    var gotLock = false;
                    try
                    {
                        sl.Enter(ref gotLock);
                        model.Counter += (i % 10);
                    }
                    finally
                    {
                        // Only give up the lock if you actually acquired it
                        if (gotLock)
                        {
                            sl.Exit();
                        }
                    }
                }
            }

            Parallel.Invoke(Action, Action, Action);
        }
Пример #2
0
        public void SimpleSpinLock_Benchmark()
        {
            var m_sl  = new SimpleSpinLock();
            var model = new DummyModelLock();

            void Action()
            {
                m_sl.Enter();
                for (int i = 0; i < Size; i++)
                {
                    try
                    {
                        model.Counter += (i % 10);
                    }
                    finally
                    {
                    }
                }
                m_sl.Leave();
            }

            Parallel.Invoke(Action, Action, Action);
        }
Пример #3
0
        public void Lock_Benchmark()
        {
            object balanceLock = new object();
            var    model       = new DummyModelLock();

            void Action()
            {
                lock (balanceLock)
                {
                    for (int i = 0; i < Size; i++)
                    {
                        try
                        {
                            model.Counter += (i % 10);
                        }
                        finally
                        {
                        }
                    }
                }
            }

            Parallel.Invoke(Action, Action, Action);
        }