简体   繁体   English

在唯一写入该变量的线程中,使用memory_order_relaxed加载原子变量是否安全?

[英]Is it safe to use memory_order_relaxed to load an atomic variable, when in the only thread that writes to that variable?

I have a thread that writes to an atomic variable. 我有一个写入原子变量的线程。 (using memory_order_release). (使用memory_order_release)。 Thread A 线程A

There are many other threads that read from that variable. 还有许多其他线程从该变量读取。 (using memory_order_acquire). (使用memory_order_acquire)。 Thread B . 线程B。

Is it safe, when in thread A, to read that variable with memory_order_relaxed ? 在线程A中使用memory_order_relaxed读取该变量是否安全?

Yes, it's safe in the sense that it will behave the same as a non-atomic variable manipulated by a single thread. 是的,从某种意义上讲,它的行为与单线程操作的非原子变量相同,这是安全的。

For example, if the write-release is sequenced before the relaxed read in program order, and there are no other writes to that variable, then the relaxed read will see the value written by the write-release ( N3337 §1.10.12-13). 例如,如果写释放在程序顺序轻松读取之前测序 ,并没有其他写入该变量,然后轻松读取将看到由写入释放写入值( N3337 §1.10.12-13 )。

是的,当线程A中的读取变量时,使用memory_order_relaxed就足够了。由于程序顺序,此读取将始终与发生在之前 ,发生在同一线程中。

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

相关问题 std :: memory_order_relaxed相对于同一原子变量的原子性 - std::memory_order_relaxed atomicity with respect to the same atomic variable 使用 memory_order_relaxed 如何在典型架构上保证原子变量的修改总顺序? - With memory_order_relaxed how is total order of modification of an atomic variable assured on typical architectures? memory_order_relaxed 和 Atomic RMW 操作 - memory_order_relaxed and Atomic RMW operations .load(std :: memory_order_relaxed)的成本与读取非原子变量的成本相同吗? - Does .load(std::memory_order_relaxed) cost same as reading unatomic variable? 在此代码中使用memory_order_relaxed是否正确? - Is it correct to use memory_order_relaxed at this code? 为什么 memory_order_relaxed 在 x86 上使用原子(锁定前缀)指令? - Why does memory_order_relaxed use atomic (lock-prefixed) instructions on x86? 是防止部分读取原子存储所必需的“memory_order_relaxed” - is `memory_order_relaxed` necessary to prevent partial reads of atomic stores 对原子类感到困惑:memory_order_relaxed - confused about atomic class: memory_order_relaxed "fetch_sub 和 memory_order_relaxed 用于原子引用计数?" - fetch_sub with memory_order_relaxed for atomic reference counting? 重新排序和memory_order_relaxed - Reordering and memory_order_relaxed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM