In a multithreaded program, do you ever get any guarantees from memory_order_seq_cst
that you wouldn't also get from a weaker ordering, if you only have one thread that uses it? Example:
#include <stdatomic.h>
extern atomic_int x, y;
void doSomething(void);
void thread1(void) {
atomic_store_explicit(&x, 1, memory_order_seq_cst);
atomic_thread_fence(memory_order_seq_cst);
if(atomic_load_explicit(&y, memory_order_seq_cst)) {
doSomething();
}
}
void thread2(void) {
atomic_store_explicit(&y, 1, memory_order_release);
atomic_thread_fence(memory_order_acq_rel);
if(atomic_load_explicit(&x, memory_order_acquire)) {
doSomething();
}
}
If thread1
and thread2
are running in parallel, is it guaranteed that exactly one will always call doSomething
?
I believe no, it doesn't give any extra guarantees if only one thread uses it.
The only difference between seq-cst and acqure/release is the existence of the global seq-cst operation order.
Other threads can't observe nor influence this order without performing seq-cst operations. This order thus copies the "sequenced-before" order of the single thread that uses seq-cst operations, since nothing else constrains it.