mirror of
https://github.com/notohh/rustlings.git
synced 2024-11-22 14:02:22 -05:00
33 lines
1,019 B
Rust
33 lines
1,019 B
Rust
// This program spawns multiple threads that each run for at least 250ms, and
|
|
// each thread returns how much time they took to complete. The program should
|
|
// wait until all the spawned threads have finished and should collect their
|
|
// return values into a vector.
|
|
|
|
use std::thread;
|
|
use std::time::{Duration, Instant};
|
|
|
|
fn main() {
|
|
let mut handles = vec![];
|
|
for i in 0..10 {
|
|
handles.push(thread::spawn(move || {
|
|
let start = Instant::now();
|
|
thread::sleep(Duration::from_millis(250));
|
|
println!("thread {} is complete", i);
|
|
start.elapsed().as_millis()
|
|
}));
|
|
}
|
|
|
|
let mut results: Vec<u128> = vec![];
|
|
for handle in handles {
|
|
// TODO: a struct is returned from thread::spawn, can you use it?
|
|
}
|
|
|
|
if results.len() != 10 {
|
|
panic!("Oh no! All the spawned threads did not finish!");
|
|
}
|
|
|
|
println!();
|
|
for (i, result) in results.into_iter().enumerate() {
|
|
println!("thread {} took {}ms", i, result);
|
|
}
|
|
}
|