简体   繁体   English

将结构数组传递给pthread_create

[英]Passing an array of structs to a pthread_create

So I have a struct as follows: 所以我有一个结构如下:

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

and I created an array of these structs like this: 我创建了这些结构的数组,如下所示:

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

then I'm passing this array of structs to a thread function as follows: 然后将这个结构数组传递给线程函数,如下所示:

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

and this is how my threadFunction is written: 这是我的threadFunction的编写方式:

void *threadFunction(void * threadP[]){

}

I've tried various things, but I always get the error that what I pass in is not correct, how do I do this properly so that I can access and process the variables in each struct object I pass in? 我已经尝试了各种方法,但是总是收到以下错误:我传入的内容不正确,我该如何正确执行操作,以便可以访问和处理传入的每个struct对象中的变量? I have a feeling that my syntax somewhere is wrong due to me using an array of structs...I just don't know where or what exactly is wrong. 我感觉到我的语法错误是由于我使用了一系列结构而导致的...我只是不知道哪里或到底是什么错误。 Any help would be appreciated! 任何帮助,将不胜感激!

void *threadFunction(void * threadP[]){

Functions cannot have parameters of array types in C++, instead the parameter has to be a pointer, and an array used as a function argument decays to a pointer to the first element, so that declaration is equivalent to: 函数不能在C ++中具有数组类型的参数,而参数必须是指针,并且用作函数参数的数组会衰减为指向第一个元素的指针,因此声明等效于:

void *threadFunction(void ** threadP){

Which clearly isn't the right type to pass to pthread_create 显然不是传递给pthread_create的正确类型

You need to pass a function with this signature: 您需要传递带有此签名的函数:

void *threadFunction(void * threadP)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM