I have a Vec<u8>
of length L
. I want to set mutually exclusive (non-overlapping) regions of this vector from multiple threads (let's say threads are 4, so there will be L / 4 sized regions, assuming for now that L is multiple of 4) just like how I can do it with copy_from_slice ? Because the size of the vector won't change I don't think there will be any problem of dangling pointer and stuff which rust ownership would prevent so whats the best safe (or if not unsafe then) way to achieve this (preferably using rayon threadpool) ?
2 thread analogue can be following:
use std::thread;
fn main() {
let mut v = vec![0; 1024];
let (left, right) = v.split_at_mut(1024 / 2);
thread::scope(|s| {
s.spawn(move || {
left.copy_from_slice(&vec![1; 1024 / 2]);
});
s.spawn(move || {
right.copy_from_slice(&vec![2; 1024 / 2]);
});
});
println!("vec: {:?}", v);
}
I want to have mutable chunks dynamically based on the number of threads