c++ - 将结构数组传递给 pthread_create

标签 c++ arrays pointers struct pthreads

所以我有一个结构如下:

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[]){

}

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

最佳答案

void *threadFunction(void * threadP[]){

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

void *threadFunction(void ** threadP){

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

您需要传递具有此签名的函数:

void *threadFunction(void * threadP)

关于c++ - 将结构数组传递给 pthread_create,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12984371/

相关文章:

c - 打印指向结构体的指针的二维数组

c++ - 指针变量和引用变量有什么区别?

c++ - 将整数表示为字节

C++ self 执行标准 : size_t

c++ - Visual C++ 中的函数重载

javascript - 在 JavaScript 中对多维数组进行排序时行为不一致

C++ 多维数组在幕后是如何工作的

javascript - 在 Highchart 中解析数据

objective-c - stringWithUTF8String : do and how it works 是什么

c - 在 C 中访问多维指针数组中的特定元素