Search

A technology, a device or an article

Ready for another core: what release/acquire publishes

A one-time C++ publication review connects release/acquire to ordinary data and separates atomicity from lifetime management.

Bound paper bundle, brass signal and open book linked by a painted arrow
ITNWS AI-generated conceptual illustration. Signal and finished data are separate objects; a publication metaphor, not a CPU diagram.ITNWS · image_gen · Original ITNWS AI-generated conceptual editorial illustration

“The flag is atomic.” That could be the opening objection in a fictional code review: one thread prepares a result, and another checks permission to read it. The objection sounds convincing. An atomic flag cannot be torn into independently observed pieces, so the reader should see an honest value. Yet the disputed object sits beside the flag. The result lives in ordinary memory. Atomic access to a separate signal does not, by itself, explain why reading that result is ordered with its preparation.

This is an annotated review of a small, one-time C++ publication protocol, rather than a report of a real incident or processor benchmark. CPU cores execute the program, but the language also determines which transformations its compiler may make. The reasoning must cover that level. Otherwise a program can appear to work on a familiar machine while lacking the necessary argument for another build or architecture. A plausible story about caches is insufficient if it overlooks the program’s actual contract.

Assume that payload and the atomic object ready are initialized before either worker thread starts. Initially ready is false. There is exactly one producer and one consumer. The producer writes 42 once and then publishes true. The consumer accesses payload only after loading true, and nobody changes the result afterward. These assumptions define this example. They are not an implementation of a reusable queue.

int payload = 0;
std::atomic<bool> ready{false};
// producer, once
payload = 42;
ready.store(true, std::memory_order_release);
// consumer, after both objects have been initialized
if (ready.load(std::memory_order_acquire)) {
    use(payload);
}

A comment beside the signal

The release store is sequenced after the preparation of the data in the producer. An acquire load that obtains its value from that store synchronizes with it. The atomic-ordering section of the C++ working draft states the rule through the atomic object involved and the origin of the value read. Merely putting the words acquire and release somewhere in a program does not establish the required connection. Our sole store of true makes the connection particularly straightforward to identify.

The relationships form a chain. Writing payload is sequenced before the release store; that store synchronizes with the appropriate acquire load; reading payload is sequenced after the load. Transitive happens-before connects preparation to use. This is a relationship in the programming model, not an ITNWS measurement of nanoseconds between cores. The ordinary object’s read is justified by ordering, even when the physical implementation contains several cached copies of memory lines.

The branch in which the load returns false deserves its own comment. It establishes no published result. The program can do something else or check again under its protocol. It must not read payload because the producer has probably almost finished. “Almost” creates no ordering between conflicting accesses. The condition identifies the path on which using the result is justified; it is more than a cosmetic state check around an otherwise permissible read.

Now replace both memory orders with relaxed. Access to the flag remains atomic, but the publication relationship for the ordinary object no longer follows from those operations. Without other synchronization, the conflicting accesses to payload lack the necessary ordering. The C++ draft’s data-race definitions explain why this cannot be described merely as a chance of reading an old number. Undefined behavior is not a promise to choose between zero and 42 with some hardware-dependent probability.

That is where “the cache has not updated yet” becomes an uncomfortably narrow explanation. It invites us to picture a valid program with delayed delivery. The compiler, however, follows the language rules rather than the author’s intention. Once those rules are violated, predicting one disappointing output does not bound all permitted consequences. First establish a correct software exchange; only then investigate which instructions and memory mechanisms implement it on a particular CPU.

Atomicity and synchronization answer related but different questions. An independent atomic statistics counter can legitimately use relaxed operations if only its own value matters and the algorithm does not rely on it to publish other objects. Relaxed is not intrinsically defective. The error is extending the guarantee for one operation to unrelated data. Choosing an order requires understanding the value’s role in the algorithm, rather than placing the available orders on a universal scale from fast to trustworthy.

Amber tile joining a filled blue square to an empty frame
ITNWS AI-generated conceptual illustration. The connector evokes a relationship between permission and result, not a physical barrier. — ITNWS · image_gen · Original ITNWS AI-generated conceptual editorial illustration

The placement of payload and ready is also outside the publication proof. They can occupy nearby or distant memory locations. Placement can affect exchange costs, but cannot replace the required synchronization relationship. If rearranging fields conceals a failure, that does not establish correctness of either protocol. The mosaic connector represents a logical connection, rather than a requirement to put the signal and its result in one physical cache line.

In the margin: what visible means

