Without pulling in all of rayon / crossbeam, is there a quick / simple way to do:
- here is a work queue
- start N threads, do work, return value
- wait for all threads to finish
Addendum: Basically, something like:
let x: Vec<T> = ...;
let y = x.par_map(f, num_threads).collect::<Vec<_>>();
Make a vector of jobs, put it in an
Mutex, spawn N threads, and have each thread keep popping items from the vector until it finished. To get back the items, probably use a channel (or return a vector of results from each thread), though you'll need separate handling if you need them in the same order as they were in originally.
f: T -> U
x: Vec<T> (input)
y: Vec<U> (output)
- U does not implement default
is there a nice way to preallocate y, then make each job take a
(&T, &mut Uninit<U>) so it directly writes to the right location ?
I am thinking of maybe going with Vec in std::vec - Rust
pub unsafe fn from_raw_parts(
ptr: *mut T,
) -> Vec<T, Global>
The question here now is, how do I even do a "malloc" for 'n' elements of 'T' (with proper alignment) ?
You can just do
Vec::with_capacity and then use raw pointers to make the uninit references.