Concurrency · intermediate
Atomicity
Atomicity names related but distinct guarantees: a concurrent atomic operation is indivisible under its memory model, while database atomicity means a transaction commits all its writes or none of them.
Why it matters
Correctness depends on naming the exact boundary and guarantee. Operation atomicity, transaction rollback, memory ordering, and transaction isolation solve different parts of a multi-step state change.
Mental model
How to reason about atomicity
Draw separate boxes around separate promises. A CPU or runtime atomic primitive prevents interleaving within one operation; a transaction creates an all-commit-or-all-rollback boundary, while isolation controls concurrent visibility.
Analogy
An atomic operation is one sealed ballot drop. A database transaction is a bundle accepted or rejected as a whole; a separate viewing policy decides who may inspect work before acceptance.
Examples
See the boundary, not just the happy path
Worked example · Atomic increment
counter.fetch_add(1)The read-modify-write is exposed as one atomic operation for that counter rather than three interleavable steps.
Worked example · Transactional transfer
debit A; credit B; COMMITDatabase atomicity makes the group commit entirely or roll back. The isolation level, not atomicity alone, governs what concurrent transactions may observe.
Useful contrast · Two atomic fields
atomic debit; atomic creditIndividual atomic writes do not make the pair atomic. An observer can still see the interval between them.
Common mistakes
Misconceptions to remove early
Assuming one source line is atomic
A statement may compile to several loads, computations, and stores. Atomicity comes from a specified primitive or synchronization boundary, not formatting.
Treating atomicity as full thread safety
Atomic primitives do not automatically preserve multi-variable invariants or provide every desired visibility ordering. Design at the invariant's scope.
Quick check
Can you predict the result?
1. If x and y are each atomic, is updating x then y one atomic pair?
- • No; another observer may run between the two atomic operations
- • Yes; all atomic variables merge into one transaction
- • Yes, but only when x and y are integers
2. What must you specify when calling a multi-step change atomic?
Keep building