[英]How do i pass a struct as an argument in pthread in c
How do i pass a struct as an argument in pthread in c如何在 c 的 pthread 中将结构作为参数传递
void * aFunction (aStruct1 *theStruct1, aStruct2 *theStruct2, aStruct3 *theStruct3){
printf("%s", theStruct1->text);
}
pthread_t thread[2];
pthread_create(&thread[0], NULL, aFunction, "how do i pass all the struct argument here (theStruct1, theStruct2 , theStruct3)");
pthread_create(&thread[1], NULL, aFunction, "how do i pass all the struct argument here (theStruct1, theStruct2 , theStruct3)");
pthread_join(thread[1],NULL);
pthread_join(thread[2],NULL);
i have tried calling it as so with no result我试过这样称呼它,但没有结果
void * aFunction (aStruct1 *theStruct1, aStruct2 *theStruct2, aStruct3 *theStruct3){
printf("%s", theStruct1->text);
}
pthread_t thread[2];
pthread_create(&thread[0], NULL, aFunction(theStruct1, theStruct2 , theStruct3), NULL);
pthread_create(&thread[1], NULL, aFunction(theStruct1, theStruct2 , theStruct3), NULL);
pthread_join(thread[1],NULL);
pthread_join(thread[2],NULL);
A thread start function passed to pthread_create
must take a single void *
argument.传递给pthread_create
的线程启动 function必须采用单个void *
参数。
Since you're passing in multiple structs, you'll need to define an additional struct which contains all data expected by the thread function and pass a pointer to that.由于您要传入多个结构,因此您需要定义一个附加结构,其中包含线程 function 预期的所有数据并传递一个指向该结构的指针。
struct thread_args {
aStruct1 *s1;
aStruct2 *s2;
aStruct3 *s3;
};
void *aFunction (void *p){
struct thread_args *args = p;
printf("%s", args->s1->text);
return NULL;
}
struct thread_args args[] = {
{ theStruct1, theStruct2, theStruct3 },
{ theStruct1, theStruct2, theStruct3 }
};
pthread_create(&thread[0], NULL, aFunction, &args[0]);
pthread_create(&thread[0], NULL, aFunction, &args[1]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.