Given two threads A and B I have the following logic:
std::atomic<int> x{0};
std::atomic<int> y{0};
Thread A:
int y_val = y.load(std::memory_order_acquire);
if (y_val != 1) {
int old_x = x.exchange(1, std::memory_order_seq_cst);
y_val = y.load(std::memory_order_acquire);
}
Thread B:
y.store(1, std::memory_order_release);
std::atomic_thread_fence(std::memory_order_seq_cst);
int x_val = x.load(std::memory_order_relaxed);
Is it possible that after both threads execute code above y_val==0
and x_val==0
?
I understand that second load from y
can be potentially reordered with store in exchange. But I am not sure if fence can occur between load and store in exchange.