c语言sscanf函数的用法是什么
229
2022-10-02
Linux pthread_exit()传递线程返回码
(1)通过全局变量进行传递struct food{ int a; int b; int c;};struct food apple;void* task1(void* arg){ apple.a = 27; apple.b = 12; apple.c = 39; pthread_exit((void*)&apple);}int main(int argc, char *argv[]){ pthread_t thrd1, thrd2, thrd3; void* tret; pthread_create(&thrd1, NULL, (void*)task1, NULL); pthread_join(thrd1, (void*)&tret); printf("The food:%d %d %d\n", ((struct food*)(tret))->a, ((struct food*)(tret))->b, ((struct food*)(tret))->c); printf("Main thread exit...\n"); return 0;} 程序输出:[root@robot ~]# gcc thread_exit.c -lpthread -o thread_exit[root@robot ~]# ./thread_exitThe food:27 12 39Main thread exit...[root@robot ~]#(2)通过malloc分配变量进行传递struct food{ int a; int b; int c;};void* task1(void* arg){ struct food *apple = malloc(sizeof(struct food)); apple->a = 23; apple->b = 82; apple->c = 59; pthread_exit((void*)apple);}int main(int argc, char *argv[]){ pthread_t thrd1, thrd2, thrd3; void* tret; pthread_create(&thrd1, NULL, (void*)task1, NULL); pthread_join(thrd1, (void*)&tret); printf("The food:%d %d %d\n", ((struct food*)(tret))->a, ((struct food*)(tret))->b, ((struct food*)(tret))->c); free(((struct food*)tret)); printf("Main thread exit...\n"); return 0;} 程序输出:[root@robot ~]# gcc thread_exit.c -lpthread -o thread_exit[root@robot ~]# ./thread_exitThe food:23 82 59Main thread exit...[root@robot ~]#
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~