liveBook · Manning

Atomicity

As explained in Functional Design and Architecture: Examples in Haskell

Atomicity is the property that a transaction's operations take effect as one indivisible unit.

Concurrent code becomes unsafe when separate threads read and update shared state independently. Atomicity makes a transaction’s operations take effect as one indivisible unit, preventing another thread from observing or interrupting a partial update. section 9.2.1 section 9.2.3

Software transactional memory (STM) applies atomicity to transactional variables such as TVars and queues. A program builds a concurrent data model, defines transactions over it, and runs those transactions atomically in a threaded environment. section 9.2.1

Atomic STM operations provide several practical guarantees:

  • Compose smaller transactions into larger transactions while retaining transactional behavior. section 9.2.1
  • Avoid the race conditions associated with accessing raw mutable IORefs from multiple threads. section 9.2.3

Thread-safe meteor reporting

A reporting channel implemented with IORef can race when threads read and write the list concurrently. Replacing it with TVar and enclosing the update in evalStmAtomically makes the read-and-write operation a single STM transaction.

How Functional Design and Architecture: Examples in Haskell covers it

How deep it goes
Explains atomic transactions, composition, rollback through retry, and execution with atomically, but leaves subtle usage concerns largely undeveloped.
Examples
Demonstrates atomicity with TQueues, meteor reporting, transaction composition, and the dining philosophers problem.
What it assumes
Assumes multithreaded shared state and mutable references, while teaching the shift from unsafe IORef access to STM-managed state.
What you can do after
Apply it via Haskell’s STM library to build, compose, retry, and atomically execute thread-safe state updates.
How it connects
Connects atomicity with software transactional memory, transactional variables, concurrency, thread safety, transaction composition, and retry.

Also explained in

Compare how 13 books explain atomicity →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Functional Design and Architecture: Examples in Haskell. The section links point to the parts of the book it draws on.