Browse Source

Stupid way to re-enter data

master
Julio Biason 2 years ago
parent
commit
8e0474ccd2
  1. 30
      prodconrs/src/main.rs

30
prodconrs/src/main.rs

@ -7,25 +7,33 @@ fn main() {
let consumer = thread::spawn(move || { let consumer = thread::spawn(move || {
while let Ok(msg) = rx.recv() { while let Ok(msg) = rx.recv() {
if msg == 0 { println!("Message: {}", msg);
println!("Quit");
if msg > 1000 {
// actually, we just need to drop self_tx, otherwise the consumer will keep waiting
// for inputs from it, even when tx was already dropped when the producer ended.
// the problem with a direct drop is that rustc can't see that it won't be used
// anymore.
break; break;
} else if msg % 2 == 0 {
if self_tx.send(msg * 2).is_err() {
println!("Failed to push new value to consumer");
break;
};
} }
println!("Message: {}", msg);
} }
}); });
let producer = thread::spawn(move || { let producer = thread::spawn(move || {
for i in 1..12 { for i in 1..12 {
tx.send(i); if tx.send(i).is_err() {
println!("Failed to send {}, ending producer", i);
break;
} }
tx.send(0); }
// tx.send(0);
}); });
println!("Waiting producer..."); producer.join().unwrap();
producer.join(); consumer.join().unwrap();
println!("Waiting consumer...");
consumer.join();
} }

Loading…
Cancel
Save