#include <iostream>
#include <vector>
#include <thread>
void mergeSort(std::vector<int>& v, int left, int right) {
if (left < right) {
int mid = left + (right - left) / 2;
std::thread t1(mergeSort, std::ref(v), left, mid);
std::thread t2(mergeSort, std::ref(v), mid + 1, right);
t1.join();
t2.join();
merge(v, left, mid, right);
}
}
I tried a multi threading approach for the merge sort algorithm, but when testing the difference between multi threaded and iterative even with inputs as large as 1M the iterative approach was way faster. What is wrong with my algorithm and how can I make it work as expected?
With threads, it's important to consider the following:
In your case, I think you're ending up spawning far, far more threads than your system can realistically handle. Let's take the simplest example and say you're on a dual-core machine; you would want to split your work across two threads for maximum throughput.
You might want something like:
This would start the first level across two threads, and each would recurse down half of the tree.
Assuming this works (untested), your next step would be to make it start threads as far as you have useful hardware to execute them. But I'd check that you understand this level first.