CSP (Communicating Sequential Processes) and Channels#
One of the most transformative paradigms in concurrent programming originated in 1978 with C.A.R. Hoare’s landmark paper, “Communicating Sequential Processes (CSP)”.
Go adopted CSP theory as a first-class citizen in the language design, shaping today’s Goroutines and Channels.
1. What Is CSP?#
In the late 1970s, computer science was intensely focused on structured programming (eliminating goto) and object-oriented paradigms. Concurrency was rarely treated as a fundamental construct; most concurrency relied on shared memory with lock synchronization.
Hoare challenged this status quo:
“Input and output are basic primitives of programming that have been overlooked, particularly in concurrent code. Rather than having processes fight over shared memory, processes should communicate exclusively via input and output commands.”
Core Principles of Hoare’s CSP#
- Sequential Processes: Each process is an encapsulated unit of logic with its own independent state running sequentially.
- Synchronous Message Passing: Inter-process interaction occurs via explicit input and output operations, not shared variables.
- Handshake: Communication occurs only when both sender and receiver meet, inherently synchronizing execution.
2. Hoare’s Notation and Go Channels#
The formal notation defined in Hoare’s 1978 paper was directly realized in Go syntax over three decades later:
| CSP Notation (Hoare, 1978) | Go Implementation | Description |
|---|---|---|
process ! value | ch <- value | Send data to process/channel (Output command) |
process ? variable | variable := <-ch | Receive data from process/channel (Input command) |
*[west?c -> east!c] | for c := range west { east <- c } | Pipeline composition between processes |
Guarded Commands (->) | select { case ... } | Wait across multiple channels and execute ready communication |
Hoare incorporated Edsger Dijkstra’s Guarded Commands so communication only triggers when conditions are satisfied, which became Go’s select statement.
3. Shared Memory vs CSP Model#
| Dimension | Shared Memory Model | CSP Model (Message Passing) |
|---|---|---|
| Primitives | sync.Mutex, Semaphore, Condition Variables | chan (Channel), select |
| Communication | Read/write to identical memory addresses | Transfer independent data values/ownership via channels |
| Synchronization | Manual Lock/Unlock conventions | Inherent blocking synchronization on send/receive |
| Key Risks | Data Races, Deadlocks, Race Conditions | Goroutine Leaks |
| Composability | Low (nesting multiple locks causes deadlocks) | Very High (pipelines, timeouts, cancellations compose naturally) |
4. Go’s Philosophy and Decision Tree: Channel vs Mutex#
The famous Go adage directly reflects Hoare’s CSP philosophy:
“Do not communicate by sharing memory; instead, share memory by communicating.”
Why then does Go also provide sync.Mutex in its standard library?
In practice, dogmatically using only channels or only mutexes is an antipattern. In Concurrency in Go, Katherine Cox-Buday provides a clear Decision Tree:
graph TD
Q1["Is it a performance critical section?"]
Q2["Are you trying to transfer ownership of data?"]
Q3["Are you trying to guard internal state of a struct?"]
Q4["Are you trying to coordinate multiple pieces of logic?"]
P["Use Primitives<br/>(sync.Mutex, atomic, etc.)"]
C["Use Channels"]
Q1 -- Yes --> P
Q1 -- No --> Q2
Q2 -- Yes --> C
Q2 -- No --> Q3
Q3 -- Yes --> P
Q3 -- No --> Q4
Q4 -- Yes --> C
Q4 -- No --> P
When to Use Channels#
- Transferring Ownership:
When a producer generates data and hands it to a consumer. Once sent onto the channel, the producer no longer touches that data, eliminating data races. - Coordination & Composition:
When assembling complex asynchronous workflows like timeouts, context cancellations, or fan-in/fan-out pipelines.
When to Use sync.Mutex#
- Guarding Internal State:
When protecting internal struct fields (e.g., caches, maps, counters). Encapsulating amu sync.Mutexinside the struct is much simpler and more idiomatic than spawning background goroutines and channels. - Performance-Critical Sections:
Channels incur runtime overhead (buffer queues, lock management, scheduling). For simple counter increments or high-frequency memory updates,sync.Mutexorsync/atomicis substantially faster.
Summary#
- CSP was formulated in 1978 by C.A.R. Hoare, establishing explicit input/output communication as the fundamental primitive of concurrency rather than shared memory.
- Go adopted CSP’s communication and guarded commands via Channels and
select, radically simplifying concurrent program structure. - Practical Selection: Use Channels for ownership transfer and workflow coordination; use Mutexes for internal state encapsulation and microsecond performance-critical sections.