Simple question, but I can't for the life of me figure out how to do this. I have a variable I want to update inside the thread but the compiler tells me pividx does not live long enough
. If possible I'd even like to do this without mutexes, as only one of the threads needs to access pividx.
fn partition_chunks_parallel(arr: &mut [i32], chunk_size: usize, pool: &mut Pool) -> usize {
let piv = arr[0];
let pividx = Arc::new(Mutex::new(0));
pool.scoped(|scope| {
for (i, chunk) in arr.chunks_mut(chunk_size).enumerate() {
let pividx_clone = pividx.clone();
scope.execute(move || {
let new_pividx = partition_chunk(chunk, piv);
if i == 0 {
*pividx.lock().unwrap() = new_pividx;
}
});
}
});
*pividx.lock().unwrap()
}
Can someone help?
Also, if I simply declare pividx as a mut usize
, it will let me update inside the thread pool, but tells me the variable is unused. The variable is also not updated. Does that mean a new mutable variable for each thread is created temporarily?