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

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

实例代码:

#include "apue.h"
#include <pthread.h>  pthread_t ntid;//线程ID  void printids(const char *s)
{  pid_t pid;  pthread_t tid;  pid = getpid();  tid = pthread_self();  printf("%s pid %u tid %u (0x%x)\n",s,(unsigned int)pid,  (unsigned int)tid,(unsigned int)tid);
}  void *thrfun(void *arg){  //sleep(1);//使得主线程先退出  printids("new thread");  return ((void *)0);
}  int main(){  int err;  err = pthread_create(&ntid,NULL,thrfun,NULL);  if(err != 0)  err_quit("can't create thread: %s\n",strerror(err));  printids("main thread");  sleep(1);//等待新线程先结束  exit(0);
}

运行结果:

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

实例代码:

#include "apue.h"
#include <pthread.h>  pthread_t ntid;//线程ID  void printids(const char *s)
{  pid_t pid;  pthread_t tid;  pid = getpid();  tid = pthread_self();  printf("%s pid %u tid %u (0x%x)\n",s,(unsigned int)pid,  (unsigned int)tid,(unsigned int)tid);
}  void *thrfun(void *arg){  sleep(1);//使得主线程先退出  printids("new thread");  return ((void *)0);
}  int main(){  int err;  err = pthread_create(&ntid,NULL,thrfun,NULL);  if(err != 0)  err_quit("can't create thread: %s\n",strerror(err));  printids("main thread");  //sleep(1);  exit(0);//注意是进程(不是线程)退出
}

运行结果:

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

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

#include "apue.h"
#include <pthread.h>  pthread_t ntid;//线程ID  void printids(const char *s)
{  pid_t pid;  pthread_t tid;  pid = getpid();  tid = pthread_self();  printf("%s pid %u tid %u (0x%x)\n",s,(unsigned int)pid,  (unsigned int)tid,(unsigned int)tid);
}  void *thrfun(void *arg){  sleep(1);//使得主线程先退出  printids("new thread");  return ((void *)0);
}  int main(){  int err;  err = pthread_create(&ntid,NULL,thrfun,NULL);  if(err != 0)  err_quit("can't create thread: %s\n",strerror(err));  printids("main thread");  //sleep(1);  pthread_exit(NULL);  exit(0);
}

运行结果:

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将会继续执行而不会退出。
实例代码:

#include "apue.h"
#include<pthread.h>  pthread_t ntid;//线程ID  void printids(const char *s)
{  pid_t pid;  pthread_t tid;  pid = getpid();  tid = pthread_self();  printf("%s pid %u tid %u (0x%x)\n",s,(unsigned int)pid,  (unsigned int)tid,(unsigned int)tid);
}  void *thrfun2(void *arg){  sleep(1);//使得创建它的主线程先退出  printids("new thread of the new thread");  return ((void *)0);
}  void *thrfun(void *arg){  sleep(1);//使得主线程先退出  printids("new thread");  int err;  err = pthread_create(&ntid,NULL,thrfun2,NULL);  if(err != 0)  err_quit("can'tcreate thread: %s\n",strerror(err));  return ((void *)0);
}  int main(){  int err;  err = pthread_create(&ntid,NULL,thrfun,NULL);  if(err != 0)  err_quit("can'tcreate thread: %s\n",strerror(err));  printids("main thread");  //sleep(1);  pthread_exit(NULL);  printf("main thread has exited!\n");  exit(0);
}

运行结果:

