Select¶
Category: Communication · Status: stub · Lessons: chapter 05, Message passing (planned)
One line: Waiting on several channel operations or futures at once, and continuing with whichever becomes ready first.
Also called: select!, race.
How it connects¶
flowchart LR
n_channel["Channel"]
n_select["Select"]
n_select -->|uses| n_channel
classDef center stroke-width:3px
class n_select center
classDef outside stroke-dasharray: 4 3
class n_channel outside
- Is built on: Channel
- See also: Cancellation, Timeout
In each language¶
| Rust | none in std; crossbeam's select! ↗ runs a random one of the ready channel operations, and tokio's select! ↗ returns when the first branch completes and cancels the rest |
| Go | the select ↗ statement: when several cases can proceed, one is chosen by uniform pseudo-random selection; a default case makes it non-blocking |
| C | select ↗ and poll ↗ wait on file descriptors, not on in-memory channels |
| Java | CompletableFuture.anyOf ↗ completes when any of the given futures does; NIO's Selector ↗ multiplexes selectable I/O channels |
| Python | asyncio.wait ↗ with return_when=FIRST_COMPLETED returns the done and pending sets as soon as any awaitable finishes |
| C# | Task.WhenAny ↗ completes when any of the supplied tasks has completed |
| JavaScript | Promise.race ↗ settles with the eventual state of the first promise to settle |
| Kotlin | the experimental select ↗ expression, which is biased to the first clause when several are ready, unlike Go's random choice |
| Erlang and Elixir | receive ↗ takes the first message in the queue that matches any clause, with an optional after timeout |
| The operating system | epoll ↗, like select and poll, monitors many file descriptors to see which can do I/O |
| Elsewhere | Clojure core.async's alts! ↗ |
Where to read more¶
- In a sibling library: Go:
selectwaits on many channels ↗ - In a sibling library: Go:
selectchooses at random ↗ - In a sibling library: Go: A nil channel disables a case ↗
- In the books: Learn Concurrent Programming with Go, James Cutajar — ch. 8, 'Selecting channels'
- In the books: Modern Multithreading, Richard H. Carver, Kuo-Chung Tai — ch. 5, 'Message Passing' → 'Selective Wait'
- In the books: Concurrency in Go, Katherine Cox-Buday — ch. 3, 'Go’s Concurrency Building Blocks' → 'The select Statement'
- In the books: Advanced Programming in the UNIX Environment, W. Richard Stevens, Stephen A. Rago — ch. 14, 'Advanced I/O' → 'select and pselect Functions'
- In the books: Learning Go, Jon Bodner — ch. 12, 'Concurrency in Go' → 'select'
- In the books: The Go Programming Language, Alan A. A. Donovan, Brian W. Kernighan — ch. 8, 'Goroutines and Channels' → 'Multiplexing with select'