You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
32 lines
610 B
32 lines
610 B
2 years ago
|
use std::sync::mpsc;
|
||
|
use std::thread;
|
||
|
|
||
|
fn main() {
|
||
|
let (tx, rx) = mpsc::channel();
|
||
|
let self_tx = tx.clone();
|
||
|
|
||
|
let consumer = thread::spawn(move || {
|
||
|
while let Ok(msg) = rx.recv() {
|
||
|
if msg == 0 {
|
||
|
println!("Quit");
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
println!("Message: {}", msg);
|
||
|
}
|
||
|
});
|
||
|
|
||
|
let producer = thread::spawn(move || {
|
||
|
for i in 1..12 {
|
||
|
tx.send(i);
|
||
|
}
|
||
|
tx.send(0);
|
||
|
});
|
||
|
|
||
|
println!("Waiting producer...");
|
||
|
producer.join();
|
||
|
println!("Waiting consumer...");
|
||
|
consumer.join();
|
||
|
|
||
|
}
|