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:

  1. why have 2 options? apparent benefit?
  2. 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

Popular posts from this blog

android - Why am I getting the message 'Youractivity.java is not an activity subclass or alias' -

python - How do I create a list index that loops through integers in another list -

c# - “System.Security.Cryptography.CryptographicException: Keyset does not exist” when reading private key from remote machine -