Skip to main content
Definition Website Computer and Technology

Software Transactional Memory

Subject: Computer and Technology noun /ˈsɔːftˌwɛr trænˈzæk.ʃən.əl ˈmɛm.ər.i/ 1 definition

  1. Definition (noun)

    Entry #340 · Updated

    Software Transactional Memory (STM) is a concurrency control mechanism implemented in language runtimes or libraries that lets threads read and modify shared memory through composable transactions rather than explicit mutual-exclusion locks. During a transaction, the runtime tracks shared-memory accesses via thread-local logs or versioned references and validates—either eagerly or at commit time—that no concurrent thread altered the read locations. When a conflict occurs, the runtime aborts the transaction, discards or rolls back its tentative writes, and automatically retries the block, ensuring atomicity and isolation while eliminating lock-order deadlocks. While STM dramatically simplifies composing thread-safe abstractions compared to fine-grained locking, it adds runtime instrumentation and retry overhead and generally forbids irrevocable side effects such as external I/O inside transactional blocks.

    Common Forms

    • Optimistic Deferred-Update (Redo-Log) STM: Buffers reads and speculative writes in a thread-local log, then validates read consistency and applies updates atomically at commit time (as in TL2 and Haskell's GHC STM), keeping aborts cheap because shared memory remains untouched until commit.

    • Eager-Update (Undo-Log) STM: Writes changes directly into shared memory while recording prior values in an undo log and acquiring ownership eagerly, accelerating uncontended commits at the cost of explicitly reverting memory upon conflict.

    • Multi-Version (MVCC) STM: Retains timestamped historical snapshots of transactional references so read-only transactions can inspect a consistent point-in-time state without blocking or aborting alongside concurrent writers, as used in Clojure's Ref system.

    • Lock-Free and Obstruction-Free STM: Coordinates memory updates strictly through atomic compare-and-swap (CAS) primitives and shared transaction descriptors rather than internal blocking locks, preventing thread stalls or preemption from halting system-wide progress.

    Example

    In a Haskell payment service using Control.Concurrent.STM, an atomically block transfers $100 from acctA (balance $500) to acctB (balance $200) via readTVar and writeTVar, composing two account updates without acquiring account-level mutexes in a fixed order. If a concurrent worker thread modifies acctA before the transaction commits, the GHC runtime detects the stale TVar read during commit validation, discards the uncommitted writes from the thread-local log, and automatically re-executes the block against the updated balances.