Hi. I was playing around with "multithreading computation" and I was trapped by Rust's strict "memory safety regulations."
What I wanted to achieve was simple. I had an array of size n
of f64
s, and I wanted to take advantage of multithreading to speed up some time-consuming, in-place computation tasks (doesn't really matter what the tasks actually were, just add 1
to each f64
instead). I divided the array as evenly as possible into num_cpu
chunks and send them to num_cou
threads to run concurrently. Since no data-overlapping problem would ever occur, I would assume that no overhead should be introduced (such as Lock
s or Rc
s).
I tried to play around split_at_mut()
, but unfortunately I didn't get my code compiled successfully. Can anyone provide any advice or a minimal working sample? (assumes that the function looks like below)
fn compute(arr: &mut [f64]) { // please modify arr in-place
...
}
Thx!