Do I have to use atomic for “exit” bool variable?

Do I have to use atomic for “exit” bool variable? Yes. Either use atomic<bool>, or use manual synchronization through (for instance) an std::mutex. Your program currently contains a data race, with one thread potentially reading a variable while another thread is writing it. This is Undefined Behavior. Per Paragraph 1.10/21 of the C++11 Standard: The … Read more

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

Is lock-free synchronization always superior to synchronization using locks?

Does this imply that, ‘there is no case where using a non-lock-free atomic type would be a better choice over using a lock-free atomic type when the latter is available’ ? (Mainly in terms of performance rather than ease-of-use). No. And that is, in general, not true. Suppose you have two cores and three threads … Read more

How do memory_order_seq_cst and memory_order_acq_rel differ? 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:, order);, order);, 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