Concepts and theorems · Intermediate

Atomicity: choosing an indivisible operation

Distinguish an indivisible access from an entire multi-step operation that must be coordinated.

Ask Logeia about this articleTry the three questionsReview your answers

Updated

On this page
  1. Specify the operation boundary
  2. How an increment can be lost
  3. Atomicity is distinct from ordering
  4. Protecting a larger invariant
  5. Test a multi-field invariant
  6. Ask Logeia about this articleCheck your understanding

Specify the operation boundary

Atomicity in concurrent programming concerns an operation that other executions cannot observe as a collection of partially completed steps. The operation boundary must be stated explicitly: an atomic load, store, increment, and conditional update are different guarantees. Saying that a variable is atomic is not enough to prove that every sequence using it is indivisible. A read followed by a calculation and a write can remain a multi-step interaction even when each individual access has an atomic implementation. Check the language and operation specification, not just the storage size.

How an increment can be lost

Start with x=0 and let two threads each perform a separate load, add one locally, and store. If both loads return zero, each thread can store one, leaving a final value of one. No torn integer representation is needed for this lost update. An atomic read-modify-write operation or a suitable lock can protect the complete increment. The example isolates an algorithmic interleaving; in a language where unsynchronized non-atomic accesses constitute a data race, the program may have even weaker or undefined semantics rather than this single predictable outcome.

Atomicity is distinct from ordering

Atomicity and memory ordering answer different questions. An indivisible counter update does not by itself specify when unrelated data becomes visible to another thread. A producer that writes a payload and then signals readiness needs the synchronization required to publish that payload correctly. Acquire and release operations, or the contract of a lock, can provide the necessary relationship when used as specified. A relaxed atomic can be appropriate for some counters but insufficient for data publication. Volatile is not a portable replacement for a synchronization design across programming languages.

Protecting a larger invariant

An invariant spanning several values may require a wider coordination mechanism than an atomic counter. Decrementing stock while recording an order must preserve a relationship between two pieces of state, not merely make one decrement indivisible. Define the invariant first and then choose the synchronization boundary. Labels such as atomic or lock-free do not alone establish the latency or correctness of the complete algorithm. Database transaction atomicity also operates at a different boundary and failure model from a processor atomic instruction; neither should be substituted for the other without examining the required guarantee.

Test a multi-field invariant

Let balances A and B start at one hundred and zero. A transfer atomically subtracts ten from A and then atomically adds ten to B. A reader between those operations can observe a total of ninety even though neither counter returns a torn representation. Preserving the sum requires a wider contract than individually atomic counters. Both readers and writers might participate in one lock protocol, or the design may require a suitable transaction or snapshot mechanism. This example assumes language-valid atomic accesses. Replacing them with unsynchronized ordinary variables can introduce a data race and does not justify predicting only the interleaving shown here.

Reading and practice are recorded separately in this browser, for each language.

Learn this in order

Build your CS foundations · STEP 17

Understand the systems behind the web · STEP 7

Prepare for AI systems · STEP 7

Choose a learning path