Platform SDK: DLLs, Processes, and Threads

InterlockedCompareExchangePointer

The InterlockedCompareExchangePointer function performs an atomic comparison of the specified values parameters and exchanges the values, based on the outcome of the comparison. More than one thread cannot use the same variable simultaneously.

If you are exchanging pointer values, this function supersedes the InterlockedCompareExchange function.

PVOID InterlockedCompareExchangePointer (
  PVOID *Destination,  // destination address
  PVOID Exchange,      // exchange value
  PVOID Comperand      // value to compare
);

Parameters

Destination
[in,out] Pointer to the pointer to the destination value. The sign is ignored.
Exchange
[in] Specifies the exchange value. The sign is ignored.
Comperand
[in] Specifies the value to compare to Destination. The sign is ignored.

Return Values

The return value is the initial value of the destination.

Remarks

On a 64-bit system, the parameters are 64 bits and must be aligned on 64-bit boundaries; otherwise, the function will fail. On a 32-bit system, the parameters are 32 bits and must be aligned on 32-bit boundaries.

The interlocked functions provide a simple mechanism for synchronizing access to a variable that is shared by multiple threads. The threads of different processes can use this mechanism if the variable is in shared memory.

The InterlockedCompareExchange function 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.

Requirements

  Windows NT/2000: Requires Windows NT 4.0 or later.
  Windows 95/98: Requires Windows 98.
  Header: Declared in Winbase.h; include Windows.h.
  Library: Use Kernel32.lib.

See Also

Synchronization Overview, Synchronization Functions, Interlocked Variable Access, InterlockedCompareExchange, InterlockedDecrement, InterlockedExchange, InterlockedExchangeAdd, InterlockedExchangePointer, InterlockedIncrement