Finish thread1

This commit is contained in:
Dylan Smith
2024-04-19 15:55:02 -04:00
parent 5484d34a98
commit f222629213

View File

@@ -13,19 +13,22 @@
use std::thread; use std::thread;
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
fn main() { fn thread_func(i: u32) -> u128 {
let mut handles = vec![];
for i in 0..10 {
handles.push(thread::spawn(move || {
let start = Instant::now(); let start = Instant::now();
thread::sleep(Duration::from_millis(250)); thread::sleep(Duration::from_millis(250));
println!("thread {} is complete", i); println!("thread {} is complete", i);
start.elapsed().as_millis() start.elapsed().as_millis()
})); }
fn main() {
let mut handles = vec![];
for i in 0..10 {
handles.push(thread::spawn(move || thread_func(i)));
} }
let mut results: Vec<u128> = vec![]; let mut results: Vec<u128> = vec![];
for handle in handles { for handle in handles {
results.push(handle.join().unwrap());
// TODO: a struct is returned from thread::spawn, can you use it? // TODO: a struct is returned from thread::spawn, can you use it?
} }