Crossbeam Channels
Advanced channels.
Beyond std mpsc
The standard mpsc channel is single consumer: only one receiver. The crossbeam-channel crate offers multi-producer, multi-consumer (mpmc) channels with more features and often better performance.
Highlights:
- Cloneable
Receivers, not just senders. - A powerful
select!macro to wait on many channels. - Bounded, unbounded, and special channels like ticks.
Adding the Dependency
Crossbeam is an external crate, so add it to Cargo.toml:
[dependencies]crossbeam-channel = "0.5"
Then import its functions. Because this needs Cargo and an external crate, the snippets here illustrate the API rather than running standalone.
// Cargo.toml
// [dependencies]
// crossbeam-channel = "0.5"
use crossbeam_channel::unbounded;
fn main() {
let (s, r) = unbounded();
s.send("hello").unwrap();
println!("{}", r.recv().unwrap());
}All lessons in this course
- mpsc Channels
- Sharing State with Arc/Mutex
- Scoped Threads
- Crossbeam Channels