https://github.com/smol-rs/easy-parallel
Run closures in parallel
https://github.com/smol-rs/easy-parallel
rust
Last synced: about 1 year ago
JSON representation
Run closures in parallel
- Host: GitHub
- URL: https://github.com/smol-rs/easy-parallel
- Owner: smol-rs
- License: apache-2.0
- Created: 2020-05-26T22:06:57.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2025-01-19T16:42:29.000Z (over 1 year ago)
- Last Synced: 2025-05-08T00:08:53.205Z (about 1 year ago)
- Topics: rust
- Language: Rust
- Homepage:
- Size: 38.1 KB
- Stars: 113
- Watchers: 3
- Forks: 9
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE-APACHE
Awesome Lists containing this project
README
# easy-parallel
[](
https://github.com/smol-rs/easy-parallel/actions)
[](
https://github.com/smol-rs/easy-parallel)
[](
https://crates.io/crates/easy-parallel)
[](
https://docs.rs/easy-parallel)
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:
```rust
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:
```rust
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:
```rust
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
* Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
* MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
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.