Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/149.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 从main中的objects成员函数生成线程_C++_Multithreading - Fatal编程技术网

C++ 从main中的objects成员函数生成线程

C++ 从main中的objects成员函数生成线程,c++,multithreading,C++,Multithreading,我一直在尝试使用线程,但有一个问题似乎无法解决。我试图从接受参数的成员函数中生成一个线程,但是我还不能让它不抛出错误 这是我的 ArraySorter s; char *arr1, *arr2, *arr3; arr1 = new char[MAXSIZE]; arr2 = new char[MAXSIZE]; arr3 = new char[MAXSIZE]; srand(time(NULL)); for (int i = 0; i < MAXSIZE; i++) { ar

我一直在尝试使用线程,但有一个问题似乎无法解决。我试图从接受参数的成员函数中生成一个线程,但是我还不能让它不抛出错误

这是我的

ArraySorter s;
char *arr1, *arr2, *arr3;

arr1 = new char[MAXSIZE];
arr2 = new char[MAXSIZE];
arr3 = new char[MAXSIZE];

srand(time(NULL));

for (int i = 0; i < MAXSIZE; i++)
{
    arr1[i] = arr2[i] = arr3[i] = rand() % MAXSIZE;
}

thread t1(&ArraySorter::InsertionSort, &s, arr1, MAXSIZE);
thread t2(&ArraySorter.MergeSort, arr2, MAXSIZE);
thread t3(&s.QuickSort, arr3, MAXSIZE);
  • 创建
    char
    数组并尝试传递
    char*
    ,但函数采用
    int*
    。您需要在某个地方更改类型,或者制作排序函数模板

  • 您无法编译代码,因为
    线程
    构造函数无法推断要使用的函数(例如
    void InsertionSort(int*arr,int n,int startIndex,int gap)
    void InsertionSort(int*arr,int n)
    )。一种纠正方法是重命名函数。另一种方法是像这样指定函数指针

  • 请参阅简化示例的完整代码

    #include <iostream>
    #include <functional>
    #include <thread>
    
    using namespace std;
    
    #define MAXSIZE 128
    
    class ArraySorter {
    public:
        static void InsertionSort(int* arr, int n) {
        }
        static void InsertionSort(int* arr, int n1, int n2) {
        }
    };
    
    int main() {
        int* arr1 = new int[MAXSIZE];
        int* arr2 = new int[MAXSIZE];
        int* arr3 = new int[MAXSIZE];
    
        for (int i = 0; i < MAXSIZE; i++) {
            arr1[i] = arr2[i] = arr3[i] = rand() % MAXSIZE;
        }
    
        void(*insertion_1)(int*, int)      = &ArraySorter::InsertionSort;
        void(*insertion_2)(int*, int, int) = &ArraySorter::InsertionSort;
        thread t1(insertion_1, arr1, MAXSIZE);
        thread t2(insertion_2, arr1, MAXSIZE, 0);
        // Won't compile!
        // thread t3(&ArraySorter::InsertionSort, arr1, MAXSIZE);
    }
    
    #包括
    #包括
    #包括
    使用名称空间std;
    #定义MAXSIZE 128
    类阵列排序器{
    公众:
    静态void InsertionSort(int*arr,int n){
    }
    静态void InsertionSort(int*arr、int n1、int n2){
    }
    };
    int main(){
    int*arr1=新的int[MAXSIZE];
    int*arr2=新的int[MAXSIZE];
    int*arr3=新int[MAXSIZE];
    对于(int i=0;i
    谢谢您的更新。如果删除
    &s
    参数,将编译第一个。这些是静态成员,不需要对象。其余的将遵循相同的形式。您甚至不需要声明
    s
    void(*insertion_1)(int*, int)      = &ArraySorter::InsertionSort;
    void(*insertion_2)(int*, int, int) = &ArraySorter::InsertionSort;
    
    #include <iostream>
    #include <functional>
    #include <thread>
    
    using namespace std;
    
    #define MAXSIZE 128
    
    class ArraySorter {
    public:
        static void InsertionSort(int* arr, int n) {
        }
        static void InsertionSort(int* arr, int n1, int n2) {
        }
    };
    
    int main() {
        int* arr1 = new int[MAXSIZE];
        int* arr2 = new int[MAXSIZE];
        int* arr3 = new int[MAXSIZE];
    
        for (int i = 0; i < MAXSIZE; i++) {
            arr1[i] = arr2[i] = arr3[i] = rand() % MAXSIZE;
        }
    
        void(*insertion_1)(int*, int)      = &ArraySorter::InsertionSort;
        void(*insertion_2)(int*, int, int) = &ArraySorter::InsertionSort;
        thread t1(insertion_1, arr1, MAXSIZE);
        thread t2(insertion_2, arr1, MAXSIZE, 0);
        // Won't compile!
        // thread t3(&ArraySorter::InsertionSort, arr1, MAXSIZE);
    }