Atomics - Junior¶
An atomic counter updates as one indivisible operation, so concurrent metrics increments are not lost.
sequenceDiagram
participant A as Worker A
participant C as Atomic counter
participant B as Worker B
A->>C: fetch_add(1)
B->>C: fetch_add(1)
Note over C: both updates are retained
Use atomics for independent counters, flags, and simple state transitions. They do not make a multi-field invariant atomic. If count and total must change together, use a lock or one immutable snapshot.
Continue to middle.md.
Test yourself¶
- Why is ordinary
counter++unsafe? - Which state fits one atomic value?
- Why do two atomics not create one transaction?