1. 程式人生 > >【Linux學習】pthread_create主執行緒與建立的新執行緒之間退出關係

【Linux學習】pthread_create主執行緒與建立的新執行緒之間退出關係

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

1、  主執行緒等待新執行緒先結束退出,主執行緒後退出。正常執行。

例項程式碼:

#include "apue.h"
#include <pthread.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)
                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;//執行緒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)
                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;//執行緒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)
                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;//執行緒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)
                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);
}

執行結果:





相關推薦

Linux學習pthread_create執行建立執行之間退出關係

我們在一個執行緒中經常會建立另外的新執行緒,如果主執行緒退出,會不會影響它所建立的新執行緒呢?下面就來討論一下。 1、  主執行緒等待新執行緒先結束退出,主執行緒後退出。正常執行。 例項程式碼: #include "apue.h" #include <pthread

netcore基礎ubuntu 16.04 搭建.net core 2.1 linux 執行環境 nginx反向代理 supervisor配置自啟動 .NetCore學習ubuntu16.04 搭建.net core mvc api 執行環境 .Net Core 部署到Ubuntu 16.0

今天來整理下netcore在linux(ubuntu)上的執行環境搭建   對應版本 ubuntu 16.04 .net core 2.1 nginx version: nginx/1.10.3 (Ubuntu) supervisor Supervisorhttp://super

linux 學習shell指令碼自動化登入

每次登入都需要輸入密碼,,好麻煩滴感覺,於是想做個自動登入指令碼,豈不美哉^ . ^ 如何輸入密碼是個大問題,好在有個互動輸入的工具【expect】 首先需要安裝一下, 【Centos】 #檢視是否安裝過 yum list | grep expect #安裝

Linux入門十三、Shell基礎4 -- 多命令執行管道符

十三、Shell基礎4 -- 多命令執行與管道符 1 多命令順序執行 執行符 格式 作用 例子 ; 命令1 ; 命令2 多個命令順序執行,命令之間沒有任何邏輯聯絡 ls ; date ; cd /hello ; date &am

Linux學習Linux環境下利用wget下載https連結內容時報錯:無法本地校驗頒發者的許可權。 要以不安全的方式連線至 ,使用“--no-check-certificate”

一、問題描述 今天在CenterOS系統上使用wget命令下載一個https連結的內容時報錯,如下所示: [[email protected] /]# wget https://mirrors.tuna.tsinghua.edu.cn/gitlab-

linux學習建立虛擬機器並搭建叢集(1)

1.下載VMware Workstation 2.建立虛擬機器 其他都是預設操作,虛擬機器名字根據自己喜好。 記憶體根據電腦情況,我選擇1G的,如果自己記憶體小,可以選512M。 3.編輯虛擬機器設定 虛擬機器-設定,將CD/DVD連線設定為ISO映像檔案,我用

linux 學習linux上安裝Tim(linux mint)

linux下安裝qq確實由點難度,網上提供的好多方法都是藉助虛擬機器(可能不是直接的那種虛擬機器,就是這類技術),很不穩定 Tim還算比較穩定,親測。 下載檔案: 百度雲連結 密碼: 2r

Linux學習Ubuntu下嵌入式交叉編譯環境arm-linux-gcc搭建

(1)首先選擇一個路徑用來存放arm-linux-gcc。我選用的是/home/book,並在下面建立一個資料夾arm-linux-gcc。 (2)利用cp EABI-4.3.3_Emdedsky_20100610.tar.bz2 arm-linux-gcc,將壓縮包EA

Linux 學習檔案目錄管理

檔案與目錄管理 主要介紹和總結Linux系統中的檔案與目錄的基本管理。 1、檢視當前目錄下的檔案和子目錄 ls -aAdfFhilnrRSt 目錄名稱 選項: -a 列出全部檔案與目錄(包含隱藏檔案和當前目錄.以及上層目錄..) -A 除了當前目錄和上層目錄外的其他所有

linux學習——如何將檔案加入到CentOS指定資料夾下

引言 最近在研究linux的東東,一言不合就裝了四個版本的linux,真的是裝的太簡單,但是從最高階往下,發現其實版本最低的那個才適合我正在學的資料,嗚嗚,寶寶心裡苦。 遇到問題

Linux初學系統分割槽之分割槽格式化

