【发布时间】:2020-02-16 13:13:52
【问题描述】:
我正在尝试访问传递给由 pthread 进程调用的函数的结构的成员。这很简单,但由于某种原因我无法弄清楚。
我尝试过使用 data.count 或 data->count,但我想我需要将我的数据指针转换为传递给它的结构?我很困惑,但我相信解决方案很简单。请帮忙。 这是结构,后面是 pthread 调用和正在使用的函数。谢谢。 我可以补充一点,BUFFER_SIZE 是全局定义的,所以从我的理解来看这不是问题。我没有在代码中分享。
typedef struct {
int buffer[BUFFER_SIZE];
int count;
int top;
int next;
pthread_mutex_t count_lock;
} prodcons;
//this is in main.c
prodcons pc_nums;
//create producer thread
pthread_create(&tid, &attr, *producer, &pc_nums);
//This is the runner function, pthread call this
void *producer(void *data)
{
//set up data structure to be shared between producer and consumer
int number;
prodcons primeNums;
pc_init(&primeNums);
//call consumer thread
pthread_t tid;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_create(&tid, &attr, *consumer, &primeNums);
while (data->count < BUFFER_SIZE)
{
number = pc_pop(data);
factor2pc(&primeNums, number);
}
}
我只是希望访问我的 pthread_create 传递的结构中的变量,但我得到了 错误:“计数”不是某些结构或联合的一部分。 我真的需要弄清楚如何让数据指向我的结构,以便我可以访问它的成员
【问题讨论】: