I'm having trouble understanding the concurrency model in Rust coming from C++.
My array is to be accessed concurrently using another array that defines the indices. For example (Pseudocode):
let indices = [1, 2, 3, 4, 1, 2, 3, 2, 1, 1, 3, 2, 2];
let mut arr = [1, 2, 3, 4, 5, 6, 7, 8, 10];
indices.iter_par().for_each(|x| {
arr[x] += x;
});
In C++, I would protect each index in arr
with a lock or use atomic access. How could I do the same in Rust?
EDIT
I have another related question.
How could I pass a normal array as mutable into the parallel iterator, where I'm sure that no race conditions can occur?
let indices = [1, 2, 3, 4, 5, 6, 7, 8];
let mut arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
indices.iter_par().for_each(|x| {
arr[x] = some_function(x);
});