-2

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);
  1. 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);
  1. 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:

Dmitry Kuzminov
  • 6,180
  • 6
  • 18
  • 40
Red Coder
  • 107
  • 1
  • 8
  • 1
    Are you forced to use `pthread`s? [`std::thread`](https://en.cppreference.com/w/cpp/thread/thread) in C++11 and beyond make this pretty much a non-problem. – user4581301 Jun 01 '20 at 20:28
  • If `std::thread` is not an option, consider this alternative: [pthread function from a class](https://stackoverflow.com/questions/1151582/pthread-function-from-a-class) – user4581301 Jun 01 '20 at 20:31
  • The error message explicitly points to the problem: the signature of your `merge_sort` function doesn't match the expected by `pthread_create`. – Dmitry Kuzminov Jun 01 '20 at 20:33

1 Answers1

0

You can't use a non-static class method for a pthread callback. A non-static method has a hidden this parameter that pthread won't be able to account for when calling the method.

You need to use a static method instead (or a non-member function), eg:

class MergeSort {
   private:
   ...
   static void* static_merge_sort(void *arg);
   void* merge_sort();
};
void* MergeSort::static_merge_sort(void *arg)
{
    return ((MergeSort*)arg)->merge_sort();
}

void* MergeSort::merge_sort()
{
    ...
    return ...;
}
pthread_create(&thread, NULL, &MergeSort::static_merge_sort, this);
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • thank you I did a little different on the base of your code: (CLion ide proposal me some edits to make it compile) ` void *MergeSort::static_merge_sort(void *arg) { return static_cast(((MergeSort *) arg)->merge_sortCallback());` } – Red Coder Jun 01 '20 at 20:48