For example if we have two std::atomic
s and want to read value from first and then flag second that we don't need value of first anymore. We don't want these operations to be reordered (otherwise first value can be rewritten before we read it), but there is no data dependency between operations, so we definetely need a barrier to prevent reordering (and memory_order_consume
doesn't fit).
Full fence is certainly overkill here. Also we don't need neither release nor acquire semantic (even if they provide such barrier). All we need is just preserving order of read-and-then-write operations.
Is there some cheap fence that does what we need?
EDIT: examples of what I need.
std::atomic<X> atomicVal;
std::atomic<bool> atomicFlag = false;
...
auto value = atomicVal.load(std::memory_order_relaxed);
some_appropriative_fence();
atomicFlag.store(true, std::memory_order_relaxed);
And after atomicFlag
is set atomicVal
can be overwritten to some further value, so we need to read it before.
Of course we can make
auto value = atomicVal.load(std::memory_order_relaxed);
std::atomic_thread_fence(std::memory_order_seq_cst);
atomicFlag.store(true, std::memory_order_relaxed);
but it will be too expensive for operation we need.
I'm interesting what minimal fence is enough to guarantee order of operations.