use std::thread;
use std::sync::{Arc, Mutex};
use std::time::Duration;

fn main() {
    let counter1 = Arc::new(Mutex::new(0));
    let counter2 = Arc::new(Mutex::new(0));

    let a = Arc::clone(&counter1);
    let b = Arc::clone(&counter2);
    let handle1 = thread::spawn(move || {
        // let lock1 = a.lock().unwrap();
        // println!("counter1: {}", *lock1);
        let lock1 = *a.lock().unwrap();
        println!("counter1: {}", lock1);
        thread::sleep(Duration::from_millis(100));
        let lock2 = b.lock().unwrap();
        println!("counter2: {}", *lock2);
    });

    let a2 = Arc::clone(&counter2);
    let b2 = Arc::clone(&counter1);
    let handle2 = thread::spawn(move || {
        // let lock2 = a2.lock().unwrap();
        // println!("counter2: {}", *lock2);
        let lock2 = *a2.lock().unwrap();
        println!("counter2: {}", lock2);
        thread::sleep(Duration::from_millis(100));
        let lock1 = b2.lock().unwrap();
        println!("counter1: {}", *lock1);
    });

    let _ = handle1.join();
    let _ = handle2.join();
}