【Linux學習】pthread_create主線程與建立的新線程之間退出關係

來源:互聯網
上載者:User

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

 

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);}

運行結果:

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.