class 中的 C++ 回调函数对 non-static 函数的引用
C++ callback functions in class Referention to non-static funct
我有那个代码,想修改它并将其放入 class 例如 MergeSort
在我将其转换为 class 之后,我在 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);
- 我想要的方式:
新建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*/);
};
旧 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);
- 旧实现:
// 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);
}
}
// multi-threading 的线程函数问题出在这里
------------------------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);
(...)
}
提前谢谢你
完整的旧代码是 here, and Full code after my unsuccesful refactor is here:
您不能将非静态 class 方法用于 pthread 回调。非静态方法有一个隐藏的 this
参数,pthread 在调用该方法时将无法考虑该参数。
您需要改用静态方法(或非成员函数),例如:
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);
我有那个代码,想修改它并将其放入 class 例如 MergeSort
在我将其转换为 class 之后,我在 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);
- 我想要的方式:
新建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*/);
};
旧 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);
- 旧实现:
// 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);
}
}
// multi-threading 的线程函数问题出在这里
------------------------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);
(...)
}
提前谢谢你
完整的旧代码是 here, and Full code after my unsuccesful refactor is here:
您不能将非静态 class 方法用于 pthread 回调。非静态方法有一个隐藏的 this
参数,pthread 在调用该方法时将无法考虑该参数。
您需要改用静态方法(或非成员函数),例如:
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);