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

fn sleep_sort(in_vec: Vec<u8>) -> Vec<u8> {
    let out_vec = Arc::new(Mutex::new(vec![]));

    let mut all_threads = vec![];
    for i in in_vec {
        let o_vec = out_vec.clone();
        let child = thread::spawn(move || {
            thread::sleep(Duration::from_millis(i as u64));
            let mut v = o_vec.lock().unwrap();
            v.push(i);
        });
        all_threads.push(child);
    }
    for t in all_threads {
        t.join().ok();
    }

    let v = out_vec.lock().unwrap();
    v.to_vec()
}

// sleep sort
fn main() {
    let in_vec = vec![2, 1, 5, 10, 7];
    let out_vec = sleep_sort(in_vec);

    out_vec.iter().for_each(|i| println!("{}", i));

    println!("Finished!");
}


