Module crossbeam
Concurrency tools to supplement std::sync, including fast channels.
- Crate
::crossbeam. - docs.rs
- crates.io
- GitHub
crossbeam provides a collection of tools for concurrent programming in Rust.
It supplements the standard library's std::sync and std::thread modules
with high-performance primitives and convenient abstractions.
The crate's primary features include:
- Fast multi-producer multi-consumer channels via
crossbeam::channel - Scoped threads that can borrow from the parent scope via
crossbeam::scope - Lock-free and wait-free data structures
- Utilities like
Backofffor optimizing spin loops - Epoch-based memory reclamation for concurrent data structures
The channel module provides bounded and unbounded channels
that are faster than std::sync::mpsc and support multiple producers
and multiple consumers. Channels can be selected over using the select! macro,
enabling patterns like waiting on multiple channels or implementing timeouts.
The scope function allows spawning threads that can safely access
non-'static data from the parent thread's stack.
This is more ergonomic than std::thread::spawn,
which requires all data to be 'static or moved into the thread.
Examples
Using scoped threads to borrow data from the parent scope:
use crossbeam;
let values = vec!;
let mut total = 0;
scope.unwrap;
assert_eq!;
Communicating between threads using channels:
use channel;
use thread;
let = unbounded;
spawn;
let mut sum = 0;
for received in receiver
assert_eq!; // 0 + 1 + 2 + 3 + 4
Using bounded channels for backpressure:
use channel;
use thread;
let = bounded;
spawn;
sleep;
let values: = receiver.iter.collect;
assert_eq!;
Selecting over multiple channels:
use channel;
use thread;
use Duration;
let = unbounded;
let = unbounded;
spawn;
spawn;
select!
Modules
- utils Miscellaneous utilities.