Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/124.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 从C+中的CPPPreference解释多线程示例+;_C++_Multithreading_C++17 - Fatal编程技术网

C++ 从C+中的CPPPreference解释多线程示例+;

C++ 从C+中的CPPPreference解释多线程示例+;,c++,multithreading,c++17,C++,Multithreading,C++17,我在以下方面找到了一个程序: std::原子x{0}; int a[]={1,2}; std::for_each(std::execution::par,std::begin(a),std::end(a),[&](int){ x、 获取添加(1,标准::内存顺序松弛); while(x.load(std::memory_order_released)==1{}//错误:假定执行顺序 }); “错误:假定执行顺序”一词是什么意思,即该程序的错误是什么?看起来这个程序的目的是要显示死锁,但我看不出

我在以下方面找到了一个程序:

std::原子x{0};
int a[]={1,2};
std::for_each(std::execution::par,std::begin(a),std::end(a),[&](int){
x、 获取添加(1,标准::内存顺序松弛);
while(x.load(std::memory_order_released)==1{}//错误:假定执行顺序
});
“错误:假定执行顺序”一词是什么意思,即该程序的错误是什么?看起来这个程序的目的是要显示死锁,但我看不出来

我知道标题不清楚,但我真的不知道如何描述这个问题,因为我在程序中找不到任何错误。很抱歉。

此示例来自:

[示例:

std::原子x{0};
int a[]={1,2};
std::for_each(std::execution::par,std::begin(a),std::end(a),[&](int){
x、 获取添加(1,标准::内存顺序松弛);
//旋转等待另一次迭代以更改x的值
while(x.load(std::memory_order_released)==1{}//不正确:假定执行顺序
});
上述示例取决于迭代的执行顺序,如果两个迭代都在同一执行线程上顺序执行,则不会终止。-结束示例]


这是C++17的问题?如果是,请添加标签。@hyde OK。执行策略在C++17中是新的,但我认为这只是一个多线程问题,与新特性没有什么关系。代码假设两个lambda调用将并行执行。但是
std::execution::par
只说它们可以并行执行。如果它们没有被调用——如果它们最终在同一个线程上按顺序被调用——那么程序将进入一个无限循环。
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
});
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
});