Question

I presume this atomic operation is faster than ++. I only see advantages favoring Interlocked.Increment. What are its disavantages?

Was it helpful?

Solution

Atomic means it is thread-safe (i.e. it is impossible for one thread to read the value while another is changing it.) Which makes it slower, not faster, due to the need to use thread synchronization mechanisms. You want to use ++ if you don't care about thread-safety. Here is some discussion on the relative performance of the ++ operator in different contexts.

OTHER TIPS

Atomic does not mean it is faster. In fact, it will almost definitely be slower.

In concurrent programming, an operation (or set of operations) is atomic, linearizable, indivisible or uninterruptible if it appears to the rest of the system to occur instantaneously.

This merely means there are no observable side effects during the operation. It does not say anything about how long the operation takes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top