線程回收

pthread_join函數

阻塞等待線程退出,獲取線程退出狀態????????? 其作用,對應進程中 waitpid() 函數。

?????? int pthread_join(pthread_t thread, void **retval); 成功:0;失敗:錯誤號

?????? 參數:thread:線程ID (【注意】:不是指針);retval:存儲線程結束狀態。

?????? 對比記憶:

????????????? 進程中:main返回值、exit參數-->int;等待子進程結束 wait 函數參數-->int *

????????????? 線程中:線程主函數返回值、pthread_exit-->void *;等待線程結束 pthread_join 函數參數-->void **

【練習】:參數 retval 非空用法。??????????????????????????????????????????????????????????????????????????????? ?????? 【pthrd_exit_join.c】

調用該函數的線程將掛起等待,直到id為thread的線程終止。thread線程以不同的方法終止,通過pthread_join得到的終止狀態是不同的,總結如下:

  1. 如果thread線程通過return返回,retval所指向的單元里存放的是thread線程函數的返回值。
  2. 如果thread線程被別的線程調用pthread_cancel異常終止掉,retval所指向的單元里存放的是常數PTHREAD_CANCELED。
  3. 如果thread線程是自己調用pthread_exit終止的,retval所指向的單元存放的是傳給pthread_exit的參數。
  4. 如果對thread線程的終止狀態不感興趣,可以傳NULL給retval參數。

?

【練習】:使用pthread_join函數將循環創建的多個子線程回收。? ? ? ? ? ??

/***
thread_join.c
***/
#include<stdio.h>
#include<error.h>
#include<string.h>
#include<unistd.h>
#include<stdlib.h>
#include<pthread.h>typedef struct 
{char ch;int var;char str[64];
}exit_t;void *thrd_func(void *arg)
{pthread_exit((void *)1);
}int main()
{pthread_t tid;int ret;int *retval;printf("In main 1 : thread id = %lu, pid = %u\n",pthread_self(),getpid());ret = pthread_create(&tid,NULL,thrd_func,NULL);if(0 != ret){fprintf(stderr,"pthread_create error : %s \n",strerror(ret));exit(1);}pthread_join(tid,(void **)&retval);printf("--------------%d\n",(int)retval);pthread_exit((void*)1);
}

運行結果:

ubuntu1604@ubuntu:~/wangqinghe/linux/20190819$ ./thread_join

In main 1 : thread id = 139721544345344, pid = 12974

--------------1

/***
pthread_join_struct.c
***/
#include<stdio.h>
#include<error.h>
#include<string.h>
#include<unistd.h>
#include<stdlib.h>
#include<pthread.h>typedef struct 
{char ch;int var;char str[64];
}exit_t;void *thrd_func(void *arg)
{exit_t *retvar = (exit_t *)malloc(sizeof(exit_t));retvar->ch = 'm';retvar->var = 200;strcpy(retvar->str,"my thread\n");pthread_exit((exit_t *)retvar);
}int main()
{pthread_t tid;int ret;exit_t *retval;printf("In main 1 : thread id = %lu, pid = %u\n",pthread_self(),getpid());ret = pthread_create(&tid,NULL,thrd_func,NULL);if(0 != ret){fprintf(stderr,"pthread_create error : %s \n",strerror(ret));exit(1);}pthread_join(tid,(void **)&retval);printf("ch = %c,var = %d,str = %s\n--------------%d\n",retval->ch,retval->var,retval->str);free(retval);pthread_exit((void*)1);
}

運行結果:

ubuntu1604@ubuntu:~/wangqinghe/linux/20190819$ ./thread_join

In main 1 : thread id = 140316487100160, pid = 13121

ch = m,var = 200,str = my thread

?

--------------202

/***
thread_join_loop.c
***/
#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<pthread.h>int var = 100;void *tfn(void *arg)
{int i;i = (int)arg;sleep(i);if(1 == i){var = 333;printf("var = %d\n",var);return (void*)var;}else if( 3 == i){var = 888;printf("i'm %dth pthread,pthread id = %lu ,var = %d\n",i+1,pthread_self(),var);pthread_exit((void*)var);}else{printf("i'm %dth pthread,pthread id = %lu ,var = %d\n",i+1,pthread_self(),var);pthread_exit((void*)var);        }return NULL;    
}int main()
{pthread_t tid[5];int i;int *ret[5];for(i = 0; i < 5; i++){pthread_create(&tid[i],NULL,tfn,(void*)i);}for(i = 0; i < 5; i++){pthread_join(tid[i],(void**)&ret[i]);printf("-----------%d's ret = %d\n",(int)ret[i]);}printf("I'm main pthread tid = %lu var = %d\n",pthread_self(),var);sleep(i);return 0;
}

