chore: reorg

This commit is contained in:
2020-10-17 11:53:39 +08:00
parent 899428370b
commit af97622c79
12 changed files with 0 additions and 0 deletions

5
__std/thread/Cargo.lock generated Normal file
View File

@@ -0,0 +1,5 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "thread-test"
version = "0.1.0"

9
__std/thread/Cargo.toml Normal file
View File

@@ -0,0 +1,9 @@
[package]
name = "thread-test"
version = "0.1.0"
authors = ["Hatter Jiang@Pixelbook <jht5945@gmail.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

38
__std/thread/src/main.rs Normal file
View File

@@ -0,0 +1,38 @@
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!");
}