Given the following code:
use std::sync::mpsc::{channel, Sender, Receiver}; use std::thread; fn transceiver( tx: Sender<u32>, tx_string: &str, rx: Receiver<u32>, rx_string: &str, ) { let message_count = 3; for message in 0..message_count { println!("message {}: {}", message, tx_string); tx.send(message).unwrap(); println!("message {}: {}", rx.recv().unwrap(), rx_string); } } fn main() { let (atx, arx) = channel(); let (btx, brx) = channel(); thread::spawn(move || { transceiver(atx, "A --> B", brx, "A <-- B"); }); thread::spawn(move || { transceiver(btx, "B --> A", arx, "B <-- A"); }); }
I have no conclusion. I had to add a delay at the end of main :
std::old_io::timer::sleep(std::time::duration::Duration::seconds(1));
After that, I get this output:
message 0: B
Doc says that these threads should outlive their parents, but they seem to die as soon as the parent (in this case, main ) dies.
source share