【问题标题】:C++11 memory ordering support in GCCGCC 中的 C++11 内存排序支持
【发布时间】:2020-01-06 23:33:22
【问题描述】:

GCC 似乎不支持不同的内存排序设置,因为它为松弛、获取和顺序一致生成相同的代码。

我在 GCC 7.4 和 9.1 中尝试了以下代码:

#include <thread>
#include <atomic>

using namespace std;

atomic<int> z(0);


void Thr1()
{
  z.store(1,memory_order_relaxed);
}

void Thr2()
{
  z.store(2,memory_order_release);
}

void Thr3()
{
  z.store(3);
}

//------------------------------------------
int main (int argc, char **argv)
{
    thread t1(Thr1);
    thread t2(Thr2);
    thread t3(Thr3);
    t1.join();
    t2.join();
    t3.join();
    return 0;
}

当我为上述生成程序集时,我会为三个函数中的每一个得到以下信息:

_Z4Thr1v:
.LFB2992:
    .cfi_startproc
    pushq   %rbp
    .cfi_def_cfa_offset 16
    .cfi_offset 6, -16
    movq    %rsp, %rbp
    .cfi_def_cfa_register 6
    subq    $16, %rsp
    movl    $1, -12(%rbp)
    movl    $0, -8(%rbp)
    movl    -8(%rbp), %eax
    movl    $65535, %esi
    movl    %eax, %edi
    call    _ZStanSt12memory_orderSt23__memory_order_modifier
    movl    %eax, -4(%rbp)
    movl    -12(%rbp), %edx
    leaq    z(%rip), %rax
    movl    %edx, (%rax)
    mfence
    nop
    leave
    .cfi_def_cfa 7, 8
    ret
    .cfi_endproc
.LFE2992:
    .size   _Z4Thr1v, .-_Z4Thr1v
    .globl  _Z4Thr2v
    .type   _Z4Thr2v, @function
_Z4Thr2v:
.LFB2993:
    .cfi_startproc
    pushq   %rbp
    .cfi_def_cfa_offset 16
    .cfi_offset 6, -16
    movq    %rsp, %rbp
    .cfi_def_cfa_register 6
    subq    $16, %rsp
    movl    $2, -12(%rbp)
    movl    $3, -8(%rbp)
    movl    -8(%rbp), %eax
    movl    $65535, %esi
    movl    %eax, %edi
    call    _ZStanSt12memory_orderSt23__memory_order_modifier
    movl    %eax, -4(%rbp)
    movl    -12(%rbp), %edx
    leaq    z(%rip), %rax
    movl    %edx, (%rax)
    mfence
    nop
    leave
    .cfi_def_cfa 7, 8
    ret
    .cfi_endproc
.LFE2993:
    .size   _Z4Thr2v, .-_Z4Thr2v
    .globl  _Z4Thr3v
    .type   _Z4Thr3v, @function
_Z4Thr3v:
.LFB2994:
    .cfi_startproc
    pushq   %rbp
    .cfi_def_cfa_offset 16
    .cfi_offset 6, -16
    movq    %rsp, %rbp
    .cfi_def_cfa_register 6
    subq    $16, %rsp
    movl    $3, -12(%rbp)
    movl    $5, -8(%rbp)
    movl    -8(%rbp), %eax
    movl    $65535, %esi
    movl    %eax, %edi
    call    _ZStanSt12memory_orderSt23__memory_order_modifier
    movl    %eax, -4(%rbp)
    movl    -12(%rbp), %edx
    leaq    z(%rip), %rax
    movl    %edx, (%rax)
    mfence
    nop
    leave
    .cfi_def_cfa 7, 8
    ret

所有代码都以内存栅栏指令结束。

【问题讨论】:

    标签: c++11 gcc atomic memory-barriers


    【解决方案1】:

    如果您对性能感兴趣,那么查看未优化的机器代码将无济于事。这是gcc -O2 生成的内容:

    Thr1():
      mov DWORD PTR z[rip], 1
      ret
    Thr2():
      mov DWORD PTR z[rip], 2
      ret
    Thr3():
      mov DWORD PTR z[rip], 3
      mfence
      ret
    

    如您所见,只有顺序一致的内存顺序需要mfence

    【讨论】:

    • 是的,这更多是关于 x86 架构,而不是关于编译器。对于教育建议,查看 ARM 和 Power 的编译器输出可能会很有趣。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-11
    • 2012-11-29
    • 1970-01-01
    相关资源
    最近更新 更多