use std::{sync::mpsc, time::Duration};

fn producer(tx: mpsc::Sender<u32>) {
    println!("Producer started");
    for i in 0..10 {
        std::thread::sleep(Duration::from_secs(1));
        tx.send(i).ok();
    }
    println!("Producer finished");
}

fn consumer(rx: mpsc::Receiver<u32>) {
    println!("Consumer started");
    while let Ok(msg) = rx.recv() {
        println!("Consumer: {msg}");
    }
    println!("Consumer finished");
}

fn main() {
    let (tx, rx) = mpsc::channel();
    let jh0 = std::thread::spawn(move || producer(tx));
    let jh1 = std::thread::spawn(move || consumer(rx));

    jh0.join().unwrap();
    jh1.join().unwrap();
    println!("Finished");
}
