我在cppreference中找到了一个程序:
std::atomic<int> x{0};
int a[] = {1,2};
std::for_each(std::execution::par, std::begin(a), std::end(a), [&](int) {
x.fetch_add(1, std::memory_order_relaxed);
while (x.load(std::memory_order_relaxed) == 1) { } // Error: assumes execution order
});
“错误:假定执行顺序”一词是什么意思,即该程序的错误是什么?该程序似乎旨在显示僵局,但我看不到。
我知道标题不清楚,但是我真的不知道如何描述问题,因为我找不到程序中的任何错误。对于那个很抱歉。
这个例子来自草稿(例如N4606 [algorithms.parallel.exec] / 3):
[ 例子:
std::atomic<int> x{0};
int a[] = {1,2};
std::for_each(std::execution::par, std::begin(a), std::end(a), [&](int) {
x.fetch_add(1, std::memory_order_relaxed);
// spin wait for another iteration to change the value of x
while (x.load(std::memory_order_relaxed) == 1) { } // incorrect: assumes execution order
});
上面的示例取决于迭代的执行顺序,并且如果两个迭代均在同一执行线程上依次执行,则该实例不会终止。—结束示例]
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句