【发布时间】:2016-07-19 02:59:01
【问题描述】:
std::atomic<int> cnt = {2};
thread 1:
doFoo();
if (cnt.fetch_sub(1, std::memory_order_relaxed) == 1) {
doBazz();
}
thread 2:
doBar();
if (cnt.fetch_sub(1, std::memory_order_relaxed) == 1) {
doBazz();
}
我们能保证doFoo() 和doBar() 总是在doBazz() 之前发生吗?
【问题讨论】:
-
您正在使用 memory_order_relaxed,这意味着没有执行内存排序。没有这样的保证。
标签: c++ multithreading c++11 atomic