简体   繁体   中英

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). Thread A

There are many other threads that read from that variable. (using memory_order_acquire). Thread B .

Is it safe, when in thread A, to read that variable with 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).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM