I want to write a small program to learn C++ 11 multi-thread program. so I decided to write a mergeSort to test it.
Here is my code:
class SortMethods
{
protected:
int N;
int *aux;
public:
void mergeSort(int a[])
{
aux = new int[N];
mergeSort(a, 0, N - 1);
}
void merge(int a[], int low, int high)
{
int mid = (low + high) / 2;
//optimization 3 for nearly-sorted array
//we can add a condition to improve performance when the array has already sorted or nearly-sorted.
if (a[mid] <= a[mid + 1])
{
return;
}
int i = low;
int j = mid + 1;
for (int k = low; k <= high; k++)
{
aux[k] = a[k];
}
for (int k = low; k <= high; k++)
{
if (i > mid)
a[k] = aux[j++];
else if (j > high)
a[k] = aux[i++];
else if (lessThan(aux[j], aux[i]))
a[k] = aux[j++];
else
a[k] = aux[i++];
}
}
void mergeSort(int a[], int low, int high)
{
if (high <= low)
{
return;
}
int mid = low + (high - low) / 2;
//single_thread
mergeSort(a, low, mid);
mergeSort(a, mid + 1, high);
/*merge(a, low, high);*/
//multi_thread
/*thread left(mergeSort, a, low, mid);
thread right(mergeSort, a, mid + 1, high);
left.join();
right.join();*/
merge(a, low, high);
}
}
int main()
{
int *a = new int(100);
for(int i=0; i<100; i++)
{
a[i] = rand() % 1000;
}
SortMethods sort(100);
sort.mergeSort(a);
}
But when I complier the code in VS2015, it will throw an error that no instance of constructor "std::thread" matches the argument list.
can you help me find the problem with my code?
=============================================
because of your guys help, i find out that because i overload the mergeSort methods. I rename the method to mergeSort_multi_thread
thread left(&SortMethods::mergeSort_multi_thread, a, low, mid);
I got the error failed to specialize function template 'unknown-type std::invoke(_Callable &&, _Types &&...)
1> g:\dataalog\datastructuresandalgo\basic_data_structures\sorting.h(240): note: see reference to function template instantiation 'std::thread::thread<void(__thiscall SortMethods::* )(int [],int,int),int&[],int&,int&,void>(_Fn &&,int &[],int &,int &)' being compiled
1> with
1> [
1> _Fn=void (__thiscall SortMethods::* )(int [],int,int)
1> ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
[enter image description here][1]
Thx