gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 00:33:08 25 4
gpt4 key购买 nike

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com