InterlockedCompareExchange 函数 (wdm.h)

InterlockedCompareExchange 例程执行原子操作,将 Destination 指向的输入值与 Comperand 的值进行比较。

语法

LONG CDECL_NON_WVMPURE InterlockedCompareExchange(
  [in, out] LONG volatile *Destination,
  [in]      LONG          ExChange,
  [in]      LONG          Comperand
);

参数

[in, out] Destination

指向与 Comperand 值进行比较的输入值的指针。

[in] ExChange

如果 Destination 指向的输入值等于 Comperand 的值,则指定 Destination 指向的输出值。

[in] Comperand

指定与 目标指向的输入值进行比较的值。

返回值

InterlockedCompareExchange 返回 *Destination 的原始值。

注解

如果 Comperand 等于 *Destination,则 *Destination 设置为等于 Exchange。 否则,*目标 保持不变。

InterlockedCompareExchange 提供了一种快速的原子方式,用于同步多个线程共享的变量的测试和更新。 如果 Destination 指向的输入值等于 Comperand 的值,则 Destination 的输出值设置为 Exchange 的值。

InterlockedCompareExchange 旨在提高速度,通常由编译器内联实现。 InterlockedCompareExchange 仅在其他 InterlockedXxx 调用方面是原子的。 它不使用旋转锁,可以安全地用于可分页数据。

互锁操作不能用于非缓存内存。

要求

要求
最低受支持的客户端 从 Windows 2000 开始可用。
目标平台 通用
标头 wdm.h (包括 Wdm.h、Ntddk.h、Ntifs.h、Miniport.h)
Library Windows 10 上的 OneCoreUAP.lib
IRQL 任何级别

另请参阅

ExInterlockedCompareExchange64

InterlockedCompareExchangePointer

InterlockedDecrement

InterlockedExchange

InterlockedExchangePointer

InterlockedIncrement