<bdo id='UzABe'></bdo><ul id='UzABe'></ul>

  1. <legend id='UzABe'><style id='UzABe'><dir id='UzABe'><q id='UzABe'></q></dir></style></legend>
  2. <tfoot id='UzABe'></tfoot>
  3. <i id='UzABe'><tr id='UzABe'><dt id='UzABe'><q id='UzABe'><span id='UzABe'><b id='UzABe'><form id='UzABe'><ins id='UzABe'></ins><ul id='UzABe'></ul><sub id='UzABe'></sub></form><legend id='UzABe'></legend><bdo id='UzABe'><pre id='UzABe'><center id='UzABe'></center></pre></bdo></b><th id='UzABe'></th></span></q></dt></tr></i><div id='UzABe'><tfoot id='UzABe'></tfoot><dl id='UzABe'><fieldset id='UzABe'></fieldset></dl></div>
    1. <small id='UzABe'></small><noframes id='UzABe'>

      原子的 C++ 内存屏障

      C++ Memory Barriers for Atomics(原子的 C++ 内存屏障)
          <i id='A1wxi'><tr id='A1wxi'><dt id='A1wxi'><q id='A1wxi'><span id='A1wxi'><b id='A1wxi'><form id='A1wxi'><ins id='A1wxi'></ins><ul id='A1wxi'></ul><sub id='A1wxi'></sub></form><legend id='A1wxi'></legend><bdo id='A1wxi'><pre id='A1wxi'><center id='A1wxi'></center></pre></bdo></b><th id='A1wxi'></th></span></q></dt></tr></i><div id='A1wxi'><tfoot id='A1wxi'></tfoot><dl id='A1wxi'><fieldset id='A1wxi'></fieldset></dl></div>
          • <tfoot id='A1wxi'></tfoot>
              <tbody id='A1wxi'></tbody>

            <small id='A1wxi'></small><noframes id='A1wxi'>

          • <legend id='A1wxi'><style id='A1wxi'><dir id='A1wxi'><q id='A1wxi'></q></dir></style></legend>

              <bdo id='A1wxi'></bdo><ul id='A1wxi'></ul>
                本文介绍了原子的 C++ 内存屏障的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我是新手.任何人都可以提供以下内存屏障之间差异的简化解释吗?

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

                • 窗口 MemoryBarrier();
                • 围栏_mm_mfence();
                • 内联汇编asm volatile(""::"memory");
                • 内在的_ReadWriteBarrier();

                如果没有简单的解释,一些指向好文章或书籍的链接可能会帮助我弄清楚.到现在为止,我只使用其他人编写的对象来包装这些调用还可以,但我想比我目前的想法有更好的理解,这基本上是在幕后实现内存屏障的方法不止一种.

                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.

                推荐答案

                MemoryBarrier (MSVC) 和 _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 分别使用 xchglock 或 实现,我看到有人声称这比 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") 和 MSVC 中的 _ReadWriteBarrier 仅提供编译器级别的内存栅栏,防止编译器重新排序内存访问.这意味着处理器仍然可以自由地进行重新排序.

                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++ 内存屏障的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Consistent pseudo-random numbers across platforms(跨平台一致的伪随机数)
                Vary range of uniform_int_distribution(改变uniform_int_distribution的范围)
                What is a seed in terms of generating a random number?(就生成随机数而言,种子是什么?)
                Is 1.0 a valid output from std::generate_canonical?(1.0 是 std::generate_canonical 的有效输出吗?)
                Getting big random numbers in C/C++(在 C/C++ 中获取大随机数)
                What is the best way to generate random numbers in C++?(在 C++ 中生成随机数的最佳方法是什么?)
                <tfoot id='BbBFg'></tfoot>

                • <i id='BbBFg'><tr id='BbBFg'><dt id='BbBFg'><q id='BbBFg'><span id='BbBFg'><b id='BbBFg'><form id='BbBFg'><ins id='BbBFg'></ins><ul id='BbBFg'></ul><sub id='BbBFg'></sub></form><legend id='BbBFg'></legend><bdo id='BbBFg'><pre id='BbBFg'><center id='BbBFg'></center></pre></bdo></b><th id='BbBFg'></th></span></q></dt></tr></i><div id='BbBFg'><tfoot id='BbBFg'></tfoot><dl id='BbBFg'><fieldset id='BbBFg'></fieldset></dl></div>

                        <tbody id='BbBFg'></tbody>
                      <legend id='BbBFg'><style id='BbBFg'><dir id='BbBFg'><q id='BbBFg'></q></dir></style></legend>
                      • <bdo id='BbBFg'></bdo><ul id='BbBFg'></ul>

                        <small id='BbBFg'></small><noframes id='BbBFg'>