C ++内存障碍的原子 [英] C++ Memory Barriers for Atomics

查看:259
本文介绍了C ++内存障碍的原子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个新手,当谈到这个。

I'm a newbie when it comes to this. Could anyone provide a simplified explanation of the differences between the following memory barriers?


  • Windows MemoryBarrier();

  • 围栏 _mm_mfence();

  • asm volatile(:::memory);

  • _ReadWriteBarrier / code>

  • The windows MemoryBarrier();
  • The fence _mm_mfence();
  • The inline assembly asm volatile ("" : : : "memory");
  • The intrinsic _ReadWriteBarrier();

如果没有一个简单的解释,一些链接到好的文章或书籍可能会帮助我直行。到现在为止,我很好,只是使用由其他人包装这些调用写的对象,但我想有一个更好的理解,比我目前的想法,这是基本上沿着线有多种方法来实现内存屏障下的内存。 MemoryBarrier (MSVC)和

If there isn't a simple explanation some links to good articles or books would probably help me get it straight. Until now I was fine with just using objects written by others wrapping these calls but I'd like to have a better understanding than my current thinking which is basically along the lines of there is more than one way to implement memory barriers under the covers.

推荐答案

_mm_mfence (由多个编译器支持)提供了一个硬件内存围栏,防止处理器在栅栏上移动读写操作。

Both MemoryBarrier (MSVC) and _mm_mfence (supported by several compilers) provide a hardware memory fence, which prevents the processor from moving reads and writes across the fence.

区别在于MemoryBarrier对于x86,x64和IA64具有特定于平台的实现,其中_mm_mfence特别使用 mfence SSE2指令,因此它不总是可用。

The main difference is that MemoryBarrier has platform specific implementations for x86, x64 and IA64, where as _mm_mfence specifically uses the mfence SSE2 instruction, so it's not always available.

在x86和x64上,MemoryBarrier分别使用 xchg 锁定或我已经看到一些声称这比mfence更快。但是我自己的基准显示了相反的情况,所以显然它非常依赖处理器模型。

On x86 and x64 MemoryBarrier is implemented with a xchg and lock or respectively, and I have seen some claims that this is faster than mfence. However my own benchmarks show the opposite, so apparently it's very much dependent on processor model.

另一个区别是mfence也可以用于排序非暂时存储( movntq 等)。

Another difference is that mfence can also be used for ordering non-temporal stores/loads (movntq etc).

GCC也有 __ sync_synchronize 这将产生硬件围栏。

GCC also has __sync_synchronize which generates a hardware fence.

asm volatile(:::memory) code> _ReadWriteBarrier 在MSVC中只提供一个编译器级的内存范围,防止编译器重新排序内存访问。这意味着处理器仍然可以自由地进行重新排序。

asm volatile ("" : : : "memory") in GCC and _ReadWriteBarrier in MSVC only provide a compiler level memory fence, preventing the compiler from reordering memory accesses. That means the processor is still free to do reordering.

编译器栅栏通常与具有某种隐式硬件围栏的操作结合使用。例如。在x86 / x64上所有商店都有一个释放围栏,加载有一个获取围栏,所以你只需要一个编译器围栏实现负载获取和存储释放。

Compiler fences are generally used in combination with operations that have some kind of implicit hardware fence. E.g. on x86/x64 all stores have a release fence and loads have an acquire fence, so you just need a compiler fence when implementing load-acquire and store-release.

这篇关于C ++内存障碍的原子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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