Performance of Interlocked.Increment

SLaks picture SLaks · Jun 23, 2009 · Viewed 15.7k times · Source

Is Interlocked.Increment(ref x) faster or slower than x++ for ints and longs on various platforms?

Answer

Michael picture Michael · Jun 23, 2009

It is slower since it forces the action to occur atomically and it acts as a memory barrier, eliminating the processor's ability to re-order memory accesses around the instruction.

You should be using Interlocked.Increment when you want the action to be atomic on state that can be shared between threads - it's not intended to be a full replacement for x++.