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
- Is a kind of: Message passing
- Kinds: Buffered and bounded channels, Unbuffered channel
- Is used by: Fan-out, fan-in, Pipeline, Select, Worker pool
- An alternative to: Mutex
- See also: Concurrency primitives, Goroutine
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¶
- In this library: Getting a result back
- In this library: Keeping every update
- In a sibling library: Rust: Channels ↗
- In a sibling library: Go: An unbuffered send waits for a receiver ↗
- In a sibling library: Go: Closing a channel ends a range ↗
- In the books: Rust Atomics and Locks, Mara Bos — ch. 5, 'Building Our Own Channels'
- In the books: Kotlin Coroutines by Tutorials, Filip Babić, Nishant Srivastava — ch. 11, 'Channels'
- In the books: Concurrency in Go, Katherine Cox-Buday — ch. 3, 'Go’s Concurrency Building Blocks' → 'Channels'
- In the books: Multi-Threaded Programming in C++, Mark Walmsley — ch. 10, 'Distributed Computing' → 'The CHANNEL Class'
- In the books: Parallel and Concurrent Programming in Haskell, Simon Marlow — ch. 7, 'Basic Concurrency: Threads and MVars' → 'MVar as a Simple Channel: A Logging Service'
- In the books: Seven Concurrency Models in Seven Weeks, Paul Butcher — ch. 6, 'Communicating Sequential Processes' → 'Day 1: Channels and Go Blocks'
- Notes: channel ↗
- Notes: Channels - rust ↗
- Notes: async-compatible nonblocking channels ↗
- Notes: crossbeam ↗
- Reference: Wikipedia: Channel (programming) ↗