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

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

  1. <legend id='XCCH9'><style id='XCCH9'><dir id='XCCH9'><q id='XCCH9'></q></dir></style></legend>

      <tfoot id='XCCH9'></tfoot>

        <bdo id='XCCH9'></bdo><ul id='XCCH9'></ul>
    1. 为什么 GCC 不使用 LOAD(无栅栏)和 STORE+SFENCE 来实现顺序一致性?

      时间:2023-12-02
    2. <i id='ji6GG'><tr id='ji6GG'><dt id='ji6GG'><q id='ji6GG'><span id='ji6GG'><b id='ji6GG'><form id='ji6GG'><ins id='ji6GG'></ins><ul id='ji6GG'></ul><sub id='ji6GG'></sub></form><legend id='ji6GG'></legend><bdo id='ji6GG'><pre id='ji6GG'><center id='ji6GG'></center></pre></bdo></b><th id='ji6GG'></th></span></q></dt></tr></i><div id='ji6GG'><tfoot id='ji6GG'></tfoot><dl id='ji6GG'><fieldset id='ji6GG'></fieldset></dl></div>
    3. <small id='ji6GG'></small><noframes id='ji6GG'>

      <tfoot id='ji6GG'></tfoot>
        <tbody id='ji6GG'></tbody>
              <bdo id='ji6GG'></bdo><ul id='ji6GG'></ul>
              <legend id='ji6GG'><style id='ji6GG'><dir id='ji6GG'><q id='ji6GG'></q></dir></style></legend>

                本文介绍了为什么 GCC 不使用 LOAD(无栅栏)和 STORE+SFENCE 来实现顺序一致性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                以下是在 x86/x86_64 中实现顺序一致性的四种方法:

                Here are four approaches to make Sequential Consistency in x86/x86_64:

                1. LOAD(无围栏)和 STORE+MFENCE
                2. LOAD(无围栏)和LOCK XCHG
                3. MFENCE+LOAD 和 STORE(无围栏)
                4. LOCK XADD(0) 和 STORE(无围栏)

                正如此处所写:http://www.cl.cam.ac.英国/~pes20/cpp/cpp0xmappings.html

                C/C++11 操作 x86 实现

                C/C++11 Operation x86 implementation

                • 加载 Seq_Cst:MOV(从内存中)
                • Store Seq Cst: (LOCK) XCHG//替代方案:MOV(进入内存),MFENCE

                注意:有一个 C/C++11 到 x86 的替代映射,它不是锁定(或隔离)Seq Cst 存储锁定/隔离 Seq Cst 负载:

                Note: there is an alternative mapping of C/C++11 to x86, which instead of locking (or fencing) the Seq Cst store locks/fences the Seq Cst load:

                • 加载 Seq_Cst:LOCK XADD(0)//替代:MFENCE,MOV(从内存中)
                • 存储 Seq Cst:MOV(到内存中)

                GCC 4.8.2(x86_64 中的 GDB) 对 使用 first(1) 方法C++11-std::memory_order_seq_cst,即LOAD(无围栏)和STORE+MFENCE:

                GCC 4.8.2(GDB in x86_64) uses first(1) approach for C++11-std::memory_order_seq_cst, i.e. LOAD(without fence) and STORE+MFENCE:

                std::atomic<int> a;
                int temp = 0;
                a.store(temp, std::memory_order_seq_cst);
                0x4613e8  <+0x0058>         mov    0x38(%rsp),%eax
                0x4613ec  <+0x005c>         mov    %eax,0x20(%rsp)
                0x4613f0  <+0x0060>         mfence
                

                我们知道,MFENCE = LFENCE+SFENCE.那么这段代码我们可以改写成这样:LOAD(without fence) and STORE+LFENCE+SFENCE

                As we know, that MFENCE = LFENCE+SFENCE. Then this code we can rewrite to this: LOAD(without fence) and STORE+LFENCE+SFENCE

                问题:

                1. 为什么我们这里不需要在 LOAD 之前使用 LFENCE,而需要在 STORE 之后使用 LFENCE(因为 LFENCE 只有在 LOAD 之前才有意义!)?
                2. 为什么 GCC 不对 std::memory_order_seq_cst 使用方法:LOAD(无栅栏)和 STORE+SFENCE?

                推荐答案

                x86 唯一的重新排序(对于正常内存访问)是它可以潜在地重新排序跟随存储的加载.

                The only reordering x86 does (for normal memory accesses) is that it can potentially reorder a load that follows a store.

                SFENCE 保证围栏之前的所有商店在围栏之后的所有商店之前完成.LFENCE 保证围栏之前的所有加载在围栏之后的所有加载之前完成.对于正常的内存访问,默认情况下已经提供了单个 SFENCE 或 LFENCE 操作的排序保证.基本上,LFENCE 和 SFENCE 本身只对 x86 较弱的内存访问模式有用.

                SFENCE guarantees that all stores before the fence complete before all stores after the fence. LFENCE guarantees that all loads before the fence complete before all loads after the fence. For normal memory accesses, the ordering guarantees of individual SFENCE or LFENCE operations are already provided by default. Basically, LFENCE and SFENCE by themselves are only useful for the weaker memory access modes of x86.

                LFENCE、SFENCE 或 LFENCE + SFENCE 都不会阻止重新排序后跟加载的存储.MFENCE 有.

                Neither LFENCE, SFENCE, nor LFENCE + SFENCE prevents a store followed by a load from being reordered. MFENCE does.

                相关参考是 Intel x86 架构手册.

                The relevant reference is the Intel x86 architectural manual.

                这篇关于为什么 GCC 不使用 LOAD(无栅栏)和 STORE+SFENCE 来实现顺序一致性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:GCC 5.4.0 的昂贵跳跃 下一篇:列出 C/C++ 函数(Unix 中的代码分析)

                相关文章

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

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

                1. <legend id='gV0Uh'><style id='gV0Uh'><dir id='gV0Uh'><q id='gV0Uh'></q></dir></style></legend>
                  • <bdo id='gV0Uh'></bdo><ul id='gV0Uh'></ul>

                  1. <tfoot id='gV0Uh'></tfoot>