Skip to content

Channel

Category: Communication · Status: stub · Lessons: chapter 05, Message passing (planned)

One line: A typed conduit between tasks: one side sends values, the other receives them, in order.

Also called: queue, mpsc.

How it connects

flowchart LR
  n_bounded_channel["Buffered and bounded channels"]
  n_channel["Channel"]
  n_fan_out_fan_in["Fan-out, fan-in"]
  n_message_passing["Message passing"]
  n_mutex["Mutex"]
  n_pipeline["Pipeline"]
  n_select["Select"]
  n_unbuffered_channel["Unbuffered channel"]
  n_worker_pool["Worker pool"]
  n_bounded_channel -->|is a| n_channel
  n_channel ---|or| n_mutex
  n_channel -->|is a| n_message_passing
  n_fan_out_fan_in -->|uses| n_channel
  n_pipeline -->|uses| n_channel
  n_select -->|uses| n_channel
  n_unbuffered_channel -->|is a| n_channel
  n_worker_pool -->|uses| n_channel
  classDef center stroke-width:3px
  class n_channel center
  classDef outside stroke-dasharray: 4 3
  class n_bounded_channel,n_fan_out_fan_in,n_message_passing,n_mutex,n_pipeline,n_select,n_unbuffered_channel,n_worker_pool outside

In each language

Rust mpsc::channel or the bounded sync_channel; there is no close: operations fail once the other half has hung up by being dropped (module docs ↗)
Go a chan T made with make and closed explicitly with close; a nil channel is never ready
Java BlockingQueue with put and take; it has no close or shutdown operation, so producers signal the end with a poison object
Python queue.Queue for threads and asyncio.Queue, which is not thread-safe, for coroutines
C# Channel<T>, split into a ChannelWriter and a ChannelReader; writer.Complete() ends it
Kotlin Channel: send and receive suspend rather than block, and unlike a queue a channel can be closed
Erlang and Elixir no channel objects: a message is sent to a process and stored in its mailbox ↗
Haskell Chan, unbounded
Elsewhere Clojure's core.async ↗ channels, made with chan

Where to read more