運行結果:

ubuntu1604@ubuntu:~/wangqinghe/linux/20190819$ ./thread_loop

i'm 1th pthread,pthread id = 139809124009728 ,var = 100

-----------100's ret = -645238160

var = 333

-----------333's ret = -651408960

i'm 3th pthread,pthread id = 139809107224320 ,var = 333

-----------333's ret = -659801664

i'm 4th pthread,pthread id = 139809098831616 ,var = 888

-----------888's ret = -668194368

i'm 5th pthread,pthread id = 139809090438912 ,var = 888

-----------888's ret = 0

I'm main pthread tid = 139809132349184 var = 888

?

轉載于:https://www.cnblogs.com/wanghao-boke/p/11389714.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/384852.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/384852.shtml
英文地址,請注明出處:http://en.pswp.cn/news/384852.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Python3數字

Python3數字數據類型用于存儲數值。 數據類型是不允許改變的&#xff0c;這就意味著&#xff0c;如果改變數字數據類型的值&#xff0c;將重新分配內存空間。 Python支持三種不同不同的數值類型&#xff1a; 整型&#xff08;int&#xff09;&#xff1a;通常是被稱為整型或整數…

多進程服務器

注意&#xff1a;包含了“wrap.c” 和“wrap.h”文件在上篇博客中 /*** server.c ***/ #include<stdio.h> #include<string.h> #include<netinet/in.h> #include<arpa/inet.h> #include<signal.h> #include<sys/wait.h> #include<ctype…

服務器之select

select select能監聽的文件描述符個數受限于FD_SETSIZE,一般為1024&#xff0c;單純改變進程打開的文件描述符個數并不能改變select監聽文件個數解決1024以下客戶端時使用select是很合適的&#xff0c;但如果鏈接客戶端過多&#xff0c;select采用的是輪詢模型&#xff0c;會大…

服務器之poll

poll服務器方法采用將監聽端口用數組存放起來&#xff0c;這樣就不需要輪詢的監聽整個文件描述符了 #include <poll.h> int poll(struct pollfd *fds, nfds_t nfds, int timeout);struct pollfd {int fd; /* 文件描述符 */short events; /* 監控的事件 */short revents; …

Mysql數據庫簡單使用(二)

Mysql導入.sql文件 進入數據庫&#xff08;要導入的數據庫&#xff09;數據庫中有要導入.sql文件名的數據庫&#xff0c;沒有則新建。source 路徑文件名souce /home/robot/csql.sql 數據庫文件.sql文件放在/home/robot目錄下 按照時間刪除數據庫數據 DELETE FROM 表名 WHERE 時…

Python3集合

集合&#xff08;set&#xff09;是一個無序的不重復元素序列。 可以使用大括號{ } 或set&#xff08;&#xff09;函數來創建集合&#xff0c;注意&#xff1a;創建一個空集合必須用set(),{ }是用來創建一個空字典的。 創建格式&#xff1a; param {value01,value02,…} set(…

Python3條件判斷

if語句&#xff1a; Python中if語句的一般形式如下&#xff1a; if condition_1:statement_block_1 elif condition_2:statement_block_2 else:statement_block_3 if語句關鍵詞&#xff1a; if – elif – else 注意&#xff1a; 每個條件后面要使用冒號:使用縮進來劃分語句塊&…

Python3循環

Python中while語句的一般形式&#xff1a; while 判斷條件: 語句 同樣需要注意冒號和縮進&#xff0c;另外在Python中沒有do…while循環 下面的實例計算1到100總和 ##calc.py n 100sum 0 counter 1 while counter < n:sum sum countercounter 1print("total from…

Python3迭代器和生成器

