日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

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

發布時間:2025/5/22 linux 54 豆豆
生活随笔 收集整理的這篇文章主要介紹了 线程退出【Linux学习】pthread_create主线程与创建的新线程之间退出关系 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本篇文章個人在青島吃飯的時候突然想到的...最近就有想寫幾篇關于線程退出的文章,所以回家到之后就奮筆疾書的寫出來發布了

????我們在一個線程中經常會創立另外的新線程,如果主線程退出,會不會影響它所創立的新線程呢?下面就來討論一下。

?

????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主线程与创建的新线程之间退出关系的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。