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

  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. 第五节:Task构造函数之TaskCreationOptions枚举处理父子线程之间的关系。

    一. 整体说明 揭秘: 通过F12查看Task类的源码(详见下面的截图),发现Task类的构造函数有有一个参数为:TaskCreationOptions类型,本章节可以算作是一个扩展章节,主要就来研究 ...

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

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

  4. 关于CPU核心,线程,进程,并发,并行,及java线程之间的关系

    前言:作为一个转行java的小白,一直搞不清楚java中的多线程.于是来梳理一下关于CPU核心,线程,进程,并发,并行,及java线程之间的关系, 1.CPU角度来看: 我们以Intel的Core i ...

  5. C#5.0 异步编程 Async和Await--理解异步方法与线程之间的关系

    这次来理解一下异步方法与线程之间的关系 新建一个控制台程序 代码如下 static void Main(string[] args) { Console.WriteLine("\n进入Mai ...

  6. 进程和线程之间的关系和区别 和 CPU牒

    流程具有一定独立功能的程序关于某个数据集合上的一次执行活动,进程是系统进行资源分配和调度的一个独立单位. 线程是进程的一个实体,是CPU调度和分派的基本单位,它是比进程更小的能独立执行的基本单位. 进 ...

  7. MySQL中的连接、实例、会话、数据库、线程之间的关系

    MySQL中的实例.数据库关系简介 1.MySQL是单进程多线程(而Oracle等是多进程),也就是说MySQL实例在系 统上表现就是一个服务进程,即进程(通过多种方法可以创建多实例,再安装一个端口号 ...

  8. Activity进程和线程之间的关系

    1,四大组件并不是程序(进程)的全部,只是他的零件. 2,应用程序启动后,将创建ActivityThread主线程. 3,同一包中的组件将运行在想通的进程空间里面. 4,不同包中的组件可以通过一定的方 ...

  9. [转]C#综合揭秘——细说进程、应用程序域与上下文之间的关系

    引言 本文主要是介绍进程(Process).应用程序域(AppDomain)..NET上下文(Context)的概念与操作.虽然在一般的开发当中这三者并不常用,但熟悉三者的关系,深入了解其作用,对提高 ...

随机推荐

  1. 【转】VC中对文件的读写

    原文网址:http://www.cnblogs.com/LJWJL/archive/2012/10/06/2712466.html 注意: 1.由于C是缓冲写 所以要在关闭或刷新后才能看到文件内容 2 ...

  2. zookeeper使用和原理探究(一)(转)

    zookeeper介绍zookeeper是一个为分布式应用提供一致性服务的软件,它是开源的Hadoop项目中的一个子项目,并且根据google发表的<The Chubby lock servic ...

  3. 【转】由DFT推导出DCT

    原文地址:http://blog.sina.com.cn/s/blog_626631420100xvxd.htm 已知离散傅里叶变换(DFT)为: 由于许多要处理的信号都是实信号,在使用DFT时由于傅 ...

  4. chmod chgrp chown

    Linux系统中的每个文件和目录都有访问许可权限,用它来确定谁可以通过何种方式对文件和目录进行访问和操作. 文件或目录的访问权限分为只读,只写和可执行三种.以文件为例,只读权限表示只允许读其内容,而禁 ...

  5. Storm系列(十)聚流示例

    功能:将多个数据源的数据汇集到一个处理单元进行集中分类处理: 入口类TestMain 1  ; i < size; i++) { 31              content += input ...

  6. EM 算法

    这个暂时还不太明白,先写一点明白的. EM:最大期望算法,属于基于模型的聚类算法.是对似然函数的进一步应用. 我们知道,当我们想要估计某个分布的未知值,可以使用样本结果来进行似然估计,进而求最大似然估 ...

  7. HW5.8

    public class Solution { public static void main(String[] args) { System.out.printf("%s\t%s\t%s\ ...

  8. Codeforces Round #313 (Div. 2) D.Equivalent Strings (字符串)

    感觉题意不太好懂 = =# 给两个字符串 问是否等价等价的定义(满足其中一个条件):1.两个字符串相等 2.字符串均分成两个子串,子串分别等价 因为超时加了ok函数剪枝,93ms过的. #includ ...

  9. SQL Server 2000的安全配置

    SQL Server 2000的安全配置 数据库是电子商务.金融连同ERP系统的基础,通常都保存着重要的商业伙伴和客户信息.大多数企业.组织连同政府 部门的电子数据都保存在各种数据库中,他们用这些数据 ...

  10. 用sharding技术来扩展你的数据库(一)sharding 介绍

    数据库的sharding技术作为一个“新瓶装旧酒”的概念,在新的应用环境中被赋予了新的意义.随着云计算的发展,sharding在最近几年是越来越火热,越来越多的产品开始声称自己支持sharding功能 ...