Cloud Parts

CSP channels

Independent sequential processes that wait on each other by sending and receiving values over channels, after Hoare’s Communicating Sequential Processes.

How they work

Tony Hoare’s 1978 paper made input and output basic primitives of programming: a process that sends waits until the receiving process is ready, and the two commands then happen together, with no buffer between them. Hoare’s processes named each other directly; Go took a different branch of the family tree and made the channel itself a first-class value that any goroutine can hold. An unbuffered Go channel is still Hoare’s rendezvous: the sender blocks until the receiver has the value. Go’s slogan for the style is “Do not communicate by sharing memory; instead, share memory by communicating.”

Clojure’s core.async (2013) brought channels to the JVM and to JavaScript as a library. Its go macro rewrites a block into a state machine that is parked at each channel operation and releases its thread, like C#’s async. Rich Hickey recommends bounded buffers over unbounded ones, because a full channel that blocks the sender is a natural form of back pressure.

Channels decouple the two sides: neither knows who is on the other end, which is Hickey’s argument for them over actors. The cost is that nothing stops two processes waiting on each other forever, so deadlocks are possible and timeouts are up to you.

Sources

Improve this page on GitHub