linux_主线程子线程退出关系相关推荐

  1. Android 主线程子线程执行关系

    Android 切换线程的方式[我常用的]     1. Handler.post[切换至主线程]     2. new Thread().start()[切换至子线程] 3. 线程池 ThreadP ...

  2. python threading-单线程 多线程 主线程 子线程 setDeamon join

    python threading-单线程 多线程 主线程 子线程 setDeamon join 单线程 多线程 主线程和子线程 setDaemon() join() 测试多线程下程序运行的时间 创建多 ...

  3. 【EventBus】事件通信框架 ( 发送事件 | 判断发布线程是否是主线程 | 子线程切换主线程 | 主线程切换子线程 )

    文章目录 前言 一.根据不同的线程模式进行不同的线程切换操作 二.完整代码示例 前言 发布线程发布事件之后 , 消息中心需要转发这些事件 , 并执行相应的订阅方法 ; 在转发的过程中 , 需要针对订阅 ...

  4. QT 主线程子线程互相传值

    本文实现了主线程给子线程传值.子线程给主线程传值.主线程子线程的互相传值.线程源文件mythread.h..cpp:主线程文件mainwindow.h..cpp;下面程序先从主线程调用子线程,子线程给 ...

  5. 多线程——主线程和子线程退出关系

    分清两个概念 主线程和当前进程的关系:当前进程由主线程和若干个子线程组成. 若当前进程退出后,子线程也会跟着一起退出:若只是主线程退出,仍要分两种情况分析:实际上主线程退出后子线程的状态依赖于它所在的 ...

  6. Golang主线程让子线程退出的三种方式

    在golang中,主go程告知子go程退出有三种方式,建议用后面两种 方式一:全局变量方式 package mainimport ("fmt""sync"&qu ...

  7. 【Android 异步操作】Android 线程切换 ( 判定当前线程是否是主线程 | 子线程中执行主线程方法 | 主线程中执行子线程方法 )

    文章目录 一.判定当前线程是否是主线程 二.子线程中执行主线程方法 三.主线程中执行子线程方法 一.判定当前线程是否是主线程 在 Android 中 , 如果要判定当前线程是否是主线程 , 可以使用如 ...

  8. Android ThreadUtil 线程公共类,判断是否在主线程/ 子线程执行 相关操作

    前言:通常,我们写的公共的模块给别人用,但是这个模块又必须在特定的线程中执行. 比如,一个加载网络图片的的方法,需要在子线程中执行. /** * 加载网络图片 */ private void load ...

  9. 多线程经典问题1——主线程子线程交替问题

    提出问题: * 子线程循环 10 次,接着主线程循环 100 次,接着又回到子线程循环 10 次,接着再回到主线程又循环 100 次.如此循环50次. 分析:这个实际上是非常easy的问题.即子线程r ...

最新文章

  1. 读阿里机器学习平台的一些总结
  2. 旷视冲刺科创板IPO获通过!距离上市仅一步之遥,拟募资60.18亿
  3. centos7怎么安装中文环境支持包
  4. C中的危险函数(缓冲区溢出)
  5. stevedore——启用方式
  6. Vue2.0三——Vue-router
  7. 【图像增强】基于matlab模糊集图像增强【含Matlab源码 394期】
  8. 网络上的计算机无权限访问权限,权限,教您怎么解决无internet访问权限
  9. 他们说我根本不了解企业运作
  10. java开源图像处理ku_我是这么设计高性能海量数(ku)据(zi)查询系统的(一)
  11. 世界陶瓷卫浴100强榜单发布!
  12. 华硕 X542UQ REV:2.1
  13. InstallShield:an error occurred streaming issetup.dll...
  14. c语言开发无人机自动驾驶仪,无人机自动驾驶仪.pdf
  15. 校园卡查询系统C语言,校园卡帐号的查询方法
  16. 分布式事务之——基于消息中间件实现
  17. 如何在指定网站搜索内容
  18. 快速排序详解(图解实例)
  19. Working Practice-召集相关人员面对面的沟通是处理问题最快的方式之一
  20. 固高gts400,vs2017,win10 -64配置记录

热门文章

  1. Unity实现类似于LookAt 的功能
  2. 5分钟搞定各类USB转serial串口驱动,最简单的方法
  3. 今天聊:程序媛是否需要职业规划?
  4. 微信授权登录mock(在没有真实微信账号的情况下测试大量微信账户授权登录的情况)...
  5. ipad和Win10电脑传输文件
  6. 计算机的网络凭据,win10凭据密码是什么,网络凭据账户和密码是多少
  7. 求 Fibonacci 数列的前 20 项
  8. 长沙小吃比较好吃and著名的地方
  9. 我心中的计算机作文500,我心中的太阳作文500字(通用10篇)
  10. C++中指针前还加是什么意思