前言:該部落格主要為慕課網上一節課的筆記 系統分割槽之分割槽與格式化 在此感謝老師通俗易懂的講解 前幾天我在鬧電腦時也遇到了關於分割槽的問題:主分割槽與邏輯分割槽的區別,在此篇部落格都記錄了下來。 1.分割槽的概念  首先,什麼是分割槽?分割槽其實就是我們的硬碟需要分成

深度學習谷歌雲GPU伺服器建立使用指南(三)

sudo wget -O driver-384.deb http://cn.download.nvidia.com/tesla/384.66/nvidia-diag-driver-local-repo-ubuntu1604-384.66_1.0-1_amd64.deb按照官網“其他”中介紹的安裝方式安裝  2

機器學習梯度下降演算法分析簡述

梯度下降演算法分析與簡述 梯度下降(gradient descent)是一種最優化演算法,基於爬山法的搜尋策略,其原理簡單易懂,廣泛應用於機器學習和各種神經網路模型中。在吳恩達的神經網路課程中,梯度下降演算法是最先拿來教學的基礎演算法。 梯度下降演算法的

機器學習演算法原理詳細推導實現(一):線性迴歸

【機器學習】演算法原理詳細推導與實現(一):線性迴歸 今天我們這裡要講第一個有監督學習演算法,他可以用於一個迴歸任務,這個演算法叫做 線性迴歸 房價預測 假設存在如下 m 組房價資料: 面積(m^2) 價格(萬元) 82.35 193 65.00 213 114.20 255 75.

機器學習演算法原理詳細推導實現(二):邏輯迴歸

【機器學習】演算法原理詳細推導與實現(二):邏輯迴歸 在上一篇演算法中,線性迴歸實際上是 連續型 的結果,即 \(y\in R\) ,而邏輯迴歸的 \(y\) 是離散型,只能取兩個值 \(y\in \{0,1\}\),這可以用來處理一些分類的問題。 logistic函式 我們可能會遇到一些分類問題,例如想要劃

機器學習演算法原理詳細推導實現(三):樸素貝葉斯

【機器學習】演算法原理詳細推導與實現(三):樸素貝葉斯 在上一篇演算法中,邏輯迴歸作為一種二分類的分類器,一般的迴歸模型也是是判別模型,也就根據特徵值來求結果概率。形式化表示為 \(p(y|x;\theta)\),在引數 \(\theta\) 確定的情況下,求解條件概率 \(p(y|x)\) 。通俗的解釋為:

機器學習演算法原理詳細推導實現(四):支援向量機(上)

【機器學習】演算法原理詳細推導與實現(四):支援向量機(上) 在之前的文章中,包括線性迴歸和邏輯迴歸,都是以線性分界線進行分割劃分種類的。而本次介紹一種很強的分類器【支援向量機】,它適用於線性和非線性分界線的分類方法。 函式間隔概念 為了更好的理解非線性分界線,區別兩種分界線對於分類的直觀理解,第一種直觀理解

機器學習演算法原理詳細推導實現(五):支援向量機(下)

【機器學習】演算法原理詳細推導與實現(五):支援向量機(下) 上一章節介紹了支援向量機的生成和求解方式,能夠根據訓練集依次得出\(\omega\)、\(b\)的計算方式,但是如何求解需要用到核函式,將在這一章詳細推導實現。 核函式 在講核函式之前,要對上一章節得到的結果列舉出來。之前需要優化的凸函式為: \[

機器學習演算法原理詳細推導實現(六):k-means演算法

【機器學習】演算法原理詳細推導與實現(六):k-means演算法 之前幾個章節都是介紹有監督學習,這個章節介紹無監督學習,這是一個被稱為k-means的聚類演算法,也叫做k均值聚類演算法。 聚類演算法 在講監督學習的時候,通常會畫這樣一張圖: 這時候需要用logistic迴歸或者SVM將這些資料分成正負兩

WPF學習第六十五章 建立無外觀控制元件

  使用者控制元件的目標是提供增補控制元件模板的設計表面,提供一種定義控制元件的快速方法,代價是失去了將來的靈活性。如果喜歡使用者控制元件的功能,但需要修改使其視覺化外觀,使用這種方法就有問題了。例如,設想希望使用相同的顏色拾取器,但希望使用不同的“面板”,將其更好地融合到已有的應用程