You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It would be useful if rust provided a channel that supported a single sender and multiple receivers. Specifically, a channel where any item sent on the channel is received by exactly one receiver (i.e. not broadcast).
One example of where this might be used is to have a worker pool for a TCP server:
use std::net;use std::thread;use std::sync::mpsc;let listener = net::TcpListener::bind("127.0.0.1:80").unwrap();fnhandle_clients(streams: mpsc::Receiver<net::TcpStream>){for s in streams.iter(){// handle s}}let(tx, rx) = mpsc::channel();let pool = (1..10).map(|_| {let local_rx = rx.clone();
thread::spawn(move || {handle_clients(local_rx);});}).collect();// accept connections and process them, spawning a new thread for each onefor stream in listener.incoming(){match stream {Ok(stream) => {
tx.send(stream);}Err(e) => {/* connection failed */}}}// close the socket serverdrop(listener);
Since Receiver is not Sync nor Copy, this obviously doesn't currently work. There has been some discussion in #2158 on a similar topic, though the objections there seem to be about full-duplex channels rather than single-writer-multi-reader channels.
The text was updated successfully, but these errors were encountered:
This is a dupe of rust-lang/rfcs#848, so closing in favor of that, but @jdm's suggestion is the current method to get a "multi-receiver" situation as well.
It would be useful if rust provided a channel that supported a single sender and multiple receivers. Specifically, a channel where any item sent on the channel is received by exactly one receiver (i.e. not broadcast).
One example of where this might be used is to have a worker pool for a TCP server:
Since
Receiver
is notSync
norCopy
, this obviously doesn't currently work. There has been some discussion in #2158 on a similar topic, though the objections there seem to be about full-duplex channels rather than single-writer-multi-reader channels.The text was updated successfully, but these errors were encountered: