线程分离pthread_detach()中的return()和pthread_exit()

 

 1 #include <stdio.h>
 2 #include <pthread.h>
 3 
 4 pthread_t tid[2];
 5 void *func_2(void *arg);
 6 void *func_1(void *arg)
 7 {       //为函数func_2创建线程
 8         printf("Starting func_1\n");
 9         pthread_create(&tid[1], NULL, func_2, NULL);
10         //tid[0]将自己挂起,等待线程tid[1]的结束
11         pthread_join(tid[1], NULL);
12 }
13 
14 void *func_2(void *arg)
15 {
16         printf("Starting func_2\n");
17         sleep(1);
18         pthread_exit(NULL);
19 }
20 
21 int main()
22 {       //为函数func_1创建线程并将其分离
23         pthread_create(&tid[0], NULL, func_1, NULL);
24 //      pthread_detach(tid[0], NULL);   //分离
25         pthread_detach(tid[0]); //分离
26         //主线程不退出,但这两个现成的资源都可以释放掉
27 //      return 0;
28         pthread_exit(NULL);
29 }

在main()函数中,如果采用return 0,则没有任何输出就退出了;而如果采用的是pthread_exit(NULL),则打印出消息

 

转载于:https://www.cnblogs.com/fallenmoon/p/6769295.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值