On this page
mpsc
Prev: `RwLock` | Up: sync | Next: `OnceLock`
Rel: sync, rust/builtins
Ref: Rust std — mpsc
Tags: #public #prog #rust #stdlib #sync #tier/core
Signature & Overview
mpsc::channel::<T>() /* + sync_channel(bound) */
Multi-producer, single-consumer FIFO channel for message passing between threads.
Notes & Pitfalls
- Multi-producer, single-consumer channel: Clone
txfor multiple sender threads;rxcloses cleanly once alltxhandles are dropped. - Use
sync_channel(bound)for backpressure so fast producers don't exhaust memory.
Example
use std::sync::mpsc;
use std::thread;
fn main() {
let (tx, rx) = mpsc::channel();
thread::spawn(move || { tx.send("ping").unwrap(); }).join().unwrap();
assert_eq!(rx.recv().unwrap(), "ping");
}
Linked from
Related notes
- tuts/rust/std/sync/OnceLock Linked here; links to this page; shared tags: #prog, #rust, #stdlib, #sync, #tier
- tuts/rust/std/sync/RwLock Linked here; links to this page; shared tags: #prog, #rust, #stdlib, #sync, #tier
- tuts/rust/std/sync Linked here; links to this page; shared tags: #prog, #rust, #stdlib, #sync
- tuts/rust/builtins Linked here; shared tags: #prog, #rust
- tuts/rust/tutorials/concurrent_worker_pool_channels links to this page; shared tags: #prog, #rust
- tuts/rust/std/sync/AtomicUsize shared tags: #prog, #rust, #stdlib, #sync, #tier
- tuts/rust/std/sync/Mutex shared tags: #prog, #rust, #stdlib, #sync, #tier
- tuts/rust/std/collections/BinaryHeap shared tags: #prog, #rust, #stdlib, #tier
- tuts/rust/std/collections/BTreeMap shared tags: #prog, #rust, #stdlib, #tier
- tuts/rust/std/collections/HashMap shared tags: #prog, #rust, #stdlib, #tier