我不知道如何正确地将我的队列指针传递给 pthread_create 函数。
这是我的代码片段:
queue_t *queue_arr[number_of_categories];
int i = 0;
for(i = 0; i < number_of_categories; i++){
queue_arr[i] = queue_create();
}
尝试在循环中创建带有指向队列指针的线程: //这里循环和一些代码
if (pthread_create(&tid[i+1], NULL, consumer_thread, (void *)queue_arr[i]) != 0 ){
perror("pthread_create for consumer failed");
exit(-1);
};
它给了我一个警告:
Incompatible pointer types passing 'void *(queue_t)' to parameter of type 'void *(*)(void *)'
不确定如何以正确的方式为 pthread_create 更改 queue_arr[i]。
谢谢大家的帮助
最佳答案
consumer_thread
的函数原型(prototype)/定义应该是这样的
void* consumer_thread(void* arg)
和 pthread_create 声明为 pthread_create(&tid[i+1], NULL, consumer_thread, (void *)queue_arr[i])
。最后,在您的 consumer_thread
函数中,您可以将 arg
类型转换回 queue_t*
数据类型。
关于c - 我如何将指向队列的指针作为 pthread_create 中的参数传递(收到不兼容的转换警告)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27056719/