InterlockedCompareExchange function

Compares the input to the comparison value and exchanges the result, atomically.


void InterlockedCompareExchange(
  in  UINT dest,
  in  UINT compare_value,
  in  UINT value,
  out UINT original_value


dest [in]

Type: UINT

The destination address.

compare_value [in]

Type: UINT

The comparison value.

value [in]

Type: UINT

The input value.

original_value [out]

Type: UINT

The original value.

Return value

This function does not return a value.


Atomically compares the value in dest to compare_value, stores value in dest if the values match, returns the original value of dest in original_value. This operation can only be performed on int or uint typed resources and shared memory variables. There are three possible uses for this function. The first is when R is a shared memory variable type. In this case, the function performs the operation on the shared memory register referenced by dest. The second scenario is when R is a resource variable type. In this scenario, the function performs the operation on the resource location referenced by dest. Finally, the third scenario is when R is a local variable type. In this scenario, the function reduces to the operation performed using local operations. This operation is only available when R is readable and writable.


If you call InterlockedCompareExchange in a for or while compute shader loop, to properly compile, you must use the [allow_uav_condition] attribute on that loop.


This function is supported in the following types of shaders:

x x x x x x


See also


Shader Model 5