I have two slices that are passed from another method:
fn example<T>(a1: &[T], a2: &mut [T]) {}
I want to process a1
with multiple threads and then write to a2
using completely arbitrary indices that are only known when each thread is executed. The indices are guaranteed by my algorithm to be mutually exclusive, so there is no data race.
The borrow checker doesn't like sharing mutable references among threads since it doesn't know of the guarantee our algorithm makes. I also get a lifetime 'static required rustc (E0621)
error.
So how to do this in Rust?
Answers to
- How can I pass a reference to a stack variable to a thread?
- Simultaneous mutable access to arbitrary indices of a large vector that are guaranteed to be disjoint
- How do I pass disjoint slices from a vector to different threads?
- How do I run parallel threads of computation on a partitioned array?
- How to get mutable references to two array elements at the same time?
Do not answer my question.
The answer to the first question addresses the scoping problem, but not the problem of accessing arbitrary mutually disjoint indexes. The answer to the second question suggests as_slice_of_cells
but that doesn't work here because of the aforementioned reason, namely the arbitrary access. The answer to the third question similarly suggests as_slice_of_cells
but again, the assumption that the array can be split into disjoint parts cannot be fulfilled here. The fourth question again asks about partitioning the array, which we cannot do here. And the same applies to the fifth question.
One answer to the scoping problem (https://stackoverflow.com/a/64502824/10056727) actually attempts to address this problem, but it doesn't suggest to use crossbeam and the alternative suggested is more unsafe than the top answer here.