janhohenheim/rust-standard-library-cookbook

parallelism.rs

tinh-phan opened this issue · 1 comments

`use std::thread;
use std:🧵:JoinHandle;

fn main() {
let child = thread::spawn(|| println!("Hello from a new thread!"));
println!("Hello from the main thread!");
child.join().expect("Failed to join the child thread");

let sum = parallel_sum(&[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]);
println!("The sum of the numbers 1 to 12 is {}", sum);

}

fn parallel_sum(range: &[i32]) -> i32 {
const NUM_THREADS: usize = 4;

if range.len() < NUM_THREADS {
    sum_bucket(range)
} else {
    let bucket_size = range.len() / NUM_THREADS;
    let mut count = 0;
    let mut threads: Vec<JoinHandle<i32>> = Vec::new();

    while count + bucket_size <= range.len() {
        println!("Run");
        let bucket = range[count..count + bucket_size].to_vec();

        let thread = thread::Builder::new().name("calculation".to_string()).spawn(move || {
            println!("Work");

            return sum_bucket(&bucket);

        })
            .expect("Failed to create the thread");
        threads.push(thread);

        count += bucket_size
    }

    let mut sum = sum_bucket(&range[count..]);

    for thread in threads {
        sum += thread.join().expect("Failed to join thread");
    }

    sum
}

}

fn sum_bucket(range: &[i32]) -> i32 {
let mut sum = 0;
for num in range {
sum += num;
}
sum
}`

Hi there! How may I help you?