Suppose I have an atomic variable aa
and a non-atomic variable na
. Is it possible to ensure that the atomic variable aa
is read AFTER na
?
In general, an acquire memory fence prevents loads from being reordered. Will it ensure ordering also in this case?
If not, is it possible to achieve this ordering without making na
atomic and without using a strong memory fence (i.e., sequential consistency) for the reader, which is significantly more expansive?
int na{0};
atomic<int> aa{0};
void thread1(){
int l1 = na;
std::signal_memory_fence(std::memory_order_acquire);
int l2 = aa;
assert( l2==0 || l1==1 ); //does this hold?
//if reading from aa observed the latest value 1, reading from na cannot have an earlier value
}
void thread2(){
aa.store(1, std::memory_order_seq_cst);
na = 1;
}
Thanks
P.S. The main difference from the following question Re-ordering Atomic Reads is that the first variable is assumed to be non atomic.