Separate atomicity from ordering
Safe access to shared state requires more than an update that cannot be split. Operations on different data also need the intended ordering relationship. This discussion uses the C++ memory model. Conflicting accesses to ordinary memory from different threads, with at least one write and without the required happens-before relationship, create a data race. Repeatedly observing the expected result on one machine is not a substitute for a language-level guarantee. List the shared objects, writers, readers and lifetimes first. For every access, explain which rule makes it valid rather than inferring safety from an apparently successful execution.
Follow a one-shot publication
A producer writes forty-two into an ordinary integer and then stores true to an atomic ready flag with release ordering. A consumer loads the flag with acquire ordering and reads the integer only after observing the corresponding published true value. That synchronization carries the preceding data write to the reader. Restrict this example to a flag initialized false and changed to true once. The producer performs no subsequent unsynchronized modification while a consumer may read the data. A small example makes the safety conditions visible; it should not be generalized into permission to repeatedly overwrite a shared payload behind a flag.
Identify the publication actually observed
An acquire operation does not synchronize with an arbitrary release simply because both order names appear in the program. The specification requires the appropriate relationship on the atomic object, including which publication the read observes. Reusing an already true flag for another payload can let a reader observe an old true while a new ordinary write races with it. Repeated communication needs another ownership or generation protocol, such as a properly designed queue. Adding a reset to false is not by itself a proof. Draw who may write each payload generation and during which interval another thread may safely read it.
Use relaxed and volatile for their actual roles
A relaxed atomic access remains atomic for its own object. It does not, by that fact alone, publish unrelated ordinary data to another thread. An independent statistical counter may suit relaxed operations, whereas using a counter as a readiness signal requires an argument about the payload accesses as well. C++ volatile is not a general replacement for thread synchronization. Its implementation-related uses, including certain device-access situations, should be separated from inter-thread ordering. Start from the relationship the algorithm requires and then choose operations that establish it. Choosing the weakest-looking order first and checking only output values afterward reverses the reasoning process.
Handle both CAS outcomes
Compare-and-swap atomically replaces a value only when it matches the expected value. In C++ compare_exchange, failure updates the expected argument with an observed value, so a retry must reconsider the candidate built from it. The weak form can fail spuriously and is normally used in an appropriate retry loop. Success and failure memory orders also have validity constraints. Atomic comparison and replacement do not automatically protect allocation, exceptions or accesses through surrounding pointers. Specify which temporary values remain usable after failure and which must be rebuilt. Otherwise, an apparently correct atomic primitive can sit inside an incorrect compound operation.
Distinguish system progress from per-thread completion
Lock-free progress generally ensures that operations continue completing system-wide. A particular thread can repeatedly lose CAS attempts, so this differs from a wait-free per-operation step bound. Furthermore, an atomic type may rely on an internal lock for some types or implementations. A platform inquiry such as is_lock_free can clarify a primitive’s implementation property, but it does not prove progress of the entire algorithm. Allocation, reclamation and other called routines can introduce blocking behavior. State the guarantee for the complete implementation and its assumptions instead of deriving it from the absence of an explicit mutex in a short code fragment.
Include object reclamation in correctness
Removing a node does not imply that no reader still holds its address. Immediate deallocation after a successful head-pointer CAS can leave an existing reader accessing an object whose lifetime has ended. Reuse of the same address can also hide an intervening history change from a pointer-only comparison, contributing to an ABA problem. A generation tag and a safe reclamation condition address different concerns. Techniques based on hazard pointers or epochs require their own rules for registration, rechecking, stalled readers and deferred freeing. Treat publication, access, removal and eventual reclamation as one correctness argument rather than considering reclamation an optional optimization.
Know what execution tests cannot establish
Begin with a two-thread, one-shot example and annotate the synchronization supporting each access. Execution tools can identify races on paths they observe, but a successful run is not a proof of every unexecuted path or of lock-free progress. Stress testing also does not establish that a rare execution is impossible. For structures with reclamation, examine a mature library’s contract and assumptions before implementing a new protocol. Compare performance only after constructing the correctness argument. Report thread count, contention, waiting and memory consumption together, because reducing lock calls alone does not establish lower latency or a simpler lifetime discipline.