1
votes

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 ?

2
There's no way to answer with the information given. We have no idea what semantics you require from the atomic variable. For example, does thread A write to some other memory, then write to the atomic variable, and you need to guarantee that other thread that see the write to the atomic variable see the prior writes? Or not?David Schwartz

2 Answers

9
votes

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

5
votes

Yes, it is sufficient to use memory_order_relaxed when read variable in thread A. This read will always be in happens-before relation with write, occures in the same thread, because of program order.