Why is there no overload of Interlocked.Add that accepts Doubles as parameters?

Others have addressed the “why?”. It is easy however to roll your own Add(ref double, double), using the CompareExchange primitive: public static double Add(ref double location1, double value) { double newCurrentValue = location1; // non-volatile read, so may be stale while (true) { double currentValue = newCurrentValue; double newValue = currentValue + value; newCurrentValue = … Read more

Alignment requirements for atomic x86 instructions vs. MS’s InterlockedCompareExchange documentation?

x86 does not require alignment for a lock cmpxchg instruction to be atomic. However, alignment is necessary for good performance. This should be no surprise, backward compatibility means that software written with a manual from 14 years ago will still run on today’s processors. Modern CPUs even have a performance counter specifically for split-lock detection … Read more

Reading an int that’s updated by Interlocked on other threads

I’m a firm believer in that if you’re using interlocked to increment shared data, then you should use interlocked everywhere you access that shared data. Likewise, if you use insert you favorite synchronization primitive here to increment shared data, then you should use insert you favorite synchronization primitive here everywhere you access that shared data. … Read more