let mut output =Mutex::new(
vec![vec![vec![255u8, 24u8, 157u8]; ini.3[1] as usize]; ini.3[0] as usize]);
...code...
//ignore the fact that it is a 3 dimensional vector. It won't matter in this case perfomance wise.
let mut rayon_needs_a_vec_for_par_iter=Vec::with_capacity(ini.3[0] as usize+1);
for x in 0..ini.3[0] as usize{
rayon_needs_a_vec_for_par_iter.push(x)
}
rayon_needs_a_vec_for_par_iter.par_iter().for_each (|i|{
for j in 0..ini.3[1] as usize {
...code...
output.lock().unwrap()[(*i)][j] =some_value_after_computation(*i,j)
}
});
This code compiles and works perfectly. Why didn't rayon need Arc Mutex for output variable. How come is it able to work with output just being Mutex.
What does rayon do with the closure internally that it is able to use this output variable in multiple threads without Arc.
How come we can have multiple some_value_after_computation(*i,j)
running at the same time.