C ++内存模型:seq_cst加载与seq_cst存储同步吗?

时间:2017-11-27 22:15:22

标签: c++ language-lawyer atomic memory-model stdatomic

在C ++内存模型中,所有顺序一致操作的所有加载和存储都有一个总订单。我想知道这是如何与具有其他内存排序的操作交互,这些操作在顺序一致的加载之前/之后排序。

例如,考虑两个线程:

std::atomic<int> a(0);
std::atomic<int> b(0);
std::atomic<int> c(0);

//////////////
// Thread T1
//////////////

// Signal that we've started running.
a.store(1, std::memory_order_relaxed);

// If T2's store to b occurs before our load below in the total
// order on sequentially consistent operations, set flag c.
if (b.load(std::memory_order_seq_cst) == 1) {
  c.store(1, std::memory_order_relaxed)
}


//////////////
// Thread T2
//////////////

// Blindly write to b.
b.store(1, std::memory_order_seq_cst)

// Has T1 set c? If so, then we know our store to b occurred before T1's load
// in the total order on sequentially consistent operations.
if (c.load(1, std::memory_order_relaxed)) {
  // But is this guaranteed to be visible yet?
  assert(a.load(1, std::memory_order_relaxed) == 1);
}

是否可以保证T2中的断言无法触发?

我在这里寻找标准的详细引用。特别是我认为这需要显示T1 中的b的负载与T2中的商店同步到b,以便确定商店到{{1在来自a的加载之前发生了线程间的争用,但据我所知,标准说a存储与加载同步,但不是相反。

0 个答案:

没有答案