- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
考虑一个
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_relaxed
,lock
的排序部分是不必要的。
关于c++ - fetch_add with acq_rel 内存顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40649104/
考虑一个 std::atomic x(0); 假设我有一个函数执行以下操作: int x_old = x.fetch_add(1,std::memory_order_acq_rel); 基于descr
下面的代码展示了多线程编程的奇妙之处。特别是 std::memory_order_relaxed 增量与单个线程中常规增量的性能。我不明白为什么 fetch_add(relaxed) 单线程比常规增量
想象一下 N 个线程按照简单的代码运行: int res = num.fetch_add(1, std::memory_order_relaxed); 其中 num 是: std::atomic nu
据我所知,cpp11中原子类型的原子操作保证是aomtic。但是,假设在多核系统中,如果两个线程同时进行以下操作,结果会是1吗?(假设最初是atomic val=0;)看起来结果肯定是2,但为什么呢?
据我所知,cpp11中原子类型的原子操作保证是aomtic。但是,假设在多核系统中,如果两个线程同时进行以下操作,结果会是1吗?(假设最初是atomic val=0;)看起来结果肯定是2,但为什么呢?
我正在对 linux 上的 g++ 4.4.6 进行一些与原子相关的研究。我有一个简单的循环,用于估算在原子上执行 fetch_add(1) 所花费的时间。 atomic ia; ia.store(0
我正在寻找某种对原子 double 起作用的加法运算。遗憾的是: myatomdouble += toadddouble; 未定义, myatomdouble = myatomdouble + toa
我多次运行以下代码,但为什么前缀增量 fetch_add() 的结果显示正确的结果,而使用添加操作 (+),它打印错误的结果? #include #include #include using
考虑以下代码: std::atomic counter; /* otherStuff 1 */ counter.fetch_add(1, std::memory_order_relaxed); /*
该程序有时会打印 00,但如果我注释掉 a.store 和 b.store 并取消注释 a.fetch_add 和 b.fetch_add ,它们执行完全相同的操作,即都设置 a=1,b=1 的值,我
论文 N4455 No Sane Compiler Would Optimize Atomics讨论编译器可以应用于原子的各种优化。栏目下Optimization Around Atomics ,对于
我注意到在 fetch_add 的 boost::atomics 库 x86 实现(其中一个不使用编译器内部函数)中使用 add指令 lock前缀: static BOOST_FORCEINLINE
有什么区别 extern std::atomic x; int i = x++; 和 extern std::atomic x; int i = x.fetch_add(1); 我觉得第二个版本更安全
我是一名优秀的程序员,十分优秀!