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 post 。 Linux memory barrier documentation可能也有用...
关于winapi - InterlockedCompareExchange Release() 和 Acquire() 之间有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9764181/