If one uses a single atomic variable and std::memory_order_seq_cst
, are non-atomic operations guaranteed not to be reordered?
For example, if I have
std::atomic<bool> quux = {false};void foo() { bar(); quux.store(true, std::memory_order_seq_cst); moo();}
is bar()
guaranteed not to get reordered after the call of store
, and moo()
not to get reordered before the call of store
, as long as I use std::memory_order_seq_cst
, at least from the perspective of another thread?
Or, to put it in code, are the following assumptions valid if run from another thread?
if(quux.load(std::memory_order_seq_cst) == true) { // bar guaranteed to be called; its side-effects are visible // moo might have been called, but is not guaranteed to} else { // bar might have been called, but is not guaranteed to // moo might have been called, but is not guaranteed to}
Note that I assume that neither bar
nor moo
use atomic operations, mutexes, locks, fences or other synchronization features.