为什么InterLocked比锁定慢?

时间:2016-05-10 07:43:48

标签: c# multithreading

出于好奇,我制作了一个程序来测试.Net中InterLocked vs lock的性能。 事实证明,InterLocked版本远比Locked版本慢,如果我在这里遗漏了一些细节,请有人指出。 根据我的理解,Interlocked应该比锁定更好。

public class TestB
    {
        private static readonly object _objLocker = new object();
        private long _shared;
        public void IncrLocked()
        {
            lock (_objLocker)
            {
                _shared++;
            }
        }
        public void IncrInterLocked()
        {
            Interlocked.Increment(ref _shared);
        }
        public long GetValue()
        {
            return _shared;
        }
    }
    class TestsCopy
    {
        private static TestB _testB = new TestB();
        static void Main(string[] args)
        {
            int numofthreads = 100;
            TestInterLocked(numofthreads);
            TestLocked(numofthreads);
            Console.ReadLine();
        }
        private static void TestInterLocked(int numofthreads)
        {
            Thread[] threads = new Thread[numofthreads];
            for (int i = 0; i < numofthreads; i++)
            {
                Thread t = new Thread(StartTestInterLocked);
                threads[i] = t;
                t.Start();
            }
            Stopwatch sw = new Stopwatch();
            sw.Start();
            for (int i = 0; i < threads.Length; i++)
            {
                threads[i].Join();
            }
            sw.Stop();
            Console.WriteLine($"Interlocked finished in : {sw.ElapsedMilliseconds}, value = {_testB.GetValue()}");
        }

        private static void TestLocked(int numofthreads)
        {
            Thread[] threads = new Thread[numofthreads];
            for (int i = 0; i < numofthreads; i++)
            {
                Thread t = new Thread(StartTestLocked);
                threads[i] = t;
                t.Start();
            }
            Stopwatch sw = new Stopwatch();
            sw.Start();
            for (int i = 0; i < threads.Length; i++)
            {
                threads[i].Join();
            }
            sw.Stop();
            Console.WriteLine($"Locked finished in : {sw.ElapsedMilliseconds}, value = {_testB.GetValue()}");
        }

        private static void StartTestInterLocked()
        {
            int counter = 10000000;
            for (int i = 0; i < counter; i++)
            {
                _testB.IncrInterLocked();
            }
        }
        private static void StartTestLocked()
        {
            int counter = 10000000;
            for (int i = 0; i < counter; i++)
            {
                _testB.IncrLocked();
            }
        }

该计划的输出是......

Interlocked finished in : 76909 ms, value = 1000000000
Locked finished in : 44215 ms, value = 2000000000

1 个答案:

答案 0 :(得分:3)

由于Jakob Olsen提到的原因,您的测试存在缺陷。此外,您的测试还包括在类中调用方法的开销(对于lock,显然在调用Interlocked.Increment()时无法避免。

你应该开始所有的线程,并安排他们在你启动秒表后开始工作。你可以让他们等待ManualResetEvent

我已经重写了你的测试代码:

using System;
using System.Diagnostics;
using System.Threading;

namespace Demo
{
    static class Program
    {
        private static readonly object _objLocker = new object();
        private static long _shared;
        private static ManualResetEvent _signal = new ManualResetEvent(false);

        static void Main(string[] args)
        {
            int numofthreads = 100;
            TestInterLocked(numofthreads);
            TestLocked(numofthreads);
            Console.ReadLine();
        }

        private static void TestInterLocked(int numofthreads)
        {
            Thread[] threads = new Thread[numofthreads];
            for (int i = 0; i < numofthreads; i++)
            {
                Thread t = new Thread(StartTestInterLocked);
                threads[i] = t;
                t.Start();
            }
            Thread.Sleep(5000); // Make sure threads have had time to start.
            Stopwatch sw = new Stopwatch();
            sw.Start();
            _shared = 0;
            _signal.Set();

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

            sw.Stop();
            _signal.Reset();
            Console.WriteLine($"Interlocked finished in : {sw.ElapsedMilliseconds}, value = {_shared}");
        }

        private static void TestLocked(int numofthreads)
        {
            Thread[] threads = new Thread[numofthreads];
            for (int i = 0; i < numofthreads; i++)
            {
                Thread t = new Thread(StartTestLocked);
                threads[i] = t;
                t.Start();
            }
            Thread.Sleep(5000); // Make sure threads have had time to start.
            Stopwatch sw = new Stopwatch();
            sw.Start();
            _shared = 0;
            _signal.Set();

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

            sw.Stop();
            _signal.Reset();
            Console.WriteLine($"Locked finished in : {sw.ElapsedMilliseconds}, value = {_shared}");
        }

        private static void StartTestInterLocked()
        {
            _signal.WaitOne();
            int counter = 10000000;
            for (int i = 0; i < counter; i++)
            {
                Interlocked.Increment(ref _shared);
            }
        }

        private static void StartTestLocked()
        {
            _signal.WaitOne();
            int counter = 10000000;
            for (int i = 0; i < counter; i++)
            {
                lock (_objLocker)
                {
                    _shared++;
                }
            }
        }
    }
}

现在结果是(来自RELEASE构建):

Interlocked finished in : 11339, value = 1000000000
Locked finished in : 30546, value = 1000000000

如您所见,Interlocked要快得多。