Search code examples
c++c++11atomicstdatomic

Must I call atomic load/store explicitly?


C++11 introduced the std::atomic<> template library. The standard specifies the store() and load() operations to atomically set / get a variable shared by more than one thread.

My question is: are assignment and access operations also atomic? Namely, given

std::atomic<bool> stop(false);
  • Is !stop.load() equivalent to !stop?
  • Is stop.store(true) equivalent to stop = true?

Solution

  • Are assignment and access operations for non-reference types also atomic?

    Yes, they are. atomic<T>::operator T and atomic<T>::operator= are equivalent to atomic<T>::load and atomic<T>::store respectively. All the operators are implemented in the atomic class such that they will use atomic operations as you would expect.

    I'm not sure what you mean about "non-reference" types? Not sure how reference types are relevant here.