Linux中主线程和子线程的终止次序

Linux中主线程和子线程的终止次序,第1张

概述Linux中主线程和子线程的终止次序

linux中pthread是我们进行多线程并发时经常使用的,pthread创建的子线程和主线程的终止顺序有什么样的关系,下面通过代码来总结下。

在代码测试前,先说下结论:

      (1)主线程和子线程之间没有必然的退出次序关系。主线程退出,子线程可以继续执行;子线程退出,主线程也可以继续执行。

      (2)程序加载到内存中执行的时候,进程就会生成一个主线程。虽然主线程和子线程之间没有必然的退出次序关系,但是如果进程终止,那么进程下所有的线程都会终止。

1. 子线程先终止,主线程后终止。 

代码如下:

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <pthread.h>#include <unistd.h>voID printIDs(const char *str) {    pID_t pID = getpID();    pthread_t tID = pthread_self();    printf("%s pID: %u, tID: %u, tID in 0x presentation: 0x%x.n",str, (unsigned int)pID, (unsigned int)tID, (unsigned int)tID);}voID *func(voID *arg) {    printIDs("descendant thread");    pthread_detach(pthread_self());    return ((voID*)0);}int main(voID) {    pthread_t myID;    pthread_create(&myID, NulL, func, NulL);        sleep(1);  // 等待子线程先退出    printIDs("main thread");    return 0;}

运行结果: 

2. 进程结束,所有线程都终止。 

代码如下:

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <pthread.h>#include <unistd.h>voID printIDs(const char *str) {    pID_t pID = getpID();    pthread_t tID = pthread_self();    printf("%s pID: %u, tID: %u, tID in 0x presentation: 0x%x.n",str, (unsigned int)pID, (unsigned int)tID, (unsigned int)tID);}voID *func(voID *arg) {    sleep(1);  // 等待主线程先退出    printIDs("descendant thread");    pthread_detach(pthread_self());    return ((voID*)0);}int main(voID) {    pthread_t myID;    pthread_create(&myID, NulL, func, NulL);        // sleep(1);  // 等待子线程先退出    printIDs("main thread");    return 0;  //进程退出,系统清除所有资源}

运行结果: 

 
此时,由于进程退出,该进程中的所有线程都会终止,系统回收所有的资源,因此子线程还没来得及输出pID和tID就已经退出了。

3、主线程先终止,子线程后终止。

主线程需要调用 pthread_exit() 终止,注意和示例2的区别。

代码如下:

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <pthread.h>#include <unistd.h>voID printIDs(const char *str) {    pID_t pID = getpID();    pthread_t tID = pthread_self();    printf("%s pID: %u, tID: %u, tID in 0x presentation: 0x%x.n",str, (unsigned int)pID, (unsigned int)tID, (unsigned int)tID);}voID *func(voID *arg) {    sleep(1);  // 等待主线程先退出    printIDs("descendant thread");    pthread_detach(pthread_self());    return ((voID*)0);}int main(voID) {    pthread_t myID;    pthread_create(&myID, NulL, func, NulL);        printIDs("main thread");    pthread_exit(NulL);    return 0;  //进程退出,系统清除所有资源}

运行结果:

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 that main() 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标准定义: 

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

注意:这里在main函数中调用pthread_exit()只会是主线程退出,而进程并未退出。参照CSAPP中的讲解,当主线程执行pthread_exit()之后,主线程终止,进程并未终止,而是等待所有的子线程终止之后再结束。

总结: 

一个线程的终止不会影响到另外一个线程。但是进程结束,该进程下所有线程也会立马终止,所有资源会被回收。

总结

以上是内存溢出为你收集整理的Linux中主线程和子线程的终止次序全部内容,希望文章能够帮你解决Linux中主线程和子线程的终止次序所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/1013244.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)