Thanks to buddies in #57126, I can finally setup a basic parallel thread model in rust.
On this basis I would like to propose some further requirements. In the previous topic, I wanted to run three threads in parallel to compute the mean, median and standard deviation of an Vec<f64>
. Now let's assume a more practical situation, suppose that we have a dual-core CPU, and four task to be computed (assume that they're mean/median/stddev/variance), I want to manage a simple thread pool to achieve the fastest execution time.
Spontaneously, we want to keep two worker threads running at a time(each runs on a actual CPU core), and we got a main thread as master, use std::sync::mpsc
to make worker run and get return vales.
I tried to implement this logic from the following code:
fn main() {
let mut threads:Vec<thread::JoinHandle<f64>> = Vec::new();
// open a thread pool
for index in 0..num {
threads.push(
thread::spawn(move || {
100.0
})
)
}
// do some stuff
// close the pool
for index in 0..num {
threads[index].join();
}
}
However I got a error message:
error[E0507]: cannot move out of index of `Vec<JoinHandle<f64>>`
--> src\main.rs:16:9
|
16 | threads[index].join();
| ^^^^^^^^^^^^^^ move occurs because value has type `JoinHandle<f64>`, which does not implement the `Copy` trait
What should I do?