The everyday word visibility can suggest flushing all caches into shared physical RAM. Release/acquire should not be presented as a requirement to push every byte out to the memory modules. Its guarantees concern observable behavior and ordering relationships. The architecture and compiler provide a suitable implementation. Some target operations may need no separate full-barrier instruction; others require specific instructions. An unremarkable assembly listing does not erase the source operation’s semantics.

GCC’s documentation for atomic builtins connects memory ordering with constraints on transformations and target implementation. Inspecting generated code is useful when the compiler version, target, and source conditions are known. Generalizing one listing to every CPU reverses the argument. The program specifies its contract, and the toolchain realizes that contract for a machine. A million successful iterations on one device do not replace the missing contract of an incorrectly synchronized program.

The word atomic also does not guarantee a lock-free implementation for every type and size. A particular operation may need an auxiliary mechanism. That implementation property differs from memory ordering. Acquire does not promise lock-free, and lock-free does not automatically imply acquire. Neither designation alone establishes the latency or scalability of the entire algorithm. They describe different obligations and implementation characteristics, which a useful review keeps separate.

Replacing the flag with volatile supplies no portable inter-thread synchronization in C++. Volatile has its own access rules and purposes, but it does not transform conflicting ordinary accesses into this publication protocol. Such a substitution is especially misleading if a previous failure becomes harder to reproduce. Changed optimization can conceal a symptom without establishing the required relationship between threads or protecting an object’s lifetime. The observation would need explanation, not promotion into a correctness rule.

The terminology exists outside C++ too. Rust’s Ordering documentation helps compare release and acquire semantics, but a C++ example involving ordinary mutable data cannot simply be copied into Rust unchanged. Rust has additional requirements for safe shared access. The publication idea does not waive aliasing or lifetime rules. Matching order names provide a reading aid, rather than permission to bypass a type system or justify arbitrary unsafe code.

Review continues after the first read

Our producer never changes payload again. Remove that condition and the small argument no longer covers the entire algorithm. The producer might publish true and immediately overwrite the result while the consumer reads it. Initial synchronization does not protect the object indefinitely. Repeated use needs an ownership protocol, acknowledgement, another version object, mutual exclusion, or another suitable design. Naming the original flag atomic cannot protect every later modification.

Reusing a Boolean flag also conceals generations. After true, false, and true, the same observed value can refer to different publications. The consumer needs to know which version it may read and whether that version has already been replaced. Adding a generation counter is not a finished solution without rules for updating it and handling wraparound. At this point the one-time example has ended and a reusable protocol needs its own complete correctness argument.

Publishing a pointer presents a related but distinct obligation. The address can be transferred with appropriate ordering, yet the object must remain alive until the reader finishes. Publication and reclamation are different parts of the algorithm. Without lifetime management, the consumer can receive a properly published address of an object that no longer exists. The strongest atomic load order does not independently extend the lifetime of the object at that address.

Several consumers require another examination. Concurrent reads of an immutable result may be appropriate when lifetime is secured. But if use itself modifies shared data, that side effect is not protected by the original flag. Publication explains how the reader obtained this result; it does not cover every subsequent action placed in the successful branch. A reviewer must inspect actual accesses after the load, rather than approving the branch solely because acquire appears at its entrance.

Correct publication also does not guarantee efficient waiting. A polling loop can consume CPU time, compete with other work, and depend on scheduling. A waiting primitive or a different exchange design changes operational behavior while still needing the required memory relationship. We make no latency comparison or speedup promise for weaker ordering here. Those claims would need a separate experiment with a stated workload, architecture, and thread placement.

An acquire operation on a different atomic object does not automatically repair the example. Each object has its own modification history. If the reader checks one flag while the producer publishes through another, an additional connecting argument is necessary. “Both ends use suitable orders” omits precisely the relationship joining the ends. This matters during refactoring, when one familiar flag becomes several state fields and the old reasoning appears preserved only because the variable names sound similar.

Nor is seq_cst a cure for every memory access. Stronger ordering of atomics cannot make an ordinary object safe to read while another thread still modifies it without the required relationship. It supplies additional guarantees where applicable, but cannot infer ownership or confirm completion of unrelated work. Discussion of barrier cost follows correctness; weakening an order requires preserving every relationship the original correctness argument actually used.

After this review, “the flag is atomic” remains true and becomes a smaller claim. For our one-time immutable result, we can identify a store, a load that reads from it, and the object preparation connected to both. A new version, an additional reader, or early reclamation needs another examination. Publication is justified when the developer can explain another core’s right to read these bytes, rather than merely display a bit that changed. Sources checked October 24, 2026.

Sources & documentation

  1. GCC: atomic builtins ↗
  2. C++ draft: atomic ordering ↗
  3. C++ draft: data races ↗
  4. Rust: atomic Ordering ↗