Volatile vs. Interlocked vs. lock

后端 未结 9 1722
悲哀的现实
悲哀的现实 2020-11-22 05:54

Let\'s say that a class has a public int counter field that is accessed by multiple threads. This int is only incremented or decremented.

T

9条回答
  •  一个人的身影
    2020-11-22 06:06

    I did some test to see how the theory actually works: kennethxu.blogspot.com/2009/05/interlocked-vs-monitor-performance.html. My test was more focused on CompareExchnage but the result for Increment is similar. Interlocked is not necessary faster in multi-cpu environment. Here is the test result for Increment on a 2 years old 16 CPU server. Bare in mind that the test also involves the safe read after increase, which is typical in real world.

    D:\>InterlockVsMonitor.exe 16
    Using 16 threads:
              InterlockAtomic.RunIncrement         (ns):   8355 Average,   8302 Minimal,   8409 Maxmial
        MonitorVolatileAtomic.RunIncrement         (ns):   7077 Average,   6843 Minimal,   7243 Maxmial
    
    D:\>InterlockVsMonitor.exe 4
    Using 4 threads:
              InterlockAtomic.RunIncrement         (ns):   4319 Average,   4319 Minimal,   4321 Maxmial
        MonitorVolatileAtomic.RunIncrement         (ns):    933 Average,    802 Minimal,   1018 Maxmial
    

提交回复
热议问题