请在x86_64的原子CAS-操作和ARM始终使用std :: memory_order_seq_cst? [英] Do atomic CAS-operations on x86_64 and ARM always use std::memory_order_seq_cst?

查看:593
本文介绍了请在x86_64的原子CAS-操作和ARM始终使用std :: memory_order_seq_cst?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于安东尼威廉斯说

some_atomic.load(的std :: memory_order_acquire)不只是下降到通过
  一个简单的加载指令,和
  some_atomic.store(的std :: memory_order_release)下降到通过一个简单的
  存储指令。

some_atomic.load(std::memory_order_acquire) does just drop through to a simple load instruction, and some_atomic.store(std::memory_order_release) drops through to a simple store instruction.

据了解,在 86 作为操作负荷()存储()内存屏障 memory_order_consume,memory_order_acquire,memory_order_release,memory_order_acq_rel 不需要处理器指令。

It is known that on x86 for the operations load() and store() memory barriers memory_order_consume, memory_order_acquire, memory_order_release, memory_order_acq_rel does not require a processor instructions.

但在 ARMv8 :我们知道,在这里都为装载内存屏障()存储()
<一href=\"http://channel9.msdn.com/Shows/Going+Deep/Cpp-and-Beyond-2012-Herb-Sutter-atomic-Weapons-1-of-2\" rel=\"nofollow\">http://channel9.msdn.com/Shows/Going+Deep/Cpp-and-Beyond-2012-Herb-Sutter-atomic-Weapons-1-of-2
<一href=\"http://channel9.msdn.com/Shows/Going+Deep/Cpp-and-Beyond-2012-Herb-Sutter-atomic-Weapons-2-of-2\" rel=\"nofollow\">http://channel9.msdn.com/Shows/Going+Deep/Cpp-and-Beyond-2012-Herb-Sutter-atomic-Weapons-2-of-2

关于CPU的不同的架构: http://g.oswego.edu/dl/ JMM / cookbook.html

About different architectures of CPUs: http://g.oswego.edu/dl/jmm/cookbook.html

其次,但在 86 ,这两条线有不同的记忆障碍是在拆卸code相同的( MSVS2012 x86_64的)的CAS操作:

Next, but for the CAS-operation on x86, these two lines with different memory barriers are identical in Disassembly code (MSVS2012 x86_64):

    a.compare_exchange_weak(temp, 4, std::memory_order_seq_cst, std::memory_order_seq_cst);
000000013FE71A2D  mov         ebx,dword ptr [temp]  
000000013FE71A31  mov         eax,ebx  
000000013FE71A33  mov         ecx,4  
000000013FE71A38  lock cmpxchg dword ptr [temp],ecx  

    a.compare_exchange_weak(temp, 5, std::memory_order_relaxed, std::memory_order_relaxed);
000000013FE71A4D  mov         ecx,5  
000000013FE71A52  mov         eax,ebx  
000000013FE71A54  lock cmpxchg dword ptr [temp],ecx  

拆卸code。通过 GCC 4.8.1编译的x86_64 - GDB

a.compare_exchange_weak(temp, 4, std::memory_order_seq_cst, std::memory_order_seq_cst);
a.compare_exchange_weak(temp, 5, std::memory_order_relaxed, std::memory_order_relaxed);

0x4613b7  <+0x0027>         mov    0x2c(%rsp),%eax
0x4613bb  <+0x002b>         mov    $0x4,%edx
0x4613c0  <+0x0030>         lock cmpxchg %edx,0x20(%rsp)
0x4613c6  <+0x0036>         mov    %eax,0x2c(%rsp)
0x4613ca  <+0x003a>         lock cmpxchg %edx,0x20(%rsp)

是在x86 / x86_64的平台,任何原子CAS-操作,比如像这样的 atomic_val.compare_exchange_weak(温度,1日的std :: memory_order_relaxed,性病:: memory_order_relaxed)的例子; 总是满意的订货的std :: memory_order_seq_cst

如果在x86上的任何CAS操作总是顺序一致性(的std :: memory_order_seq_cst )无论障碍跑,然后在ARMv8是一样的吗?

And if the any CAS operation on the x86 always run with sequential consistency (std::memory_order_seq_cst) regardless of barriers, then on the ARMv8 it is the same?

问:的std :: memory_order_relaxed CAS 的顺序块存储器在x86或ARM巴士?

QUESTION: Should the order of std::memory_order_relaxed for CAS block memory bus on x86 or ARM?

答: 86任 compare_exchange_weak() 任何操作的std :: memory_orders (甚至的std :: memory_order_relaxed 始终将转换为 LOCK CMPXCHG 用锁车,真正做到原子,并有等于昂贵 XCHG - CMPXCHG 是一样的 XCHG 指令昂贵。

ANSWER: On x86 any compare_exchange_weak() operations with any std::memory_orders(even std::memory_order_relaxed) always translates to the LOCK CMPXCHG with lock bus, to be really atomic, and have equal expensive to XCHG - "the cmpxchg is just as expensive as the xchg instruction".

(安此外: XCHG 等于 LOCK XCHG ,但 CMPXCHG 不等于 LOCK CMPXCHG (这实在是原子)

(An addition: XCHG equal to LOCK XCHG, but CMPXCHG doesn't equal to LOCK CMPXCHG(which is really atomic)

ARM和PowerPC的any`compare_exchange_weak()作为不同的std :: memory_orders有有型动物锁的处理器指令,通过的 LL / SC

On ARM and PowerPC for any`compare_exchange_weak() for different std::memory_orders there are differents lock's processor instructions, through LL/SC.

处理器内存屏障指令:的 http://www.cl.cam.ac.uk/~pes20/cpp/cpp0xmappings.html

Processor memory-barriers-instructions for x86(except CAS), ARM and PowerPC: http://www.cl.cam.ac.uk/~pes20/cpp/cpp0xmappings.html

推荐答案

您不应该担心什么指示编译器映射一个给定的C11构建为这不捕捉一切。相反,你需要开发code相对于C11的内存模型的保证。由于上述评论的笔记,你的编译器或将来的编译器是免费的,只要它不违反C11的内存模型来重新排序轻松内存操作。这也是一个值得通过像CDSChecker一个工具来查看内存模式下,允许哪些行为你运行code。

You shouldn't worry about what instructions the compiler maps a given C11 construct to as this doesn't capture everything. Instead you need to develop code with respect to the guarantees of the C11 memory model. As the above comment notes, your compiler or future compilers are free to reorder relaxed memory operations as long as it doesn't violate the C11 memory model. It is also a worthwhile running your code through a tool like CDSChecker to see what behaviors are allowed under the memory model.

这篇关于请在x86_64的原子CAS-操作和ARM始终使用std :: memory_order_seq_cst?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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