0

所以我有一个结构如下:

struct threadData{
    string filename
    int one;
    int two;
};

我创建了一个这样的结构数组:

pthread_t threadID[5];
struct threadData *threadP;
threadP = new struct threadData[5];

然后我将这个结构数组传递给一个线程函数,如下所示:

for(int i = 0; i < 5; i++){
    pthread_create(&threadID[i], NULL, threadFunction, (void * ) threadP[i]);
}

这就是我的 threadFunction 的编写方式:

void *threadFunction(void * threadP[]){

}

我尝试了各种方法,但总是收到我传入的内容不正确的错误,我该如何正确执行此操作,以便我可以访问和处理我传入的每个结构对象中的变量?我有一种感觉,由于我使用了结构数组,我的语法在某处是错误的……我只是不知道哪里或哪里出了问题。任何帮助,将不胜感激!

4

1 回答 1

1
void *threadFunction(void * threadP[]){

在 C++ 中,函数不能有数组类型的参数,而参数必须是指针,用作函数参数的数组衰减为指向第一个元素的指针,因此声明等价于:

void *threadFunction(void ** threadP){

这显然不是传递给的正确类型pthread_create

您需要使用此签名传递一个函数:

void *threadFunction(void * threadP)
于 2012-10-20T02:31:24.203 回答