What guarantees do the C++ atomic memory orders provide, and when is relaxed ordering sufficient?
Show answer and explanation
Relaxed operations preserve atomicity and each object's modification order but establish no inter-thread ordering for unrelated memory. Release publishes prior work, acquire can observe that publication when it reads the corresponding value, and acq_rel combines both roles for read-modify-write operations. Sequentially consistent operations additionally participate in a single total order consistent with their required ordering; consume is dependency-oriented and is generally avoided in portable C++20 code because implementations have historically treated it as acquire. Relaxed is appropriate only when the atomic value itself matters and no other state must become visible through that operation, such as a statistics counter.
Relaxed operations preserve atomicity and each object's modification order but establish no inter-thread ordering for unrelated memory. Release publishes prior work, acquire can observe that publication when it reads the corresponding value, and acq_rel combines both roles for read-modify-write operations. Sequentially consistent operations additionally participate in a single total order consistent with their required ordering; consume is dependency-oriented and is generally avoided in portable C++20 code because implementations have historically treated it as acquire. Relaxed is appropriate only when the atomic value itself matters and no other state must become visible through that operation, such as a statistics counter.
Connect the conclusion directly to the type, lifetime, ownership, overload-resolution rule, or library contract present in the question. State implementation dependence or a violated precondition explicitly instead of merely guessing output.