Chinaunix

标题: 主线程先退出,子线程会被强制退出吗 [打印本页]

作者: zhongyj    时间: 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(编译生成的程序)也看不到了

是主线程退出时,子线程会强制退出吗?
作者: alexhappy    时间: 2008-10-13 14:12
yes
作者: chenzhanyiczy    时间: 2008-10-13 14:15
当然也有方法实现主线程先退出,子线程继续
作者: cugb_cat    时间: 2008-10-13 14:23
原帖由 chenzhanyiczy 于 2008-10-13 14:15 发表
当然也有方法实现主线程先退出,子线程继续

如何做呢?
作者: 雨过白鹭洲    时间: 2008-10-13 14:33
主线程退出,进程也就结束了,系统回收所有资源

子线程还能继续运行?
作者: zhongyj    时间: 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
作者: 雨过白鹭洲    时间: 2008-10-13 14:41
TO: zhongyj

我在想,你测试的结果之所以是这样,会不会是因为线程sleep阻塞住了。这样线程无法退出,只能等sleep时间到了之后再退出?
作者: alexhappy    时间: 2008-10-13 14:43
同楼上一样想法。。。。
作者: zhongyj    时间: 2008-10-13 14:46
标题: 回复 #7 雨过白鹭洲 的帖子
你是说的上面的
2:如果主线程调用了pthread_exit,那么它退出了,子线程也不会推出
这种吧

这个代码和上面主线程先退出,子线程也退出的代码只是多了句pthread_exit(NULL);
而且sleep的时间差了5秒,不会阻塞这么长时间吧
作者: 雨过白鹭洲    时间: 2008-10-13 14:53
标题: 回复 #9 zhongyj 的帖子
子线程退出,孙线程不会退出这个很好理解

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

我查一下pthread_exit先。。
作者: alexhappy    时间: 2008-10-13 15:01
理论上说,pthread_exit()和线程宿体函数退出的功能是相同的,函数结束时会在内部自动调用pthread_exit()来清理线程相关的资源。但实际上二者由于编译器的处理有很大的不同。  
在进程主函数(main())中调用pthread_exit(),只会使主函数所在的线程(可以说是进程的主线程)退出;而如果是return,编译器将使其调用进程退出的代码(如_exit()),从而导致进程及其所有线程结束运行。

[ 本帖最后由 alexhappy 于 2008-10-13 15:02 编辑 ]
作者: 雨过白鹭洲    时间: 2008-10-13 15:07
When you program with POSIX Threads API, there is one thing about pthread_exit() that you may ignore for mistake. In subroutines that complete normally, there is nothing special you have to do unless you want to pass a return code back using pthread_exit(). The completion won't affect the other threads which were created by the main thread of this subroutine. However, in main(), when the code has been executed to the end, there could leave a choice for you. If you want to kill all the threads that main() created before, you can dispense with calling any functions. But if you want to keep the process and all the other threads except for the main thread alive after the exit of main(), then you can call pthread_exit() to realize it. And any files opened inside the main thread will remain open after its termination.


确实如zhongyj兄所说

按照POSIX标准定义,当主线程在子线程终止之前调用pthread_exit()时,子线程是不会退出的
作者: Cyberman.Wu    时间: 2008-10-14 13:03
这个其实很简单,因为调用exit会使整个进程终止,而在main中return实际上就会调用到exit而导致所有线程都终止。入口代码是类似这样的方式调用main的:
exit(main(...));
作者: hongjunbj    时间: 2008-10-14 23:24
标题: 回复 #12 雨过白鹭洲 的帖子
学习了,这位兄弟很会查资料哈
作者: yylogo    时间: 2010-01-02 20:24
额,, 什么主线程, 子线程..
线程都是一样的, 退出了一个不会影响另外一个..
但是你们口中的那个"主线程"吧,, 是main,, 在main执行完之后, 会调用exit()..
exit() 会让进程Over.. 那所有线程不就都GoodbYE了.`?
  这也符合那个子线程退出, 孙线程不退出的说法..

也就是说,, 你遇到的问题是进程和线程之间的关系.. 不是线程和线程..

不知道我说清楚没有`
作者: tianxiaogang12    时间: 2010-01-03 08:57
原帖由 雨过白鹭洲 于 2008-10-13 15:07 发表


雨过白鹭洲兄  资料在那找到的,是posix标准吗? 我怎么在posix标准里没有找到,麻烦给个链接,或者具体说说怎么找到的
作者: cjaizss    时间: 2010-01-03 19:56
原帖由 cugb_cat 于 2008-10-13 14:23 发表

如何做呢?

pthread_exit
不过我一般不习惯把主线程退出去,我还是喜欢拿它来做监听之类的事。

[ 本帖最后由 cjaizss 于 2010-1-3 19:58 编辑 ]
作者: JohnBull    时间: 2010-01-04 01:40
原帖由 yylogo 于 2010-1-2 20:24 发表
额,, 什么主线程, 子线程..
线程都是一样的, 退出了一个不会影响另外一个..
但是你们口中的那个"主线程"吧,, 是main,, 在main执行完之后, 会调用exit()..
exit() 会让进程Over.. 那所有线程不就都GoodbYE了 ...


线程不像进程,一个进程中的线程之间是没有父子之分的,都是平级关系。

你基本概念很清楚!他们说的有问题!
作者: c/unix    时间: 2010-01-04 10:29
提示: 作者被禁止或删除 内容自动屏蔽
作者: 紫色的撒加    时间: 2010-01-04 16:55
原帖由 c/unix 于 2010-1-4 10:29 发表



我测试了一下,main线程调用pthread_exit之后,return 0根本没执行,所有进程就没有退出。

#include
#include

void* thrd_start_routine(void* v)
{
    int i;
   
    for (i = 0; i < 1 ...



也就是说进程是在所有线程退出以后退出,或者某个线程调用exit。因为main中调用了pthread_exit导致调用exit的线程提前退出,所以要到其他线程全部执行完进程才会退出。不知这么理解对不对
作者: 紫色的撒加    时间: 2010-01-04 16:55
原帖由 JohnBull 于 2010-1-4 01:40 发表


线程不像进程,一个进程中的线程之间是没有父子之分的,都是平级关系。

你基本概念很清楚!他们说的有问题!


我觉得这么较真儿没有意义
作者: lzyshijune2010    时间: 2015-09-25 16:54
回复 21# 紫色的撒加


    有意义啊




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2