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.












