线程退出【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主线程与创建的新线程之间退出关系的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: rhel6下配置ftp服务器
- 下一篇: PHP生词小记