c++ - Is assignment equivalent to load/store for std::atomic<bool> -
i see potentially answered in question must call atomic load/store explicitly?.
so sake of clarity restate question succinctly in hopes future readers find clear.
is
std::atomic<bool> b(false); bool x = b;
same
std::atomic<bool> b(false); bool x = b.load();
and
std::atomic<bool> b(false); b = true;
same
std::atomic<bool> b(false); b.store(true);
if indeed case then:
- why have 2 options? apparent benefit?
- is practice when dealing atomics prefer more verbose load()/store() on potentially confusing assignment(=) mean either depending on whether lhs or rhs atomic.
note aware of fact both variables cannot std::atomic i.e lhs , rhs not possible read , write atomically in 1 instruction.
yes, same. think reason overloaded operators provided convenience. not mention making easier convert existing code use atomics.
personally, prefer explicit load
, store
always. think it's better practice , forces remember you're dealing atomic.
also, functions allow specify other memory orders, not possible overloaded operator versions.
Comments
Post a Comment