I have that code and want to modify it and put it in class for example MergeSort
after I convert it to class I get an error in that line in initMergeSort:
error: no matches converting function ‘merge_sort’ to type ‘void* ()(void)’ if (pthread_create(&thread, NULL, merge_sort, (void *) NULL) != 0) {
Referention to non-static function must be called (possible target for call
{Clion recognize it *merge_sort})
pthread_create(&thread, NULL, merge_sort,(void *) NULL);
- The way I want to make it:
New header
and the new header:
class MergeSort {
private:
//int *notSortedData;
int *sortedData{};
int part = 0;
void merge(int low, int mid, int high);
void merge_sort(int low, int high);
//Problem is here: --v
void *merge_sort(void *arg);
public:
MergeSort(int* notSortedData){
this->sortedData = notSortedData;
}
static const int MAX = 20;
static const int HALF_OF_MAX = MAX / 2;
int initMergeSort(/*int *notSortedData*/);
};
old header:
static const int MAX = 20 ;
static const int HALF_OF_MAX = MAX / 2;
void merge(int low, int mid, int high);
void merge_sort(int low, int high);
void *merge_sort(void *arg);
int initMergeSort(int *notSortedData);
- old implementation:
// array of size MAX
int *a;
int part;
void merge(int low, int mid, int high) {(...)}
// merge sort function
void merge_sort(int low, int high) {
// calculating mid point of array
(...)
// calling first half
merge_sort(low, mid);
// calling second half
merge_sort(mid + 1, high);
// merging the two halves
merge(low, mid, high);
}
}
// thread function for multi-threading Problem is here
----------------------v
void *merge_sort(void *arg) {
// which part out of 4 parts
int thread_part = part++;
// calculating low and high
int low = thread_part * (MAX / 4);
int high = (thread_part + 1) * (MAX / 4) - 1;
// evaluating mid point
int mid = low + (high - low) / 2;
if (low < high) {
merge_sort(low, mid);
merge_sort(mid + 1, high);
merge(low, mid, high);
}
}
// merge function for merging two parts
int initMergeSort(int *notSortedData) {
a = notSortedData;
(...)
pthread_t threads[THREAD_MAX];
// creating 4 threads Here I have to invoke function call merge_sort
for (unsigned long &thread : threads) {
pthread_create(&thread, NULL, merge_sort,
(void *) NULL);
}
// joining all 4 threads
(...)
// merging the final 4 parts
merge(0, (HALF_OF_MAX - 1) / 2, HALF_OF_MAX - 1);
(...)
}
Thank you for in advance
Full old code is here, and Full code after my unsuccesful refactor is here: