7 stable releases

3.1.0 Jul 22, 2020
3.0.0 May 28, 2020
2.2.0 May 27, 2020
1.0.1 May 26, 2020

#368 in Concurrency

Download history 55/week @ 2020-05-29 33/week @ 2020-06-05 13/week @ 2020-06-12 39/week @ 2020-06-19 14/week @ 2020-06-26 107/week @ 2020-07-03 63/week @ 2020-07-10 381/week @ 2020-07-17 1101/week @ 2020-07-24 1451/week @ 2020-07-31 2207/week @ 2020-08-07 2666/week @ 2020-08-14 2444/week @ 2020-08-21 2437/week @ 2020-08-28 1663/week @ 2020-09-04 1868/week @ 2020-09-11

154 downloads per month
Used in 19 crates (6 directly)

Apache-2.0 OR MIT

11KB
103 lines

easy-parallel

Build License Cargo Documentation

Run closures in parallel.

This is a simple primitive for spawning threads in bulk and waiting for them to complete. Threads are allowed to borrow local variables from the main thread.

Examples

Run two threads that increment a number:

use easy_parallel::Parallel;
use std::sync::Mutex;

let mut m = Mutex::new(0);

Parallel::new()
    .add(|| *m.lock().unwrap() += 1)
    .add(|| *m.lock().unwrap() += 1)
    .run();

assert_eq!(*m.get_mut().unwrap(), 2);

Square each number of a vector on a different thread:

use easy_parallel::Parallel;

let v = vec![10, 20, 30];

let squares = Parallel::new()
    .each(0..v.len(), |i| v[i] * v[i])
    .run();

assert_eq!(squares, [100, 400, 900]);

Compute the sum of numbers in an array:

use easy_parallel::Parallel;

fn par_sum(v: &[i32]) -> i32 {
    const THRESHOLD: usize = 2;

    if v.len() <= THRESHOLD {
        v.iter().copied().sum()
    } else {
        let half = (v.len() + 1) / 2;
        let sums = Parallel::new().each(v.chunks(half), par_sum).run();
        sums.into_iter().sum()
    }
}

let v = [1, 25, -4, 10, 8];
assert_eq!(par_sum(&v), 40);

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

No runtime deps