I'm studying about memory barriers. I have some questions about following code.
//version 1
Thread A:
*val = 1;
atomic_thread_fence(memory_order_release);
atomic_store_explicit(published, 1, memory_order_relaxed);
Thread B:
if (atomic_load_explicit(published, memory_order_relaxed) == 1) {
atomic_thread_fence(memory_order_acquire);
assert(*val == 1); // will never fail
}
//version 2
/* Thread A */
*val = 1;
atomic_thread_fence(memory_order_release);
*published = 1;
/* Thread B */
if (*published == 1) {
atomic_thread_fence(memory_order_acquire);
assert(*val == 1); /* may fail */
}
- Does atomic_thread_fence only affect atomic loads/stores, and does it have any impact on the compiler or only for cpu?
- In version 2, where the store to published is non-atomic, how can it lead to a failed assertion due to the use of atomic_thread_fence, which is only meant for atomic loads/stores?
- Why is *val = 1 not written as atomic_store_explicit(val, 1, memory_order_relaxed)?