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

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

示例代码:

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/types.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)
perror("pthread_create");
printids("main thread"); sleep(1);//等待新线程先结束 exit(0);
}

运行结果:

huangcheng@ubuntu:~$ ./a.out
main thread pid 2344 tid 3077813952 (0xb773b6c0)
new thread pid 2344 tid 3077811056 (0xb773ab70)

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

示例代码:

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/types.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)
perror("pthread_create");
printids("main thread"); //sleep(1);//等待新线程先结束 exit(0);
}

运行结果:

huangcheng@ubuntu:~$ ./a.out
main thread pid 2366 tid 3077641920 (0xb77116c0)

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

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

示例代码:

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/types.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)
perror("pthread_create");
printids("main thread"); //sleep(1);//等待新线程先结束
pthread_exit(NULL); // exit(0);
}

运行结果:

huangcheng@ubuntu:~$ ./a.out
main thread pid 2385 tid 3077768896 (0xb77306c0)
new thread pid 2385 tid 3077766000 (0xb772fb70)

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 <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/types.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)
perror("pthread_create"); return ((void *)0);
} int main(){
int err;
err = pthread_create(&ntid,NULL,thrfun,NULL); if(err != 0)
perror("pthread_create");
printids("main thread"); //sleep(1); pthread_exit(NULL);
}

运行结果:

huangcheng@ubuntu:~$ ./a.out
main thread pid 2413 tid 3077912256 (0xb77536c0)
new thread pid 2413 tid 3077909360 (0xb7752b70)
new thread of the new thread pid 2413 tid 3069516656 (0xb6f51b70)

UNIX环境高级编程——主线程与子线程的退出关系的更多相关文章

  1. (转)C#/.NET主线程与子线程之间的关系

    一般 一个应用程序就对应一个进程,一个进程可有一个或多个线程,而一般有一个主线程.       有的博客上说“至少一个主线程”,这一说法持有怀疑         主线程与子线程之间的关系        ...

  2. C#/.NET主线程与子线程之间的关系

    以前一直没有在程序中写过总结,再翻开程序时却不知所云,所以我决定写总结        一般 一个应用程序就对应一个进程,一个进程可有一个或多个线程,而一般有一个主线程.       有的博客上说“至少 ...

  3. Unix 环境高级编程

    UNIX 环境高级编程 本书描述了UNIX系统的程序设计接口--系统调用接口和标准C库提供的很多函数. 与大多数操作系统一样,Unix为程序员运行提供了大量的服务--打开文件,读文件,启动一个新程序, ...

  4. (十三) [终篇] 一起学 Unix 环境高级编程 (APUE) 之 网络 IPC:套接字

    . . . . . 目录 (一) 一起学 Unix 环境高级编程 (APUE) 之 标准IO (二) 一起学 Unix 环境高级编程 (APUE) 之 文件 IO (三) 一起学 Unix 环境高级编 ...

  5. multiple definition of `err_sys' 《UNIX环境高级编程》

    本文地址:http://www.cnblogs.com/yhLinux/p/4079930.html 问题描述: [点击此处直接看解决方案] 在练习<UNIX环境高级编程>APUE程序清单 ...

  6. unix环境高级编程基础知识之第二篇(3)

    看了unix环境高级编程第三章,把代码也都自己敲了一遍,另主要讲解了一些IO函数,read/write/fseek/fcntl:这里主要是c函数,比较容易,看多了就熟悉了.对fcntl函数讲解比较到位 ...

  7. (一) 一起学 Unix 环境高级编程 (APUE) 之 标准IO

    . . . . . 目录 (一) 一起学 Unix 环境高级编程 (APUE) 之 标准IO (二) 一起学 Unix 环境高级编程 (APUE) 之 文件 IO (三) 一起学 Unix 环境高级编 ...

  8. (二) 一起学 Unix 环境高级编程 (APUE) 之 文件 IO

    . . . . . 目录 (一) 一起学 Unix 环境高级编程 (APUE) 之 标准IO (二) 一起学 Unix 环境高级编程 (APUE) 之 文件 IO (三) 一起学 Unix 环境高级编 ...

  9. (三) 一起学 Unix 环境高级编程 (APUE) 之 文件和目录

    . . . . . 目录 (一) 一起学 Unix 环境高级编程 (APUE) 之 标准IO (二) 一起学 Unix 环境高级编程 (APUE) 之 文件 IO (三) 一起学 Unix 环境高级编 ...

随机推荐

  1. TypeScript: Week Reflection

    TypeScript: Week Reflection Introduction Type Script already provide decorators to help developers i ...

  2. python中input()和raw_input()的区别

    两者均是python的内置函数,通过读取控制台的输入与用户实现交互.raw_input:将所有输入作为字符串看待,不管用户输入什么类型的都会转变成字符串.                   raw的 ...

  3. 论文笔记--PCN:Real-Time Rotation-Invariant Face Detection with Progressive Calibration Networks

    关键词:rotation-invariant face detection, rotation-in-plane, coarse-to-fine 核心概括:该篇文章为中科院计算所智能信息处理重点实验室 ...

  4. 无忧代理免费ip爬取(端口js加密)

    起因 为了训练爬虫技能(其实主要还是js技能-),翻了可能有反爬的网站挨个摧残,现在轮到这个网站了:http://www.data5u.com/free/index.shtml 解密过程 打开网站,在 ...

  5. ACM 排列2

    Ray又对数字的列产生了兴趣: 现有四张卡片,用这四张卡片能排列出很多不同的4位数,要求按从小到大的顺序输出这些4位数.  Input每组数据占一行,代表四张卡片上的数字(0<=数字<=9 ...

  6. vue开发中v-for在Eslint的规则检查下出现:Elements in iteration expect to have 'v-bind:key' directives

    在使用VScode编辑器vue开发过程中,v-for在Eslint的规则检查下出现报错:Elements in iteration expect to have 'v-bind:key' direct ...

  7. EntityFramework Core 自动绑定模型映射

    笔者最近在和同事共同开发项目时,需要从他们提供的包含数据库实体类型的类库中读取实体信息绑定到自己的项目中(但是都在同一个解决方案里),所以很直接的一种方式就是把项目中所有的实体都以 public Db ...

  8. Windows下Java如何调用本地获取mac地址

    import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import ...

  9. Redis之(三)管理命令

    4.1键管理 通过学习五种数据类型的操作命令,可以发现,Redis对每种数据的处理之前,都要先指定该数据的key,然后再指定对该数据进行何种操作. Redis中的key有点类似于Java中的变量名,起 ...

  10. Dynamics CRM2016 关于修改部署管理员账号权限引发的问题

    最近在用2016做项目,一个同事的一个操作给我带来了一个头疼的问题,他把部署管理员的CRM账号的管理员权限给移除了,导致整个系统的所有账号进CRM都是下图这样 即使系统中还存在其他的拥有管理员权限的账 ...