InterlockedCompareExchange Release()和Acquire()有什么区别? [英] What's the difference between InterlockedCompareExchange Release() and Acquire()?

查看:246
本文介绍了InterlockedCompareExchange Release()和Acquire()有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

When I try to learn the synchronization functions with WIN32 API, I find there are two functions named differently but seems to do the same thing:

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,它说这些功能是:

I check the MSDN, it says those functions are:

对指定的对象执行原子比较和交换操作 价值观.该函数比较两个指定的32位值,并且 根据运算结果与另一个32位值进行交换 比较.

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.

所以我很好奇这两个功能之间的区别. 什么时候使用获取内存访问语义释放内存访问语义? 有例子吗?

So I'm curious about the difference between these two functions. When to use the acquire memory access semantics or release memory access semantics? Are there any examples?

谢谢!

推荐答案

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

The plain version uses a full barrier while the suffixed versions only deals with loads or stores, this can be faster on some CPUs (Itanium-based processors etc)

MSDN上有一篇有关获取和发布语义互锁* API 作为以及这篇很棒的博客文章. Linux内存屏障文档可能也有用...

MSDN has a article about Acquire and Release Semantics and the Interlocked* API as well as this great blog post. The Linux memory barrier documentation might also be useful...

这篇关于InterlockedCompareExchange Release()和Acquire()有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