我們在一個線程中經常會創建另外的新線程,如果主線程退出,會不會影響它所創建的新線程呢?下面就來討論一下。
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); }
查看本欄目更多精彩內容:http://www.bianceng.cn/OS/unix/
運行結果:
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)
作者信息:csdn博客 ctthuangcheng