Part 6 · 1 chapters · ~8 min

Actors and CSP

Shared memory, actors and communicating sequential processes compared, Erlang processes and supervision, Akka and Orleans, Go channels, select and pipelines, fan-out and fan-in, backpressure with bounded channels, and choosing a model.

11

Three models

code
// Go pipeline: fan-out to 5 workers, fan-in results, bounded channels give backpressure
jobs := make(chan Transfer, 100)
results := make(chan Result, 100)
for w := 0; w < 5; w++ { go func() { for t := range jobs { results <- process(t) } }() }
go func() { for _, t := range batch { jobs <- t }; close(jobs) }()   // blocks when the buffer is full
for range batch { r := <-results; record(r) }

%% Elixir: one process per wallet; messages handled one at a time, so no locks
def handle_call({:debit, amt}, _from, bal) when bal >= amt, do: {:reply, :ok, bal - amt}
def handle_call({:debit, _}, _from, bal), do: {:reply, {:error, :insufficient}, bal}
THREE MODELS OF CONCURRENCY
shared memory, actors, and communicating sequential processes
shared memory + locksthreads share dataactorsprivate state, mailboxesCSPprocesses + channels
swipe the figure sideways, or tap expand for full screen
1/4
shared memory
Threads share data and coordinate with locks and atomics. Fast and flexible, and the source of most concurrency bugs. Java, C++, C#, Rust (made safe by the type system).
share data, coordinate with locksfast, error-prone