Files
Rust-High-Performance/Chapter10/example12.rs
2018-03-28 11:19:46 +05:30

29 lines
698 B
Rust

use std::thread;
use std::sync::{Arc, Mutex};
fn main() {
let my_vec = Arc::new(Mutex::new(Vec::new()));
let t_vec = my_vec.clone();
let handle = thread::Builder::new()
.name("my thread".to_owned())
.spawn(move || {
for i in 0..50 {
t_vec.lock().unwrap().push(i);
}
})
.expect("could not create the thread");
for i in 0..50 {
my_vec.lock().unwrap().push(i);
}
if handle.join().is_err() {
println!("Something bad happened :(");
}
let vec_mutex = Arc::try_unwrap(my_vec).unwrap();
let f_vec = vec_mutex.into_inner().unwrap();
println!("Final vector: {:?}", f_vec);
}