views:

656

answers:

1

Is there an equivalent for Interlocked.Exchange for boolean? i.e., an atomic exchange of values that returns the previous value and doesn't require locks?

+5  A: 

No; use integers instead of booleans.

In principle such a thing could be written (cmpxchg, the underlying processor instruction, can operate on 8, 16, 32, and 64-bit operands on x86, 8, 16, 32, 64, and 128-bit operands on x64), but in practice most APIs stick to pointer and double pointer (32 and 64-bit on x86, 64 and 128-bit on x64) operands, because they're all you really need.

DrPizza