- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
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
});
然后它说,
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.
问题:
评论说:“不正确:假定执行顺序”。什么是“假定执行顺序”?我很想念它。
“上面的示例取决于迭代的执行顺序”中的“迭代”指的是什么?这是否意味着while循环中的迭代?或者它指的是std::for_each
的迭代?
如果 std::for_each
的迭代由不同的线程并行执行,那么其中一个迭代/线程是否不会退出?因为 x.fetch_add(1, std::memory_order::relaxed) 是原子的,所以一个线程将使 x
1,另一个线程将使 x
> 2 并且两个线程都不可能有 x == 1。没有?
最佳答案
"incorrect: assumes execution order". What's the "assumed execution order"?
它假设 lambda 的主体由多个线程而不是一个线程执行。该标准更确切地说它可以并行执行。
What does the "iterations" refer to in "The above example depends on the order of execution of the iterations"?
它可能指的是另一个线程执行 lambda。但该标准并不保证存在另一个线程。请参阅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. 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. Any such invocations executing in the same thread are indeterminately sequenced with respect to each other.
关于c++ - C++ 标准中错误使用 std::memory_order::relaxed 的示例 [n4713 中的 algorithms.parallel.exec/5],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58287969/
这是一个关于 std::memory_order 的问题C++11 中的规则,当涉及到三个线程时。比如说,一个线程 producer 保存一个值并设置一个标志。然后,另一个线程 relay 在设置另一
我有以下 C++ 2011 代码: std::atomic x, y; std::atomic z; void f() { x.store(true, std::memory_order_rel
我读了一章,我不太喜欢它。我仍然不清楚每个内存顺序之间的区别是什么。这是我目前的猜测,在阅读了更简单的 http://en.cppreference.com/w/cpp/atomic/memory_o
据我所知,std::memory_order 枚举提供内存栅栏,但我需要确定每个 std::memory_order 枚举元素提供的栅栏。下面我按照我对每个 std::memory_order 枚举元
#include std::atomic val{1}; const auto my_order = std::memory_order_relaxed; // const lvalue int m
在像 std::atomic::compare_exchange 这样的函数中,有像 std::memory_order_release、std::memory_order_relaxed 这样的运行
作为我之前 question 的跟进, atomic类使用 memory_order 指定大多数操作范围。与栅栏相反,此内存顺序仅影响其操作的原子。据推测,通过使用几个这样的原子,您可以构建一个并发算
您能否给出一个真实世界的例子,其中出于某种原因使用了 std::atomic::compare_exchange 的两个 memory_order 参数版本(因此一个 memory_order 参数版
std::atomic_flag 有 2 个具有这些默认 std::memory_order 的函数: void clear(std::memory_order order = std::memory
std::atomic 函数,例如 store 和 load 采用 std::memory_order 参数。参数可以在运行时确定,就像任何其他函数参数一样。但是,实际值可能会影响编译期间代码的优化。
谁能解释一下 std::memory_order 是什么?用简单的英语,以及如何将它们与 std::atomic<> 一起使用? 我在这里找到了引用资料和几个例子,但根本不明白。 http://en.
根据cppreference,在C++20中有wait , notify_one , notify_all在std::atomic 。看起来他们做 std::atomic可用作futex。 我问为什么
我阅读了很多帖子并观看了几个 Youtube 视频 C++ 原子和内存模型(ConCpp 17、14)。 当我阅读Concurrency In Action一书的第 5.3.3 节,RELAXED O
似乎与成员函数不同,您无法指定要使用的 memory_order,因此可能最终使用了一些“默认”。 最佳答案 根据 C++ 标准 (§29.7/35),使用非原子调用原子类型 A 的 operator
C++ 标准中滥用 std::memory_order::relaxed 的示例之一: std::atomic x{0}; int a[] = {1,2}; std::for_each(std::ex
我是一名优秀的程序员,十分优秀!