Rust bounded worker pool over item chunks
Rust
use std::thread;
fn pool<F>(items: Vec<u32>, workers: usize, f: F) -> Vec<Result<u32, String>>
where
F: Fn(u32) -> Result<u32, String> + Sync,
{
let workers = workers.max(1).min(items.len().max(1));
let chunk = ((items.len() + workers - 1) / workers).max(1);
let mut out = Vec::new();
thread::scope(|s| {
let mut handles = Vec::new();
for part in items.chunks(chunk) {
let f = &f;
handles.push(s.spawn(move || part.iter().map(|&x| f(x)).collect::<Vec<_>>()));
}
for handle in handles {
out.extend(handle.join().unwrap());
}
});
out
}