winapi - InterlockedCompareExchange Release() 和 Acquire() 之间有什么区别?

标签 winapi synchronization interlocked

InterlockedCompareExchangeRelease()InterlockedCompareExchangeAcquire() 之间有什么区别?

当我尝试学习WIN32 API的同步函数时,我发现有两个函数名称不同,但似乎做同样的事情:

LONG __cdecl InterlockedCompareExchangeRelease(
  __inout  LONG volatile *Destination,
  __in     LONG Exchange,
  __in     LONG Comparand
);

LONG __cdecl InterlockedCompareExchangeAcquire(
  __inout  LONG volatile *Destination,
  __in     LONG Exchange,
  __in     LONG Comparand
);

我查了一下MSDN,它说这些功能是:

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.

但是对于InterlockedCompareExchangeAcquire()

The operation is performed with acquire memory access semantics.

对于InterlockedCompareExchangeRelease()

The exchange is performed with release memory access semantics.

所以我很好奇这两个函数之间的区别。 何时使用获取内存访问语义释放内存访问语义? 有没有例子?

谢谢!

最佳答案

普通版本使用完整屏障,而后缀版本仅处理加载存储,这在某些 CPU(基于 Itanium 的处理器等)上可能会更快

MSDN 有一篇关于 Acquire and Release Semantics 的文章和 Interlocked* API以及this great blog postLinux memory barrier documentation可能也有用...

关于winapi - InterlockedCompareExchange Release() 和 Acquire() 之间有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9764181/

相关文章:

c - 如何更改按钮上的光标?

html - 在 HTML5 本地存储上实现同步原语

c++ - 具有获取和释放语义的互锁操作(多平台)

c# - 等待所有异步 Web 服务调用返回

c# - 与使用 Interlocked 类相比,使用 volatile 关键字有什么优势吗?

c++ - 有人可以帮助发现我的低锁定列表中的错误吗?

c++ - 如何检测属性表中的当前事件页面 (C++/VS)

c# - 更改 Windows 7 窗口颜色

c++ - 打印作业时,最后的作业状态是 JOB_STATUS_RETAINED,而不是 JOB_STATUS_PRINTED

android - 移动应用程序和 WooCommerce WordPress 网站数据同步