,

Contents · Concurrency models (actors, CSP, STM)


Concurrency vs parallelism, safety and liveness

  • Concurrency: composition of independently executing tasks; parallelism: do more at once.
  • Safety (no races) and liveness (no deadlock/starvation) are primary goals.
  • Memory models (SC, TSO) constrain visibility and reordering.

Actor model

  • Actors encapsulate state; communicate by async messages via mailboxes.
  • Supervision hierarchies and failure handling (let it crash).
  • Runtime: mailboxes, schedulers (M:N), backpressure, and routing.
spawn(actor, args) → pid; send(pid, msg); receive { case ... }

CSP (Communicating Sequential Processes)

  • Processes communicate via synchronous (or buffered) channels.
  • Select/alt constructs for waiting on multiple events.
  • Runtime: lightweight processes, channel queues, and scheduler fairness.
go func() { ch <- x }(); y := <-ch; // Go-style CSP

Software Transactional Memory (STM)

  • Atomic, isolated transactions on memory; optimistic concurrency with rollback.
  • Versioned memory (TMVars), conflict detection, and commit protocols.
  • Strengths: composability; Costs: contention, validation overhead.
atomically { x := readTVar(t); writeTVar(t, x+1) }

Locks, lock-free, and wait-free

  • Mutexes/RW locks: simplicity vs contention; deadlock avoidance patterns.
  • Lock-free structures via CAS; progress guarantees; ABA and hazard pointers.
  • Wait-free algorithms for strict progress at higher complexity.

Mapping models to runtimes and languages

  • Actors: Erlang/Elixir, Akka, Orleans; CSP: Go, Clojure core.async.
  • STM: Haskell STM, Clojure refs; Lock-free: C++ atomics, Rust with ownership.
  • Pick models per subsystem; hybrid designs are common.

Exercises

  1. Build a small actor system with mailboxes and a work-stealing scheduler.
  2. Implement CSP-style channels and a select operation; measure throughput.
  3. Implement STM with versioned records and contention manager; test composability.
Concurrency is about composition: choose the model that matches your failure, latency, and throughput needs.