The InterlockedCompareExchangeToMode function performs an atomic compare-and-exchange operation on a 32-bit value, based on the specified processor mode.
Syntax
LONG InterlockedCompareExchangeToMode(
LONG volatile *Destination,
LONG ExChange,
LONG Comperand,
KPROCESSOR_MODE Mode
);
Parameters
Destination
[in, out]指向包含目标值的内存位置的指针。 The pointer must be naturally aligned for the data type; that is, the memory location must be 4-byte aligned since Destination points to a 4-byte type.
ExChange
[in]交换值。
Comperand
[in] The value to compare to Destination.
Mode
[in]确定内存访问的执行方式的处理器模式。 Mode can be one of the following values.
| Value | Meaning |
|---|---|
| KernelMode | Destination points to kernel-mode memory. 该函数执行直接原子比较和交换作。 有关更多详细信息,请参阅“备注”。 |
| UserMode | Destination points to user-mode memory. The function raises an exception if Destination doesn't point to user-mode memory; otherwise it performs an atomic compare-and-exchange operation at the specified address. 有关更多详细信息,请参阅“备注”。 |
Return value
The function returns the initial value of the Destination parameter.
Remarks
联锁函数提供了一种简单的机制,用于同步对多个线程共享的变量的访问。 对于对其他互锁函数的调用,此函数是原子的。 它生成完整内存屏障,以确保按顺序完成内存作。
此函数提供对内存中 32 位值的原子比较和交换访问。 The function compares 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. 否则,不执行任何作。 行为取决于指定的处理器模式:
- When Mode is UserMode, the function ensures that the destination address is a valid user-mode address and performs the operation safely for user-mode memory access.
- When Mode is KernelMode, the function operates directly on kernel-mode addresses.
如果作失败,函数将引发结构化异常,例如当目标地址不可访问或指定模式无效时。
此函数适用于所有版本的 Windows,而不仅仅是最新版本。 You need to consume the latest WDK to get the function declaration from the usermode_accessors.h header. You also need the library (umaccess.lib) from the latest WDK. 但是,生成的驱动程序将在较旧版本的 Windows 上运行。
Requirements
| Requirement | Value |
|---|---|
| 最低支持的客户端 | See Remarks |
| Header | usermode_accessors.h |
| Library | umaccess.lib |
| IRQL | 小于或等于APC_LEVEL |