简体   繁体   English

在 C++ 标准 [algorithms.parallel.exec/5 in n4713] 中滥用 std::memory_order::relaxed 的示例

[英]Example of misuse of std::memory_order::relaxed in C++ Standard [algorithms.parallel.exec/5 in n4713]

One of the examples of misuse of std::memory_order::relaxed in C++ Standard:在 C++ 标准中滥用std::memory_order::relaxed的例子之一:

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
});

And then it says,然后它说,

The above example depends on the order of execution of the iterations, and will not terminate if both iterations are executed sequentially on the same thread of execution.上面的例子取决于迭代的执行顺序,如果两个迭代在同一个执行线程上按顺序执行,则不会终止。

Questions:问题:

  1. The comment says, "incorrect: assumes execution order".评论说,“不正确:假定执行顺序”。 What's the "assumed execution order"?什么是“假定的执行顺序”? I miss it.我想念它。

  2. What does the "iterations" refer to in "The above example depends on the order of execution of the iterations"? “上面的例子取决于迭代的执行顺序”中的“迭代”指的是什么? Does it mean the iteration in while loop?这是否意味着while循环中的迭代? Or does it refer to the iteration of std::for_each ?还是它指的是std::for_each的迭代?

  3. If the iterations of std::for_each are executed in parallel by different threads, isn't it still true that one of the iterations/threads won't exit?如果std::for_each的迭代由不同的线程并行执行,那么迭代/线程之一不会退出是否仍然正确? Because x.fetch_add(1, std::memory_order::relaxed) is atomic and so one thread will make x 1 and another will make x 2 and it is impossible to have x == 1 for both thread.因为x.fetch_add(1, std::memory_order::relaxed)是原子的,所以一个线程将使x 1 而另一个线程将使x 2 并且不可能有 x == 1 两个线程。 No?不?

"incorrect: assumes execution order". “不正确:假定执行顺序”。 What's the "assumed execution order"?什么是“假定的执行顺序”?

It assumes that the body of the lambda gets executed by multiple threads rather than one.它假设 lambda 的主体由多个线程而不是一个线程执行。 The standard rather says that it may execute in parallel.该标准宁愿说它可以并行执行。

What does the "iterations" refer to in "The above example depends on the order of execution of the iterations"? “上面的例子取决于迭代的执行顺序”中的“迭代”指的是什么?

It probably refers to the execution of the lambda by another thread.它可能是指另一个线程执行lambda。 But the standard doesn't guarantee that there is another thread.但标准不保证有另一个线程。 See execution_policy_tag_t :execution_policy_tag_t

parallel_policy The execution policy type used as a unique type to disambiguate parallel algorithm overloading and indicate that a parallel algorithm's execution may be parallelized. parallel_policy执行策略类型,用作消除并行算法重载的歧义并指示并行算法的执行可以并行化的唯一类型。 The invocations of element access functions in parallel algorithms invoked with this policy (usually specified as std::execution::par) are permitted to execute in either the invoking thread or in a thread implicitly created by the library to support parallel algorithm execution.使用此策略调用的并行算法中元素访问函数的调用(通常指定为 std::execution::par)被允许在调用线程或由库隐式创建的线程中执行以支持并行算法执行。 Any such invocations executing in the same thread are indeterminately sequenced with respect to each other.在同一线程中执行的任何此类调用相对于彼此的顺序是不确定的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM