Acquire/Release versus Sequentially Consistent memory order

The C++11 memory ordering parameters for atomic operations specify constraints on the ordering. If you do a store with std::memory_order_release, and a load from another thread reads the value with std::memory_order_acquire then subsequent read operations from the second thread will see any values stored to any memory location by the first thread that were prior …

Read more

“Use of deleted function” error with std::atomic_int

Your code is attempting to construct a temporary std::atomic_int on the RHS, then use the std::atomic_int copy constructor (which is deleted) to initialise stop, like so: std::atomic_int stop = std::atomic_int(0); That’s because copy-initialisation, as you are performing here, is not quite equivalent to other kinds of initialisation. [C++11: 8.5/16]: The semantics of initializers are as …

Read more

How do memory_order_seq_cst and memory_order_acq_rel differ?

http://en.cppreference.com/w/cpp/atomic/memory_order has a good example at the bottom that only works with memory_order_seq_cst. Essentially memory_order_acq_rel provides read and write orderings relative to the atomic variable, while memory_order_seq_cst provides read and write ordering globally. That is, the sequentially consistent operations are visible in the same order across all threads. The example boils down to this: bool …

Read more

Does std::atomic work appropriately?

The standard does not specify a specialization of std::atomic<std::string>, so the generic template <typename T> std::atomic<T> applies. 29.5 [atomics.types.generic] p1 states: There is a generic class template atomic. The type of the template argument T shall be trivially copyable (3.9). There is no statement that the implementation must diagnose violations of this requirement. So either …

Read more

Why don’t compilers merge redundant std::atomic writes?

The C++11 / C++14 standards as written do allow the three stores to be folded/coalesced into one store of the final value. Even in a case like this: y.store(1, order); y.store(2, order); y.store(3, order); // inlining + constant-folding could produce this in real code The standard does not guarantee that an observer spinning on y …

Read more

What does the [[carries_dependency]] attribute mean?

[[carries_dependency]] is used to allow dependencies to be carried across function calls. This potentially allows the compiler to generate better code when used with std::memory_order_consume for transferring values between threads on platforms with weakly-ordered architectures such as IBM’s POWER architecture. In particular, if a value read with memory_order_consume is passed in to a function, then …

Read more

Must I call atomic load/store explicitly?

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 …

Read more