The standard library only has multiple producer, single consumer (MPSC) channels.
That is, if there are 10 consumers, how can every consumer receive a message sent by a single producer?
Check out the bus crate that @CodesInChaos mentioned. A working example:
extern crate bus;
use std::thread;
fn main() {
let mut bus = bus::Bus::new(10);
let mut receiver1 = bus.add_rx();
let mut receiver2 = bus.add_rx();
let mut receiver3 = bus.add_rx();
let a = thread::spawn(move || {
println!("receiver1 - {}", receiver1.recv().expect("1"));
});
let b = thread::spawn(move || {
println!("receiver2 - {}", receiver2.recv().expect("2"));
});
let c = thread::spawn(move || {
println!("receiver3 - {}", receiver3.recv().expect("3"));
});
bus.broadcast(42);
a.join().expect("a");
b.join().expect("b");
c.join().expect("c");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With