我正在研究如何在pthread_create((中传递多个参数。初始化为
pthread_t th[1];
pthread_create(&th[i], NULL, &producer, shmData);
我的生产者方法是这样的。
void producer(struct ShmData *shmData, sem_t *sem);
当调用pthread_creat
时,我基本上需要同时传递shmData
和sem
。我如何完成这项任务?
您需要为使用struct
struct thread_arg {
struct ShmData *shmData;
sem_t *sem;
};
void producer(struct ShmData *shmData, sem_t *sem);
static void *producer_thread(void *p)
{
struct thread_arg *arg = p;
producer(p->shmData, p->sem);
free(p);
return NULL;
}
void fx()
{
pthread_t th[1];
struct thread_arg *arg = malloc(sizeof(*arg));
arg->shmData = shmData; // from a variable something
arg->sem = sem; // from a variable or something
pthread_create(&th[i], NULL, producer_thread, arg);
}