Synchronization Functions


InterlockedCompareExchange Function

Performs an atomic compare-and-exchange operation on the specified values. The function compares two specified 32-bit values and exchanges with another 32-bit value based on the outcome of the comparison.

If you are exchanging pointer values, this function has been superseded by the InterlockedCompareExchangePointer function.

To operate on 64-bit values, use the InterlockedCompareExchange64 function.

Syntax

C++
LONG __cdecl InterlockedCompareExchange(
  __inout  LONG volatile *Destination,
  __in     LONG Exchange,
  __in     LONG Comparand
);

Parameters

Destination [in, out]

A pointer to the destination value.

Exchange [in]

The exchange value.

Comparand [in]

The value to compare to Destination.

Return Value

The function returns the initial value of the Destination parameter.

Remarks

The function compares the Destination value with the Comparand value. If the Destination value is equal to the Comparand value, the Exchange value is stored in the address specified by Destination. Otherwise, no operation is performed.

The parameters for this function must be aligned on a 32-bit boundary; otherwise, the function will behave unpredictably on multiprocessor x86 systems and any non-x86 systems. See _aligned_malloc.

The interlocked functions provide a simple mechanism for synchronizing access to a variable that is shared by multiple threads. This function is atomic with respect to calls to other interlocked functions.

This function is implemented using a compiler intrinsic where possible. For more information, see the Winbase.h header file and _InterlockedCompareExchange.

This function generates a full memory barrier (or fence) to ensure that memory operations are completed in order.

Intel IPF:  For performance-critical applications, use InterlockedCompareExchangeAcquire or InterlockedCompareExchangeRelease instead.

Requirements

Minimum supported clientWindows 2000 Professional
Minimum supported serverWindows 2000 Server
HeaderWinbase.h (include Windows.h)
LibraryKernel32.lib
DLLKernel32.dll

See Also

Interlocked Variable Access
InterlockedCompare64Exchange128
InterlockedCompareExchange64
InterlockedCompareExchangeAcquire
InterlockedCompareExchangePointer
InterlockedCompareExchangeRelease
Synchronization Functions

Send comments about this topic to Microsoft

Build date: 11/19/2009

Tags :


Community Content

Stanley Roark
InterlockedAnything

InterlockedCompareExchange can be used to implement any interlocked operation that operates on a single word of data. The pattern is as follows:

  1. Atomically read variable into old_value
  2. Operate on old_value to produce new_value
  3. Set variable to new_value if the current value of variable is still old_value
    • otherwise, go back to 1

For example, here's how a Lehmer PRNG can be implemented in a lock-free way:

LONG Random(LONG seed)
{
return (LONG)(DWORD)(0x41C64E6D * (DWORD64)(DWORD)seed + 0x3039) % ((DWORD64)MAXDWORD + 1);
}




LONG InterlockedRandom(volatile LONG * seed)
{
LONG old_value = *seed;
LONG cur_value = old_value;
LONG new_value;





for(;;)
{
// calculate the function

new_value = Random(old_value);





// set the new value if the current value is still the expected one
cur_value = InterlockedCompareExchange(seed, new_value, old_value);





// we found the expected value: the exchange happened
if(cur_value == old_value)
break;





// recalculate the function on the unexpected value
old_value = cur_value;
}





// success
return new_value;
}
Tags :

AMissico
Semantics of InterlockedCompareExchange

InterlockedCompareExchange can be understood as _atomic_ implementation of the following snippet:

LONG CmpXchg(LONG * Destination, LONG Exchange, Long Comparand) {
    LONG retval = *Destination
    if (*Destination == Comparand)
       *Destination = Exchange;







return retval;
}
Tags :

Page view tracker