Note that to generate the intrinsic expansion, you need to use /Oi. /Oi is implied with /O2.
To declare one of the interlocked functions for use as an intrinsic, the function must be declared with the leading underscore and the new function must appear in a #pragma intrinsic statement. For convenience, the intrinsic versions of the functions can be declared in a #define statement to appear in the source code without the leading underscore.
_InterlockedCompareExchange performs an atomic comparison of the Destination value with the Comperand value. If the Destination value is equal to the Comperand value, the Exchange value is stored in the address specified by Destination. Otherwise, no operation is performed.
For a sample of how to use _InterlockedCompareExchange, see _InterlockedDecrement.
There are several variations on _InterlockedCompareExchange that vary based on the data types they involve and whether processor-specific acquire or release semantics is used.
While the _InterlockedCompareExchange function operates on long integer values, _InterlockedCompareExchange16 operates on short integer values and _InterlockedCompareExchange64 operates on 64-bit integer values. Because _InterlockedCompareExchange64 uses the cmpxchg8b instruction, it is not available on pre-Pentium processors, such as the 486.
The IPF-specific _InterlockedCompareExchange_acq, _InterlockedCompareExchange16_acq, and _InterlockedCompareExchange64_acq intrinsic functions are the same as the corresponding functions without the acq suffix except that the operation is performed with acquire semantics, which is useful when entering a critical section.
The _InterlockedCompareExchange_rel, _InterlockedCompareExchange16_rel, and _InterlockedCompareExchange64_rel intrinsic functions are the same as the corresponding functions without the rel suffix except that the operation is performed with release semantics, which is useful when leaving a critical section.
In Visual C++ 2005, these functions behave as read-write memory barriers. For more information, see _ReadWriteBarrier.
These routines are only available as intrinsics.