When is std::atomic appropriate, when is a mutex required, and why is lock-free operation not guaranteed?
Show answer and explanation
std::atomic makes individual operations on one atomic object free of data races and is useful for counters, flags, and carefully designed lock-free protocols. It does not automatically make a multi-step invariant across several values atomic, so a mutex is usually clearer when an operation must update related state as one critical section or may block. Atomicity does not imply lock freedom: support depends on the type and target, can be queried with is_lock_free or is_always_lock_free, and memory-order choices must still establish the required happens-before relationships.
std::atomic makes individual operations on one atomic object free of data races and is useful for counters, flags, and carefully designed lock-free protocols. It does not automatically make a multi-step invariant across several values atomic, so a mutex is usually clearer when an operation must update related state as one critical section or may block. Atomicity does not imply lock freedom: support depends on the type and target, can be queried with is_lock_free or is_always_lock_free, and memory-order choices must still establish the required happens-before relationships.
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.