← back to programming

Concurrency

Abelson & Sussman ยท 1996 ยท SICP ยง3.4

Shared mutable state + concurrency = bugs. Serializers ensure critical sections run atomically. But composing serializers can deadlock. There is no free lunch.

A read x write x B read x write x conflict time → interleaving makes order unpredictable — shared state + concurrency = bugs

The nature of time in concurrent systems

Without concurrency, events happen in a definite order. With concurrency, operations on shared state can interleave. The result depends on timing, not just logic.

Scheme

Mechanisms for controlling concurrency

A serializer takes a procedure and returns a version that cannot interleave with other serialized procedures from the same serializer. The serializer is a higher-order function that wraps procedures with mutual exclusion.

Scheme

Implementing serializers: the mutex

A serializer is built from a mutex (mutual exclusion). A mutex has two operations: acquire and release. A process that has acquired the mutex blocks all others until it releases.

Scheme

Deadlock

When two accounts need to transfer money, each must lock both accounts. If account A locks itself and waits for B, while B locks itself and waits for A, neither can proceed. This is deadlock. The classic fix: always acquire locks in a fixed global order.

Scheme

Notation reference

SICP concept Python Meaning
parallel-executethreading.ThreadRun procedures concurrently
(make-serializer)threading.Lock()Create mutual exclusion guard
(s proc)with lock:Serialize a procedure
test-and-set!CAS / atomic opHardware-level atomic test-and-set
deadlockdeadlockCircular wait on locks

Translation notes

Neighbors
Ready for the real thing? Read SICP §3.4.