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?
use of moved value: `pividx`
value moved into closure here, in previous iteration of loop
note: move occurs because `pividx` has type `std::sync::atomic::AtomicUsize`, which does not implement the `Copy` traitrustc(E0382)
New Code:
fn partition_chunks_parallel(arr: &mut [i32], chunk_size: usize, pool: &mut Pool) -> usize {
let piv = arr[0];
let mut pividx = AtomicUsize::new(0);
pool.scoped(|scope| {
for (i, chunk) in arr.chunks_mut(chunk_size).enumerate() {
scope.execute(move || {
let new_pividx = partition_chunk(chunk, piv);
if i == 0 {
*pividx.get_mut() = new_pividx;
}
});
}
});
pividx.into_inner()
}
Hi, thank you, both of you. Life kept me busy, which is why I'm only responding now. It turned out I actually needed a vector of usize and not just one usize, so I did solve it a little differently using mutexes. Unless I'm wrong and that is also possible with atomics? Nonetheless your answers are very illuminating. I just noticed that the scoped_threadpool library I used is terribly outdated. Next time I'll use rayon.