C++;原子学与交叉线可见性 < C++ >原子( >家庭提供3个好处: 基本指令不可分割(无脏读) 内存顺序(对于CPU和编译器)和 跨线程可见性/更改传播
我不确定第三个子弹,因此看一下下面的例子C++;原子学与交叉线可见性 < C++ >原子( >家庭提供3个好处: 基本指令不可分割(无脏读) 内存顺序(对于CPU和编译器)和 跨线程可见性/更改传播,c++,multithreading,c++11,atomicity,C++,Multithreading,C++11,Atomicity,我不确定第三个子弹,因此看一下下面的例子 #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 // e
#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;
}
#包括
std::atomic_bool a_flag=atomic_VAR_INIT(false);
结构数据{
int x;
长y;
字符常量*z;
}数据;
void thread0()
{
//由于“释放”,数据将写入内存
//完全按照以下顺序:x->y->z
数据x=1;
数据y=100;
data.z=“foo”;
//写入之间可能存在任意延迟
//对于任何成员,以及它在其他
//线程(不显式同步)
//atomic_bool保证写入“a_标志”
//将是干净的,因此没有其他线程会读取一些
//4位+4位的奇怪混合
存储(true,std::memory\u order\u release);
}
void thread1()
{
while(a_flag.load(std::memory_order_acquire)==false){};
//在“已发布”原子上的“获取”保证所有写入
//thread0(因此数据成员修改)将在此处可见
}
无效线程2()
{
而(data.y!=100){};
//不“获取”a_标志并不保证会看到所有
//内存写入,但当我看到z==100时,我知道我可以假设
//由于“发布顺序”=>assert(x==1),以前的写操作已经完成
}
int main()
{
thread0();//并发
thread1();//并发
thread2();//并发
//加入
返回0;
}
首先,请用代码验证我的假设(尤其是thread2
)
第二,我的问题是:
a_标志
write如何传播到其他内核std::atomic
是否将写入程序缓存中的a_标志
与其他内核缓存同步(使用MESI或任何其他方法),或者传播是自动的a_flag
形成的获取-释放对,它是一个同步点,导致thread0
和thread1
的效果以特定顺序出现(即,存储之前thread0
中的所有内容发生在thread1
中循环之后的所有内容之前)thread2
放在代码中会导致整个程序的行为未定义只是为了好玩,并且为了说明了解自己正在发生的事情是有启发性的,我将代码编译为三种变体(我添加了一个glbbal
intx
,并在thread1
中添加了x=data.y;
)
获取/发布:(您的代码)
顺序一致:(删除显式排序)
“天真”:(只需使用bool
)
正如您所看到的,没有太大区别。“不正确”版本实际上看起来基本正确,只是缺少加载(它使用cmp
和内存操作数).顺序一致的版本在xggh
指令中隐藏了它的开销,该指令具有隐式锁前缀,并且似乎不需要任何显式围栏
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
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