[英]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.