【问题标题】:C++ atomics and cross-thread visibilityC++ 原子和跨线程可见性
【发布时间】:2013-10-17 07:59:20
【问题描述】:

AFAIK C++ atomics (<atomic>) 系列提供 3 个好处:

  • 原始指令不可分割(无脏读),
  • 内存排序(用于 CPU 和编译器)和
  • 跨线程可见性/更改传播。

我不确定第三个项目符号,因此请看以下示例。

#include <atomic>

std::atomic_bool a_flag = ATOMIC_VAR_INIT(false);
struct Data {
    int x;
    long long y;
    char const* z;
} data;

void thread0()
{
    // due to "release" the data will be written to memory
    // exactly in the following order: x -> y -> z
    data.x = 1;
    data.y = 100;
    data.z = "foo";
    // there can be an arbitrary delay between the write 
    // to any of the members and it's visibility in other 
    // threads (which don't synchronize explicitly)

    // atomic_bool guarantees that the write to the "a_flag"
    // will be clean, thus no other thread will ever read some
    // strange mixture of 4bit + 4bits
    a_flag.store(true, std::memory_order_release);
}

void thread1()
{
    while (a_flag.load(std::memory_order_acquire) == false) {};
    // "acquire" on a "released" atomic guarantees that all the writes from 
    // thread0 (thus data members modification) will be visible here
}

void thread2()
{
    while (data.y != 100) {};
    // not "acquiring" the "a_flag" doesn't guarantee that will see all the 
    // memory writes, but when I see the z == 100 I know I can assume that 
    // prior writes have been done due to "release ordering" => assert(x == 1)
}

int main()
{
    thread0(); // concurrently
    thread1(); // concurrently
    thread2(); // concurrently

    // join

    return 0;
}

首先,请在代码中验证我的假设(尤其是thread2)。

其次,我的问题是:

  1. a_flag 写入如何传播到其他内核?

  2. std::atomic 是否将写入器缓存中的 a_flag 与其他核心缓存(使用 MESI 或其他任何东西)同步,或者传播是自动的?

  3. 假设在特定机器上对标志的写入是原子的(想想 x86 上的 int_32)并且我们没有任何私有内存要同步(我们只有一个标志)我们需要使用原子吗?

  4. 考虑到最流行的 CPU 架构(x86、x64、ARM v.whatever、IA-64),跨核心可见性是自动的(我现在考虑重新排序) (但可能会延迟),或者您需要发出特定命令来传播任何数据?

【问题讨论】:

    标签: c++ multithreading c++11 atomic


    【解决方案1】:
    1. 核心本身并不重要。问题是“所有内核如何最终看到相同的内存更新”,这是你的硬件为你做的事情(例如缓存一致性协议)。只有一个内存,所以主要关注的是缓存,这是硬件的私人关注。

    2. 这个问题似乎不清楚。重要的是a_flag的加载和存储形成的acquire-release对,这是一个同步点,导致thread0thread1的效果以一定的顺序出现(即thread0之前的所有内容)存储 happens-before thread1 中循环之后的所有内容。

    3. 是的,否则您将没有同步点。

    4. 在 C++ 中不需要任何“命令”。 C++ 甚至不知道它运行在任何特定类型的 CPU 上。您可能有足够的想象力在魔方上运行 C++ 程序。 C++ 编译器 选择必要的指令来实现由 C++ 内存模型描述的同步行为,在 x86 上涉及发出指令锁定前缀和内存栅栏,以及不会过多地重新排序指令。由于 x86 有一个强有序的内存模型,与没有原子的天真、不正确的代码相比,上面的代码应该产生最少的额外代码。

    5. 在代码中包含您的thread2 会使整个程序的行为未定义。


    只是为了好玩,并且为了表明自己了解正在发生的事情可能会有所启发,我将代码编译成三种变体。 (我添加了一个 glbbal int xthread1 我添加了 x = data.y;)。

    获取/发布:(您的代码)

    thread0:
        mov DWORD PTR data, 1
        mov DWORD PTR data+4, 100
        mov DWORD PTR data+8, 0
        mov DWORD PTR data+12, OFFSET FLAT:.LC0
        mov BYTE PTR a_flag, 1
        ret
    
    thread1:
    .L14:
        movzx   eax, BYTE PTR a_flag
        test    al, al
        je  .L14
        mov eax, DWORD PTR data+4
        mov DWORD PTR x, eax
        ret
    

    顺序一致:(去掉显式排序)

    thread0:
        mov eax, 1
        mov DWORD PTR data, 1
        mov DWORD PTR data+4, 100
        mov DWORD PTR data+8, 0
        mov DWORD PTR data+12, OFFSET FLAT:.LC0
        xchg    al, BYTE PTR a_flag
        ret
    
    thread1:
    .L14:
        movzx   eax, BYTE PTR a_flag
        test    al, al
        je  .L14
        mov eax, DWORD PTR data+4
        mov DWORD PTR x, eax
        ret
    

    “天真”:(仅使用bool

    thread0:
        mov DWORD PTR data, 1
        mov DWORD PTR data+4, 100
        mov DWORD PTR data+8, 0
        mov DWORD PTR data+12, OFFSET FLAT:.LC0
        mov BYTE PTR a_flag, 1
        ret
    
    thread1:
        cmp BYTE PTR a_flag, 0
        jne .L3
    .L4:
        jmp .L4
    .L3:
        mov eax, DWORD PTR data+4
        mov DWORD PTR x, eax
        ret
    

    如您所见,差别不大。 “不正确”的版本实际上看起来大部分是正确的,除了缺少负载(它使用带有内存操作数的cmp)。顺序一致的版本在 xcgh 指令中隐藏了它的昂贵性,该指令具有隐式锁定前缀,并且似乎不需要任何显式围栏。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-05
      • 2020-12-08
      • 2021-09-20
      • 1970-01-01
      • 2021-05-13
      • 1970-01-01
      • 2014-11-30
      • 1970-01-01
      相关资源
      最近更新 更多