[英]Understanding C++ memory model : Different values on different runs
What is wrong with the below code? 以下代码有什么问题? I expect to see 10 to be produced by consumer1 and consumer2, but I see -1 sometimes. 我希望看到10由consumer1和consumer2生成,但我有时会看到-1。
#include <thread>
#include <atomic>
#include <cassert>
#include <string>
std::atomic<int> global;
void producer()
{
global.store(10, std::memory_order_release);
}
void consumer1()
{
int a = global.load(std::memory_order_acquire);
printf("a in consumer1 %d\n", a);
}
void consumer2()
{
int a = global.load(std::memory_order_acquire);
printf("a in consumer2 %d\n", a);
}
int main()
{
global.store(-1, std::memory_order_seq_cst);
std::thread t1(producer);
std::thread t2(consumer1);
std::thread t3(consumer2);
t1.join(); t2.join(); t3.join();
}
I see a in consumer1 10
a in consumer2 10
and a in consumer1 -1
a in consumer2 10
我看到a in consumer1 10
a in consumer2 10
和a in consumer1 -1
a in consumer2 10
If I understand correctly, the thread which does memory_order_acquire
always syncs with the thread which does memory_order_release
. 如果我理解正确,执行memory_order_acquire
的线程总是与执行memory_order_release
的线程同步。 Am I wrong? 我错了吗? I am running on x86-64 bit machine. 我在x86-64位机器上运行。 I am compiling with g++ file.cpp -pthread -std=c++11 我正在使用g ++ file.cpp -pthread -std = c ++ 11进行编译
Atomic variables have the very nice property that the value read is a value that has been written before. 原子变量具有非常好的属性,读取的值是之前写入的值。 And with release/acquire semantics it's even the last value written. 通过发布/获取语义,它甚至可以写入最后一个值。
In this case, you have 2 writes and two reads. 在这种情况下,您有2次写入和2次读取。 Only the write of -1 is sequenced-before the reads, the write of 10 is not sequenced. 只有-1的写入被排序 - 在读取之前,10的写入未被排序。 Therefore either value may be the last written. 因此,任何一个值都可能是最后写的。 It's guaranteed that you read -1 or 10 and not garbage. 它保证你读-1或10而不是垃圾。
If you add a sleep before 如果您之前添加了睡眠
global.store(10, std::memory_order_release);
then you can observe consistently a -1. 那么你可以持续观察-1。
The key point is that std::memory_order is not a sempaphore-like syncronization, but a rather more subtle matter. 关键点在于std :: memory_order不是类似sempaphore的同步,而是一个相当微妙的问题。 See cppreference 见cppreference
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.