I have written a simple slice-based quick sort function in rust but am facing some difficulties making it multi-threaded.
Here's the quicksort function:
fn quicksort<
T: PartialOrd + Clone + Copy + Send,
>(
arr: &mut [T],
) {
let (low,high) = partition(arr);
let len = arr.len();
// Splitting the array into two parts so they can be safely moved into the thread closures.
let (mut first, mut second) = arr.split_at_mut(low);
let mut handles: Vec<std::thread::JoinHandle<()>> = vec![];
if low > 0 {
handles.push(thread::spawn(move || {
quicksort(first);
}));
}
if high < len {
handles.push(thread::spawn(move || {
quicksort(second);
}));
}
for handle in handles {
handle.join();
}
}
With the following partition function:
fn partition<T: PartialOrd + Clone + Copy + std::fmt::Debug + std::fmt::Display>(
arr: &mut [T],
) -> (usize, usize) {
let len = arr.len();
let pivot_index = len / 2;
let pivot = arr[pivot_index];
arr.swap(pivot_index, len);
let mut low = 0;
let mut high = 0;
let mut i = 0;
while i < len {
if arr[i] < pivot {
if i > low {
arr.swap(i, low);
if high != low {
arr.swap(i, high);
}
}
low += 1;
high += 1
} else if arr[i] == pivot {
if i > high {
arr.swap(i, high);
}
high += 1
}
i += 1;
}
(low, high)
}
Upon trying to compile the program, I get the following error:
error[E0621]: explicit lifetime required in the type of `arr`
--> main.rs:180:22
|
164 | arr: &mut [T],
| -------- help: add explicit lifetime `'static` to the type of `arr`: `&'static mut [T]`
...
180 | handles.push(thread::spawn(move || {
| ^^^^^^^^^^^^^ lifetime `'static` required
The 'static
lifetime annotation is not viable since I want to be able to use the function inside function scopes without having to define arr
in the main()
function.
If I understand correctly, because I am calling handle.join()
for both threads, lifetime 'static
should not be required, since &arr
would only go out of scope once the two threads had finished. What can I do to fix this?
I would also like to add that I am still extremely new to rust and that any insight into rust's ownership and lifetime system will be greatly appreciated.