迭代器 迭代是Python最強大的功能之一&#xff0c;是訪問元素集合的一種方法。 迭代器是一個可以記住遍歷的位置的對象。 迭代器對象從集合的第一個元素開始訪問&#xff0c;直到所有的元素被訪問完結束&#xff0c;迭代器只能向前不會后退。 迭代器有兩個基本方法&#xff0c;…

Pythton3實例

計算1-100之和 #add.py n 0 sum 0 for n in range(0,101):sum n print(sum) 實現99乘法法則 #mul.py i 1 while i < 9:j 1while j < i:mut j*iprint("%d * %d %d"%(j,i,mut),end" ")j 1print(" ")i 1 運算結果: robotubuntu:~/wa…

Python3函數

函數是組織好的&#xff0c;可重復使用的&#xff0c;用來實現單一&#xff0c;或相關功能的代碼段。 函數能提高應用的模塊性&#xff0c;和代碼的重復使用率。 定義一個函數 可以定義一個由自己想要功能的函數&#xff0c;以下是簡單規則&#xff1a; l 函數代碼塊是以def關…

epoll函數

epoll是Linux下多路復用IO接口select/poll的增強版本&#xff0c;它能顯著提高程序在大量并發連接中只有少量活躍的情況下的系統CPU利用率&#xff0c;因為它會復用文件描述符集合來傳遞結果而不用迫使開發者每次等待事件之前都必須重新準備要被偵聽的文件描述符集合&#xff0…

epoll事件模型

事件模型 EPOLL事件有兩種模型&#xff1a; Edge Triggered (ET) 邊緣觸發只有數據到來才觸發&#xff0c;不管緩存區中是否還有數據。 Level Triggered (LT) 水平觸發只要有數據都會觸發。 思考如下步驟&#xff1a; 假定我們已經把一個用來從管道中讀取數據的文件描述符(RFD)…

epoll反應堆模型代碼

libevent函數庫核心思想 /*** epoll_loop.c ***/ #include<stdio.h> #include<sys/epoll.h> #include<sys/socket.h> #include<arpa/inet.h> #include<fcntl.h> #include<unistd.h> #include<errno.h> #include<string.h> #in…

UDP廣播

廣播是在局域網之間的一對多的通信方式&#xff0c;使用的udp協議 /*** client.c ***/ #include <stdio.h> #include <unistd.h> #include <string.h> #include <sys/socket.h> #include <arpa/inet.h>#define SERVER_PORT 8000 #define MAXLINE…

UDP組播

多播(組播) 組播組可以是永久的也可以是臨時的。組播組地址中&#xff0c;有一部分由官方分配的&#xff0c;稱為永久組播組。永久組播組保持不變的是它的ip地址&#xff0c;組中的成員構成可以發生變化。永久組播組中成員的數量都可以是任意的&#xff0c;甚至可以為零。那些沒…

Python3數據結構

列表&#xff1a; Python列表是可變的&#xff0c;這是它區別于字符串數組和元組的最重要的特點。列表可以修改&#xff0c;而字符串和元組不能。 以下是Python中列表的描述方法&#xff1a; 方法 描述 list.append(x) 將元素添加到列表結尾 list.extend(L) 通過添加指定列…

sed、awk工具

ed sed意為流編輯器&#xff08;Stream Editor&#xff09;&#xff0c;在Shell腳本和Makefile中作為過濾器使用非常普遍&#xff0c;也就是把前一個程序的輸出引入sed的輸入&#xff0c;經過一系列編輯命令轉換為另一種格式輸出。sed和vi都源于早期UNIX的ed工具&#xff0c;所…

C語言正則表達式

POSIX規定了正則表達式的C語言庫函數&#xff0c;詳見regex(3)。我們已經學習了很多C語言庫函數的用法&#xff0c;讀者應該具備自己看懂man手冊的能力了。本章介紹了正則表達式在grep、sed、awk中的用法&#xff0c;學習要能夠舉一反三&#xff0c;請讀者根據regex(3)自己總結…

makefile通用版本

實際當中程序文件比較大&#xff0c;這時候對文件進行分類&#xff0c;分為頭文件、源文件、目標文件、可執行文件。也就是說通常將文件按照文件類型放在不同的目錄當中&#xff0c;這個時候的Makefile需要統一管理這些文件&#xff0c;將生產的目標文件放在目標目錄下&#xf…