#include#includestruct val{ int num1; int num2;};//send a int to argvoid *text(void *arg){ int *p = (int *)arg; printf("arg is %d\n",*p); pthread_exit(NULL);}//send char to argvoid *text2(void *arg){ char *d = (char *)arg; printf("arg is %s\n",arg); pthread_exit(NULL);}//send a struct to argvoid *text3(void *arg){ struct val *v = (struct val *)arg; printf("arg is v.num1:%d,v.num2:%d\n",v->num1,v->num2); pthread_exit(NULL);}int main(){ pthread_t pth; char val[10] = "i am arg."; int arry = 10; pthread_create(&pth, NULL, text,(void *)&arry); pthread_join(pth, NULL); pthread_create(&pth, NULL, text2, (void *)val); pthread_join(pth, NULL); struct val v; v.num1 = 0; v.num2 = 1; pthread_create(&pth, NULL, text3, (void *)&v); //一定要用&v因为结构体是值类型; pthread_join(pth, NULL); return 0;}
线程传参的格式问题
最新推荐文章于 2023-10-30 23:38:54 发布