免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
123下一页
最近访问板块 发新帖
查看: 38504 | 回复: 21
打印 上一主题 下一主题

主线程先退出,子线程会被强制退出吗 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-10-13 14:04 |只看该作者 |倒序浏览
子线程先退出的代码
  1. #include <pthread.h>
  2. #include <stdio.h>

  3. void* thrd_start_routine(void* v)
  4. {
  5.     sleep(5);
  6.         printf("created thread\n");
  7. }

  8. int main()
  9. {
  10.         pthread_t thrdid;
  11.                
  12.         pthread_create(&thrdid, NULL, &thrd_start_routine, NULL);
  13.        
  14.         sleep(10);
  15.         printf("main thread\n");
  16.        
  17.         return  0;
  18. }
复制代码

输出是
//过了5秒
created thread
//又过了5秒
main thread

主线程先退出
  1. #include <pthread.h>
  2. #include <stdio.h>

  3. void* thrd_start_routine(void* v)
  4. {
  5.     sleep(10);
  6.         printf("created thread\n");
  7. }

  8. int main()
  9. {
  10.         pthread_t thrdid;
  11.                
  12.         pthread_create(&thrdid, NULL, &thrd_start_routine, NULL);
  13.        
  14.         sleep(5);
  15.         printf("main thread\n");
  16.        
  17.         return  0;
  18. }
复制代码

输出是
//过了5秒
main thread
//退出了,切换到了shell,而且在5秒内用ps -ef | grep thread(编译生成的程序)也看不到了

是主线程退出时,子线程会强制退出吗?

论坛徽章:
0
2 [报告]
发表于 2008-10-13 14:12 |只看该作者
yes

论坛徽章:
1
申猴
日期:2014-02-11 14:50:31
3 [报告]
发表于 2008-10-13 14:15 |只看该作者
当然也有方法实现主线程先退出,子线程继续

论坛徽章:
0
4 [报告]
发表于 2008-10-13 14:23 |只看该作者
原帖由 chenzhanyiczy 于 2008-10-13 14:15 发表
当然也有方法实现主线程先退出,子线程继续

如何做呢?

论坛徽章:
0
5 [报告]
发表于 2008-10-13 14:33 |只看该作者
主线程退出,进程也就结束了,系统回收所有资源

子线程还能继续运行?

论坛徽章:
0
6 [报告]
发表于 2008-10-13 14:35 |只看该作者
又写了两个测试代码
1:主进程退出时,不管子、孙...线程都会退出
     但是不是创建子线程的线程退出,被他创建的子线程都要退出,比如下面的的子线程创建了孙线程,子线程先退出,孙线程没有退出
  1. #include <pthread.h>
  2. #include <stdio.h>

  3. void* fun(void* v)
  4. {
  5.     sleep(10);
  6.     printf("grandson thread\n");
  7. }


  8. void* thrd_start_routine(void* v)
  9. {
  10.     pthread_t thd;
  11.     pthread_create(&thd, NULL, &fun, NULL);
  12.     sleep(5);
  13.         printf("created thread\n");
  14. }

  15. int main()
  16. {
  17.         pthread_t thrdid;
  18.                
  19.         pthread_create(&thrdid, NULL, &thrd_start_routine, NULL);
  20.        
  21.         sleep(15);
  22.         printf("main thread\n");
  23.         return  0;
  24. }
复制代码

输出是
//过了5秒
created thread
//又过了5秒
grandson thread
//又过了5秒
main thread

------------------------------------------------------------------------------------------
2:如果主线程调用了pthread_exit,那么它退出了,子线程也不会推出
  1. #include <pthread.h>
  2. #include <stdio.h>

  3. void* thrd_start_routine(void* v)
  4. {
  5.     sleep(10);
  6.         printf("created thread\n");
  7. }

  8. int main()
  9. {
  10.         pthread_t thrdid;
  11.                
  12.         pthread_create(&thrdid, NULL, &thrd_start_routine, NULL);
  13.        
  14.         sleep(5);
  15.         printf("main thread\n");
  16.         pthread_exit(NULL);
  17.         return  0;
  18. }
复制代码

//过了5秒
main thread
//又过了5秒
created thread

论坛徽章:
0
7 [报告]
发表于 2008-10-13 14:41 |只看该作者
TO: zhongyj

我在想,你测试的结果之所以是这样,会不会是因为线程sleep阻塞住了。这样线程无法退出,只能等sleep时间到了之后再退出?

论坛徽章:
0
8 [报告]
发表于 2008-10-13 14:43 |只看该作者
同楼上一样想法。。。。

论坛徽章:
0
9 [报告]
发表于 2008-10-13 14:46 |只看该作者

回复 #7 雨过白鹭洲 的帖子

你是说的上面的
2:如果主线程调用了pthread_exit,那么它退出了,子线程也不会推出
这种吧

这个代码和上面主线程先退出,子线程也退出的代码只是多了句pthread_exit(NULL);
而且sleep的时间差了5秒,不会阻塞这么长时间吧

论坛徽章:
0
10 [报告]
发表于 2008-10-13 14:53 |只看该作者

回复 #9 zhongyj 的帖子

子线程退出,孙线程不会退出这个很好理解

但主线程退出时调用pthread_exit,然后你仍然return 0,这时候主线程应该还是退出了,按照main的定义,进程就应该结束了

我查一下pthread_exit先。。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP