← Lessons

quiz vs the machine

Platinum1820

Concurrency

The Channel And Select Pattern

Typed pipes that pass values between concurrent tasks, with select waiting on whichever is ready first.

6 min read · advanced · beat Platinum to climb

Communicating by sending values

A channel is a typed conduit through which one task sends values and another receives them. Rather than sharing memory and locking it, tasks share by communicating. A send blocks or buffers until a receiver is ready, and a receive blocks until a value arrives. This turns coordination into simple send and receive.

Buffered and unbuffered

Channels come in two flavors.

  • An unbuffered channel hands a value directly, so send and receive rendezvous, synchronizing the two tasks.
  • A buffered channel holds a fixed number of pending values, letting senders run ahead until the buffer fills.

Closing a channel signals that no more values will come, which receivers can detect.

Selecting among many

Real systems wait on several channels at once. The select construct blocks until any one of its cases is ready, then runs that case.

  • Wait on a receive from several channels.
  • Include a timeout case so waiting cannot last forever.
  • Add a default case to avoid blocking when nothing is ready.

Select is how a task multiplexes inputs and reacts to whichever event fires first.

Key idea

Channels let concurrent tasks share by sending typed values rather than sharing memory, and select waits on multiple channels at once, running whichever case becomes ready first.

Check yourself

Answer to earn rating on the learn ladder.

1. What is the core philosophy behind channels?

2. How does an unbuffered channel differ from a buffered one?

3. What does select do?