fn sm() {
        let counter = Arc::new(Mutex::new(0));
        let mut handles = vec![];

        for _ in 0..10 {
            let counter = Arc::clone(&counter);
            let handle = thread::spawn(move || {
                let mut num = counter.lock().unwrap();
                *num += 1;
            });
            handles.push(handle);
        }

        for handle in handles {
            handle.join().unwrap();
        }
        println!("Result: {}", *counter.lock().unwrap());
    }
// sharing a mutex<T> between multiple threads
sharing a mutex between multiple threads
sharing a mutex between multiple threads
fn sm() {
        let counter = Arc::new(Mutex::new(0));
        let mut handles = vec![];
        for _ in 0..10 {
            let counter = Arc::clone(&counter);
            let handle = thread::spawn(move || {
                let mut num = counter.lock().unwrap();
                *num += 1;
            });
            handles.push(handle);
        }
        for handle in handles {
            handle.join().unwrap();
        }
        println!("Result: {}", *counter.lock().unwrap());
    }
fn sm ( ) { let counter = Arc :: new ( Mutex :: new ( 0 ) ) ; let mut handles = vec ! [ ] ; for _ in 0 .. 10 { let counter = Arc :: clone ( & counter ) ; let handle = thread :: spawn ( move | | { let mut num = counter . lock ( ) . unwrap ( ) ; * num += 1 ; } ) ; handles . push ( handle ) ; } for handle in handles { handle . join ( ) . unwrap ( ) ; } println ! ( " " counter lock ( ) unwrap ( ) ) ; }
sharing a mutex<T> between multiple threads
sharing a mutex<T > between multiple threads