`mpsc`
mpsc means multi-producer, single-consumer. The standard library channel clones the sender and keeps one receiver. It is the default thread-to-thread queue.
Detailed Explanation
std::sync::mpsc is named for its topology. Many threads may hold a Sender (or SyncSender). One thread holds the Receiver. That matches a worker that owns state and accepts commands: each client clones tx, the worker loops on rx.
channel() is asynchronous in the producer sense: send does not wait for recv. The queue grows. sync_channel(n) is the bounded form; send waits when the buffer is full. try_send and try_recv are the non-blocking variants and return immediately with a full/empty error.
Disconnect is symmetric. If the receiver is dropped, send returns Err(SendError(value)) and gives you the value back. If all senders are dropped, recv returns Err(RecvError). A worker loop is usually while let Ok(msg) = rx.recv().
mpsc is not a broadcast. A second consumer needs a different design: a dedicated fan-out thread, crossbeam channels that are multi-consumer, or tokio broadcast. Cloning the receiver does not compile. That compile error is the type system encoding the topology.
Inside async code, std::sync::mpsc::recv blocks the executor thread. tokio::sync::mpsc is the matching topology with recv().await. The names are the same on purpose. The runtimes are not interchangeable.
When you reach for std mpsc: OS threads, a single worker. When you reach for a bounded channel: you want backpressure. When you reach for tokio mpsc: tasks, not threads.
Try to keep the worker's loop the only recv site. Two threads sharing a mental model of "who reads" without sharing the Receiver will fight you at compile time, which is good. If you need several workers, spawn several receivers on several channels, or use a crate whose channel is multi-consumer, and be honest that work can then be processed out of order.
Code Examples
use std::sync::mpsc;
use std::thread;
fn main() {
let (tx, rx) = mpsc::channel();
let tx2 = tx.clone();
thread::spawn(move || { tx.send(1).unwrap(); });
thread::spawn(move || { tx2.send(2).unwrap(); });
let a = rx.recv().unwrap();
let b = rx.recv().unwrap();
println!("{}", a + b); // prints: 3
}Common Gotchas
You cannot clone the std `Receiver`
The type is single-consumer. Split work some other way.
`channel()` has no capacity limit
A stuck consumer plus a hot producer is a growing `Vec` inside the channel.
`SendError` returns the message
On a dropped receiver you can log the value or persist it. It is not lost inside the `Err`.
Mixing std `recv` with Tokio is a blocked worker
Use the tokio channel in async functions.