gpt4 book ai didi

c++ - fetch_add with acq_rel 内存顺序

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:18:59 25 4
gpt4 key购买 nike

考虑一个

std::atomic<int> x(0);

假设我有一个函数执行以下操作:

int x_old = x.fetch_add(1,std::memory_order_acq_rel);

基于description for acquire release memory ordering :

memory_order_relaxed Relaxed operation: there are no synchronization or ordering constraints, only atomicity is required of this operation (see Relaxed ordering below)

memory_order_consume A load operation with this memory order performs a consume operation on the affected memory location: no reads or writes in the current thread dependent on the value currently loaded can be reordered before this load. Writes to data-dependent variables in other threads that release the same atomic variable are visible in the current thread. On most platforms, this affects compiler optimizations only (see Release-Consume ordering below)

memory_order_acquire A load operation with this memory order performs the acquire operation on the affected memory location: no reads or writes in the current thread can be reordered before this load. All writes in other threads that release the same atomic variable are visible in the current thread (see Release-Acquire ordering below)

memory_order_release A store operation with this memory order performs the release operation: no reads or writes in the current thread can be reordered after this store. All writes in the current thread are visible in other threads that acquire the same atomic variable (see Release-Acquire ordering below) and writes that carry a dependency into the atomic variable become visible in other threads that consume the same atomic (see Release-Consume ordering below).

memory_order_acq_rel A read-modify-write operation with this memory order is both an acquire operation and a release operation. No memory reads or writes in the current thread can be reordered before or after this store. All writes in other threads that release the same atomic variable are visible before the modification and the modification is visible in other threads that acquire the same atomic variable.

memory_order_seq_cst Any operation with this memory order is both an acquire operation and a release operation, plus a single total order exists in which all threads observe all modifications in the same order (see Sequentially-consistent ordering below)

是否有可能 2 个不同的线程接收相同的 x_old 值 0?或者它们是否保证以 x_old 只有其中一个为 0 而另一个为 1 的方式执行。

如果 x_old 对它们都为 0,将内存顺序更改为 std::memory_order_seq_cst 是否保证 x_old ?

最佳答案

Is it possible for 2 distinct threads to receive the same x_old value of 0?

这是不可能的,因为操作是原子的。它要么全部发生,要么根本不发生。

排序与前面/后面的加载/存储有关,因为您没有任何东西,所以排序在这里无关紧要。换句话说,x.fetch_add(1, std::memory_order_relaxed); 在这里有同样的效果。

在当前的 x86 上,无论 memory_order 是相同的 lock xadd 指令,lock 前缀同时提供原子性和顺序。对于 memory_order_relaxedlock 的排序部分是不必要的。

关于c++ - fetch_add with acq_rel 内存顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40649104/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com