threads1 solution

This commit is contained in:
mo8it 2024-07-01 10:59:33 +02:00
parent 663a03a17b
commit b000164eed
3 changed files with 52 additions and 12 deletions

View file

@ -3,31 +3,35 @@
// wait until all the spawned threads have finished and should collect their // wait until all the spawned threads have finished and should collect their
// return values into a vector. // return values into a vector.
use std::thread; use std::{
use std::time::{Duration, Instant}; thread,
time::{Duration, Instant},
};
fn main() { fn main() {
let mut handles = vec![]; let mut handles = Vec::new();
for i in 0..10 { for i in 0..10 {
handles.push(thread::spawn(move || { let handle = 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 {i} done");
start.elapsed().as_millis() start.elapsed().as_millis()
})); });
handles.push(handle);
} }
let mut results: Vec<u128> = vec![]; let mut results = Vec::new();
for handle in handles { for handle in handles {
// TODO: a struct is returned from thread::spawn, can you use it? // TODO: Collect the results of all threads into the `results` vector.
// Use the `JoinHandle` struct which is returned by `thread::spawn`.
} }
if results.len() != 10 { if results.len() != 10 {
panic!("Oh no! All the spawned threads did not finish!"); panic!("Oh no! Some thread isn't done yet!");
} }
println!(); println!();
for (i, result) in results.into_iter().enumerate() { for (i, result) in results.into_iter().enumerate() {
println!("thread {} took {}ms", i, result); println!("Thread {i} took {result}ms");
} }
} }

View file

@ -1037,7 +1037,7 @@ hint = """
https://doc.rust-lang.org/std/thread/fn.spawn.html https://doc.rust-lang.org/std/thread/fn.spawn.html
A challenge with multi-threaded applications is that the main thread can A challenge with multi-threaded applications is that the main thread can
finish before the spawned threads are completed. finish before the spawned threads are done.
https://doc.rust-lang.org/book/ch16-01-threads.html#waiting-for-all-threads-to-finish-using-join-handles https://doc.rust-lang.org/book/ch16-01-threads.html#waiting-for-all-threads-to-finish-using-join-handles
Use the `JoinHandle`s to wait for each thread to finish and collect their Use the `JoinHandle`s to wait for each thread to finish and collect their

View file

@ -1 +1,37 @@
// Solutions will be available before the stable release. Thank you for testing the beta version 🥰 // 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,
time::{Duration, Instant},
};
fn main() {
let mut handles = Vec::new();
for i in 0..10 {
let handle = thread::spawn(move || {
let start = Instant::now();
thread::sleep(Duration::from_millis(250));
println!("Thread {i} done");
start.elapsed().as_millis()
});
handles.push(handle);
}
let mut results = Vec::new();
for handle in handles {
// Collect the results of all threads into the `results` vector.
results.push(handle.join().unwrap());
}
if results.len() != 10 {
panic!("Oh no! Some thread isn't done yet!");
}
println!();
for (i, result) in results.into_iter().enumerate() {
println!("Thread {i} took {result}ms");
}
}