FFORGE//RS
← Roadmap

rust / LEVEL 4

The counter that wrapped to zero

EST.35 MIN
01

THEORY / RETRIEVAL

What to restore

  • Implement a compare-exchange retry loop
  • Choose Relaxed ordering only when the metric carries no synchronization meaning

Atomic does not mean compound expressions are atomic

A load followed by a store is two separate operations and loses concurrent updates. Read-modify-write operations or a compare-exchange loop make the transition conditional on the value that was observed.

Ordering belongs to the data contract

Relaxed ordering is enough for an independent statistics counter whose value does not publish other memory. If observing the counter must make other writes visible, the contract needs an acquire/release relationship instead.

CHECKPOINT

When is `Ordering::Relaxed` sufficient for an atomic request counter?

ISOLATED RUST 1.96
src/lib.rsEDIT

02 / IMPLEMENTATION

Implement the contract

Implement a lock-free saturating add that returns the new value, never wraps, and preserves concurrent increments. This counter has no synchronization role beyond its number.

Initializing editor…
CLOUD SANDBOXnetwork off · 256 MB · 12 s
1 / 64 KB
OUTPUT
Runner is waiting for a submission.