I am trying to use std::atomic library.
- What’s the difference between specialized and non-specialized atomic
member functions? - What’s the difference (if there is any) between following functions?
- operator= stores a value into an atomic object (public member function) v.s. store (C++11) atomically replaces the value of the atomic object with a non-atomic argument (public member function)
- operator T() loads a value from an atomic object (public member function) v.s. load (C++11) atomically obtains the value of the atomic object (public member function).
- operator+= v.s. fetch_add
- operator-= v.s. fetch_sub
- operator&= v.s. fetch_and
- operator|= v.s. fetch_or
- operator^= v.s. fetch_xor
- What’s the downside of declare a variable as atomic v.s. a
non-atomic variable. For example, what’s the downside of
std::atomic<int> xv.s.int x? In other words, how much is the overhead of an atomic variable? - Which one has more overhead? An atomic variable, v.s. a normal
variable protected by a mutex?
Here is the reference to my quesitons. http://en.cppreference.com/w/cpp/atomic/atomic
Not an expert, but I’ll try:
int) contain additional operations such asfetch_add. Non-specialized forms (user defined types) will not contain these.operator=returns its argument,storedoes not. Also, non-operators allow you to specify a memory order. The standard saysoperator=is defined in terms ofstore.load.intin the way you would usestd::atomic_int.int <= std::atomic <= int and std::mutexwhere<=means ‘less overhead’. So it’s likely better than locking with a mutex (especially for built-in types), but worse thanint.