I see that this is potentially answered in question Must I call atomic load/store explicitly?.
So for sake of clarity I will restate my question succinctly in the hopes that future readers find this clear.
Is
std::atomic<bool> b(false);
bool x = b;
Same as
std::atomic<bool> b(false);
bool x = b.load();
And
std::atomic<bool> b(false);
b = true;
Same as
std::atomic<bool> b(false);
b.store(true);
NOTE I am already aware of the fact that both variables cannot be std::atomic i.e LHS and RHS as it is not possible to read and write atomically in one instruction.
no, its not..... you need to use a locking primitive of some sort.
atomic::loadAtomically loads and returns the current value of the atomic variable. Memory is affected according to the value of order .
You need atomic<bool> to avoid race-conditions. A race-condition occurs if two threads access the same memory location, and at least one of them is a write operation. If your program contains race-conditions, the behavior is undefined.
Yes, they are the same. I think the reason the overloaded operators are provided is for convenience. Not to mention making it easier to convert existing code to use atomics.
Personally, I prefer to be explicit with load
and store
always. I think it's better practice and forces you to remember that you're dealing with an atomic.
Also, those functions allow you to specify other memory orders, which is not possible with the overloaded operator versions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With