CSP — Explained with Examples
CSP (Communicating Sequential Processes) is a concurrent model where processes communicate through channels rather than shared memory.
Communicating Sequential Processes (CSP) was formalized by Tony Hoare. In CSP, concurrent processes are independent and communicate exclusively through channels. A process sends a value into a channel, and another process receives it. Channel operations can be synchronous (blocking until both sender and receiver are ready) or buffered (asynchronous with a bounded queue). Go’s goroutines and channels are the most famous CSP implementation.
Think of CSP like a factory assembly line with conveyor belts (channels) between workstations (processes). Worker A places a part on the belt (send), and Worker B picks it up (receive). Workers do not reach into each other’s workspace — all communication happens through the belt. This eliminates shared-state bugs.
CSP differs from the Actor Model: in CSP, channels are first-class objects and communication typically involves explicit channel names. In the Actor Model, actors have implicit addresses (mailboxes).
package main
import "fmt"
func producer(ch chan<- int, done chan<- bool) {
for i := 0; i < 5; i++ {
ch <- i // Send to channel
}
done <- true
}
func consumer(ch <-chan int, done <-chan bool) {
for {
select {
case val := <-ch:
fmt.Printf("Received: %d\n", val)
case <-done:
fmt.Println("Done!")
return
}
}
}
func main() {
ch := make(chan int, 3) // Buffered channel
done := make(chan bool)
go producer(ch, done)
consumer(ch, done)
}CSP encourages writing concurrent programs as networks of communicating processes, making them easier to reason about, test, and compose.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro