本篇文章个人在青岛吃饭的时候突然想到的...最近就有想写几篇关于线程退出的文章,所以回家到之后就奋笔疾书的写出来发布了

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

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

实例代码:

#include "apue.h"
#include <pthread.h>pthread_t ntid;//线程IDvoid 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;//线程IDvoid 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;//线程IDvoid 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;//线程IDvoid 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);
}

运行结果:

文章结束给大家分享下程序员的一些笑话语录: 一位程序员去海边游泳,由于水性不佳,游不回岸了,于是他挥着手臂,大声求.救:“F1,F1!”

--------------------------------- 原创文章 By
退出和学习
---------------------------------

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

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

    我们在一个线程中经常会创建另外的新线程,如果主线程退出,会不会影响它所创建的新线程呢?下面就来讨论一下. 1.  主线程等待新线程先结束退出,主线程后退出.正常执行. 实例代码: #include & ...

  2. 每个java程序都至少有一个线程给主线程,java程序在主线程中判断各个子线程状态的操作,该如何解决...

    java程序在主线程中判断各个子线程状态的操作 每个子线程在队列为空时会wait等待其他线程添加新url到队列,到最后所有子线程都取不到url时也会都wait住,要在主线程中判断如果所有的子线程都是w ...

  3. 主线程是如何向子线程传递数据的?_c++ 利用thread创建线程

    用进行多线程开发 小时候,老师总是教育我们上课要专心,"一心不可二用".可是CPU这个不听话的"熊孩子"偏偏却在一个芯片中加入了两个甚至多个运算核心,想要一&q ...

  4. 模态对话框阻塞主线程的话不影响其他线程操作主线程控件(不阻塞)

    Task.Factory.StartNew(() => {Thread.Sleep(5000);this.Invoke(new Action(() => {this.button7.Tex ...

  5. 面试官:如何让主线程等待所有的子线程执行结束之后再执行

    java 主线程等待所有子线程执行完毕在执行,在工作总往往会遇到异步去执行某段逻辑, 然后先处理其他事情, 处理完后再把那段逻辑的处理结果进行汇总(比如用户下单一个产品,后台会做一系列的处理,为了提高 ...

  6. 在非主线程中创建窗口

    很多朋友都会有过这样的经历,为什么在主线程中创建窗口且窗口工作很正常,但一移到非主线程(有的朋友喜欢叫它为工作线程),却无法正常工作.本文就这个问题和各位探讨,可能无法做到尽善尽美,但能抛砖引玉也算是 ...

  7. 一个Servlet同一时刻只有一个实例。 当多个请求发送到同一个Servlet,服务器会为每个请求创建一个新线程来处理。

    Servlet是一种独立于操作系统平台和网络传输协议的服务器端的Java应用程序.  相同点:  1. 不是独立的应用程序,没有main()方法.  2. 不是由用户调用,由另一个应用程序(容器)调用 ...

  8. qt调用linux系统的线程函数吗,Qt之主线程与子线程通讯(linux下)

    Qt之主线程与子线程通信(linux下) 转载请注明出处:http://blog.csdn.net/feng1790291543 主线程与子线程通信主要是通过Qt上的Gui按钮,触发使得主线程上的信息 ...

  9. pthread线程传递数据回主线程_操作系统4:线程(1)

    接下来讨论下线程.进程和线程是一个很有趣的话题,进程和线程的区别到底是什么?一些书上讲线程是"轻量级进程",从而可以节省切换开销.但是线程到底是怎么样成为轻量级进程的呢? 可以设想 ...

最新文章

  1. 关于ListView中adapter调用notifyDataSetChanged无效的原因
  2. 大佬带你深入浅出Lua虚拟机
  3. jQuery中的文档操作处理(五):append()、prepend()、after()、before()、wrap()、wrapAll()、wrapInner()、clone()等...
  4. 汇编语言随笔(13)- 外中断(可屏蔽中断)、实验15
  5. python结束线程_2018-01-02 如何优雅地终止python线程
  6. 【VMware vSAN 6.6】2.5.硬件部署选项:我们有软硬件项目解决方案
  7. Python学习笔记:一维数据的插值
  8. python按行读取excel文件_python3读取excel文件只提取某些行某些列的值方法
  9. NIOS II 常见问题总结FAQ - xiangyuqxq的专栏 - CSDN博客
  10. ue4 时间轴是什么意思_UE4中Timeline的使用
  11. 使用CSVDE批量创建和修改域用户
  12. 值得终身背诵的道家名言50句,拔高人生境界
  13. 计算机办公模式是什么,华为Mate 10“电脑模式”告诉你什么是真正的“移动办公”...
  14. 【年薪百万之IT界大神成长之路零】年薪百万之IT界大神成长之路
  15. Google Code Jam 2010 Qualification Round 资格赛 Problem A. Snapper Chain 问题A.按扣链条
  16. 【数据挖掘】天池挑战赛 新闻推荐
  17. html div背景图片路径,css如何设置背景图片位置?
  18. 小米 11 ultra旗舰版官方原版ROM系统MIUI13所有固件
  19. Thinkphp入门-创建一个最简单的ThinkPhp项目工程
  20. android sqlite 操作类封装,SQLiteUtils 一个简单的基于 Android 的 Sqlite 数据库的操作封装库 @codeKK Android开源站...

热门文章

  1. 吴恩达:诸位CEO,我有一本「AI转型秘籍」传授给你
  2. 操作系统第一篇【引论】
  3. 单板计算机倍受欢迎 廉价的ARM计算机能干嘛
  4. 自定义Django的admin界面
  5. [改善Java代码]优先使用整型池
  6. postgres使用dblink
  7. Linux 性能监测工具
  8. 数据中心虚拟化的7大考量要素
  9. Django实战(20):分页(Pagination)
  10. 解析Spring IOC原理——工厂模式与反射机制的综合应用