【Linux学习】pthread_create主线程与创建的新线程之间退出关系

13 篇文章 0 订阅

我们在一个线程中经常会创建另外的新线程,如果主线程退出,会不会影响它所创建的新线程呢?下面就来讨论一下。

 

1、  主线程等待新线程先结束退出,主线程后退出。正常执行。

实例代码:

[cpp]  view plain  copy
 print ?
  1. #include "apue.h"  
  2. #include <pthread.h>  
  3.   
  4. pthread_t ntid;//线程ID  
  5.   
  6. void printids(const char *s)  
  7. {  
  8.         pid_t pid;  
  9.         pthread_t tid;  
  10.         pid = getpid();  
  11.         tid = pthread_self();  
  12.         printf("%s pid %u tid %u (0x%x)\n",s,(unsigned int)pid,  
  13.                         (unsigned int)tid,(unsigned int)tid);  
  14. }  
  15.   
  16. void *thrfun(void *arg){  
  17.         //sleep(1);//使得主线程先退出  
  18.         printids("new thread");  
  19.   
  20.         return ((void *)0);  
  21. }  
  22.   
  23. int main(){  
  24.         int err;  
  25.         err = pthread_create(&ntid,NULL,thrfun,NULL);  
  26.   
  27.         if(err != 0)  
  28.                 err_quit("can't create thread: %s\n",strerror(err));  
  29.         printids("main thread");  
  30.   
  31.         sleep(1);//等待新线程先结束  
  32.   
  33.         exit(0);  
  34. }  
运行结果:


2、  进程先退出,新线程也会立即退出,系统清除所有资源。

实例代码:

[cpp]  view plain  copy
 print ?
  1. #include "apue.h"  
  2. #include <pthread.h>  
  3.   
  4. pthread_t ntid;//线程ID  
  5.   
  6. void printids(const char *s)  
  7. {  
  8.         pid_t pid;  
  9.         pthread_t tid;  
  10.         pid = getpid();  
  11.         tid = pthread_self();  
  12.         printf("%s pid %u tid %u (0x%x)\n",s,(unsigned int)pid,  
  13.                         (unsigned int)tid,(unsigned int)tid);  
  14. }  
  15.   
  16. void *thrfun(void *arg){  
  17.         sleep(1);//使得主线程先退出  
  18.         printids("new thread");  
  19.   
  20.         return ((void *)0);  
  21. }  
  22.   
  23. int main(){  
  24.         int err;  
  25.         err = pthread_create(&ntid,NULL,thrfun,NULL);  
  26.   
  27.         if(err != 0)  
  28.                 err_quit("can't create thread: %s\n",strerror(err));  
  29.         printids("main thread");  
  30.   
  31.         //sleep(1);  
  32.   
  33.         exit(0);//注意是进程(不是线程)退出  
  34. }  

运行结果:


可以发现主线程退出后所创建的新线程也停止运行了。


3、如果主线程调用了pthread_exit,那么它退出了,子线程也不会退出。

实例代码:

[cpp]  view plain  copy
 print ?
  1. #include "apue.h"  
  2. #include <pthread.h>  
  3.   
  4. pthread_t ntid;//线程ID  
  5.   
  6. void printids(const char *s)  
  7. {  
  8.         pid_t pid;  
  9.         pthread_t tid;  
  10.         pid = getpid();  
  11.         tid = pthread_self();  
  12.         printf("%s pid %u tid %u (0x%x)\n",s,(unsigned int)pid,  
  13.                         (unsigned int)tid,(unsigned int)tid);  
  14. }  
  15.   
  16. void *thrfun(void *arg){  
  17.         sleep(1);//使得主线程先退出  
  18.         printids("new thread");  
  19.   
  20.         return ((void *)0);  
  21. }  
  22.   
  23. int main(){  
  24.         int err;  
  25.         err = pthread_create(&ntid,NULL,thrfun,NULL);  
  26.   
  27.         if(err != 0)  
  28.                 err_quit("can't create thread: %s\n",strerror(err));  
  29.         printids("main thread");  
  30.   
  31.         //sleep(1);  
  32.   
  33.             pthread_exit(NULL);  
  34.   
  35.         exit(0);  
  36. }  
运行结果:



POSIX标准定义:

When you program with POSIX Threads API,there is one thing about pthread_exit() that you may ignore for mistake. Insubroutines that complete normally, there is nothing special you have to dounless you want to pass a return code back using pthread_exit(). The completionwon't affect the other threads which were created by the main thread of thissubroutine. 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 thatmain() created before, you can dispense with calling any functions. But if you want to keep the process and all the other threadsexcept 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 openafter its termination.

 

按照POSIX标准定义,当主线程在子线程终止之前调用pthread_exit()时,子线程是不会退出的。

 

注意:这里在main函数中调用pthread_exit()只会是主线程退出,而进程并未退出。因此新线程继续执行而没有退出。

我们可以在return 0;这条语句前面添加一条输出语句printf(“Mainthread has exited!\n”);来进行测试,输出结果不发生任何变化,说明这条语句没有被执行到。也就说明进程并未退出。

 

因此:

一个线程的退出不会影响另外一个线程。但是进程结束,所有线程也就结束了,所有资源会被回收。

 

我们可以再写一个程序来进行验证:

4、在创建的新线程B中再次创建新线程C,那么如果B先退出,那么C将会继续执行而不会退出。

实例代码:

[cpp]  view plain  copy
 print ?
  1. #include "apue.h"  
  2. #include<pthread.h>  
  3.   
  4. pthread_t ntid;//线程ID  
  5.   
  6. void printids(const char *s)  
  7. {  
  8.         pid_t pid;  
  9.         pthread_t tid;  
  10.         pid = getpid();  
  11.         tid = pthread_self();  
  12.         printf("%s pid %u tid %u (0x%x)\n",s,(unsigned int)pid,  
  13.                        (unsigned int)tid,(unsigned int)tid);  
  14. }  
  15.   
  16.   
  17. void *thrfun2(void *arg){  
  18.         sleep(1);//使得创建它的主线程先退出  
  19.         printids("new thread of the new thread");  
  20.   
  21.         return ((void *)0);  
  22. }  
  23.   
  24. void *thrfun(void *arg){  
  25.         sleep(1);//使得主线程先退出  
  26.         printids("new thread");  
  27.         int err;  
  28.         err = pthread_create(&ntid,NULL,thrfun2,NULL);  
  29.   
  30.         if(err != 0)  
  31.                 err_quit("can'tcreate thread: %s\n",strerror(err));  
  32.   
  33.         return ((void *)0);  
  34. }  
  35.   
  36. int main(){  
  37.         int err;  
  38.         err = pthread_create(&ntid,NULL,thrfun,NULL);  
  39.   
  40.         if(err != 0)  
  41.                 err_quit("can'tcreate thread: %s\n",strerror(err));  
  42.         printids("main thread");  
  43.   
  44.         //sleep(1);  
  45.   
  46.         pthread_exit(NULL);  
  47.   
  48.         printf("main thread has exited!\n");  
  49.   
  50.         exit(0);  
  51. }  

运行结果:



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值