✉️ Rust Channels
= one way, thread-safe queue between threads, multiproducer, single-consumer pattern
How?
Values that can be sent:
- Implement
std::marker::Send
: safe to pass by value - Implement
std::marker::Sync
: safe to pass by non-mut reference
Normal channels
let (sender, receiver) = mpsc::channel()
sender.send(item)
,receiver.recv()
-> iterator- Multiple:
- senders: clone the sender
- receivers: 🔒 Rust Mutex
Synchronous channels
Channels, that have a fixed size (to avoid ever-growing backlog due to slow receiver)
let (sender, receiver) = mpsc::sync_channel(size)