Pthread創建線程后必須使用join或detach釋放線程資源

這兩天在看Pthread 資料的時候,無意中看到這樣一句話(man pthread_detach):

Either pthread_join(3) or pthread_detach() should be called for each thread
that an application creates, so that system resources for the thread can be
released. (But note that the resources of all threads are freed when the
process terminates.)

也就是說:每個進程創建以后都應該調用pthread_join 或 pthread_detach 函數,只有這樣在線程結束的時候資源(線程的描述信息和stack)才能被釋放.

之后又查了pthread_join?但是沒有明確說明必須調用pthread_join 或 pthread_detach.

但是再查了 Pthread for win32?pthread_join

When a joinable thread terminates, its memory resources (thread descriptor and stack) are not deallocated until another thread performs pthread_join on it. Therefore,?pthread_join must be called? once? for?each joinable thread created to avoid?memory leaks.


才知道如果在新線程里面沒有調用pthread_join 或 pthread_detach會導致內存泄漏, 如果你創建的線程越多,你的內存利用率就會越高, 直到你再無法創建線程,最終只能結束進程。

解決方法有三個:
1. ? 線程里面調用?pthread_detach(pthread_self())?這個方法最簡單
2.?在創建線程的設置PTHREAD_CREATE_DETACHED屬性
3.?創建線程后用?pthread_join()?一直等待子線程結束。

下面是幾個簡單的例子
1.?調用??pthread_detach(pthread_self())
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *PrintHello(void)
{
pthread_detach(pthread_self());
int stack[1024 * 20] = {0,};
//sleep(1);
long tid = 0;
//printf(“Hello World! It’s me, thread #%ld!\n”, tid);
//pthread_exit(NULL);
}
int main (int argc, char *argv[])
{
pthread_t pid;
int rc;
long t;
while (1) {
printf(“In main: creating thread %ld\n”, t);
rc = pthread_create(&pid, NULL, PrintHello, NULL);
if (rc){
printf(“ERROR; return code from pthread_create() is %d\n”, rc);
//exit(-1);
}
sleep(1);
}
printf(” \n— main End —- \n”);
pthread_exit(NULL);
}
2.?在創建線程的設置PTHREAD_CREATE_DETACHED屬性
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *PrintHello(void)
{
int stack[1024 * 20] = {0,};
//pthread_exit(NULL);
//pthread_detach(pthread_self());
}
int main (int argc, char *argv[])
{
pthread_t pid;
int rc;
long t;
while (1) {
printf(“In main: creating thread %ld\n”, t);
pthread_attr_t attr;
pthread_t thread;
pthread_attr_init (&attr);
pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
rc = pthread_create(&pid, &attr, PrintHello, NULL);
pthread_attr_destroy (&attr);
if (rc){
printf(“ERROR; return code from pthread_create() is %d\n”, rc);
//exit(-1);
}
sleep(1);
}
printf(” \n— main End —- \n”);
pthread_exit(NULL);
}
3. 創建線程后用?pthread_join() 一直等待子線程結束。

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *PrintHello(void)
{
int stack[1024 * 20] = {0,};
//sleep(1);
long tid = 0;
//pthread_exit(NULL);
//pthread_detach(pthread_self());
}
int main (int argc, char *argv[])
{
pthread_t pid;
int rc;
long t;
while (1) {
printf(“In main: creating thread %ld\n”, t);
rc = pthread_create(&pid, NULL, PrintHello, NULL);
if (rc){
printf(“ERROR; return code from pthread_create() is %d\n”, rc);
//exit(-1);
}
pthread_join(pid, NULL);
sleep(1);
}
printf(” \n— main End —- \n”);
pthread_exit(NULL);
}

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

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

相關文章

二維數組求平均值(指針的使用)

#include<stdio.h>int main() {int buf[3][5] {{1,2,3,4,5},{4,5,6,7,8},{7,8,9,10,11}};int i;int j;//求行平均值 for(i 0; i < 3; i){int sum 0;for(j 0; j < 5; j){sum (*(*(buf i) j));}printf("sum %d\n",sum/5);}//求列平均值for(i 0; i …

linux終端關閉時為什么會導致在其上啟動的進程退出?

現象 經常在linux下開發的人應該都有這樣的經驗&#xff0c;就是在終端上啟動的程序&#xff0c;在關閉終端時&#xff0c;這個程序的進程也被一起關閉了。看下面這個程序&#xff0c;為了使進程永遠運行&#xff0c;在輸出helloworld后&#xff0c;循環調用sleep&#xff1a; …

二維數組做函數參數傳遞

#include<stdio.h> //#include<> //二位數組作為函數參數時&#xff0c;可以不指定第一個下標 void print_buf(int (*p)[3],int a,int b) //void print_buf(int p[][3],int a,int b) {int i,j;for(i 0 ; i < a; i){for(j 0; j < b; j){printf("p[%…

libevent源碼深度剖析

第一章 1&#xff0c;前言 Libevent是一個輕量級的開源高性能網絡庫&#xff0c;使用者眾多&#xff0c;研究者更甚&#xff0c;相關文章也不少。寫這一系列文章的用意在于&#xff0c;一則分享心得&#xff1b;二則對libevent代碼和設計思想做系統的、更深層次的分析&#xff…

函數返回指針類型(strchr函數)

#include<stdio.h> #include<string.h> char *mystrchr(char *s,char c) {while(*s){if(*s c){return s;}s;}return NULL; }int main() {char str[100] "hello world";//char* s strchr(str,a);char *s mystrchr(str,e);//返回ello world字符串 prin…

函數與指針

#include<stdio.h>int add(int a,int b) {return ab; }int main() {void *p(int,char *); //聲明了一個函數 &#xff0c;函數名為p&#xff0c;函數返回值為void*,函數的 void (*p)(int,char *);//定義了一個指向參數為int和char*返回值為void的函數指針//定義一個參數為…

使用指針在函數中交換數值

#include<stdio.h>void swap(int* a,int *b) {/*int temp *a;*a * b;*b temp;*/*a *b;*b *a - *b;*a *a - *b; }int main() {int a 10;int b 20;swap(&a,&b);printf("a %d,b %d\n",a,b);} 轉載于:https://www.cnblogs.com/wanghao-boke/p/1…

linux C 基于鏈表鏈的定時器

源碼如下&#xff1a;util_timer.h#ifndef LST_TIMER#define LST_TIMER#include <time.h>#include <sys/time.h>#include <stdlib.h>#include <signal.h>#define BUFFER_SIZE 64struct util_timer;/*struct client_data{sockaddr_in address;int sockf…

libevent學習筆記 一、基礎知識

一、libevent是什么libevent是一個輕量級的開源的高性能的事件觸發的網絡庫&#xff0c;適用于windows、linux、bsd等多種平臺&#xff0c;內部使用select、epoll、kqueue等系統調用管理事件機制。它被眾多的開源項目使用&#xff0c;例如大名鼎鼎的memcached等。特點&#xff…

漢字逆置

在計算機中&#xff0c;一個漢字用無法用1個字節來表示 #include<stdio.h> int main() {char buf[256] "你好";int len 0;while(buf[len]);len--;printf("%d\n",len);// 4一個漢字兩個字節 //printf("%p\n",buf);return 0; } 在windows下…

libevent項目分析(一) -- 準備階段

項目的簡介 我理解libevent是一個輕量級的&#xff0c;跨平臺高效的&#xff08;C語言實現&#xff09;事件驅動庫&#xff0c;類似于ACE項目中的ACE_Reactor&#xff0c;它實現了網絡通訊套接口I/O事件&#xff0c;定時器事件&#xff0c;信號事件的監聽和事件處理函數回調機制…

混合字符串字符數統計

因為漢字占一個以上字節&#xff0c;如何統計一個既有漢字又有字母的字符串呢&#xff1f; 漢字在計算機中的ASCII是以負數來與其他普通字符的ASCII區分的。 #include<stdio.h> int main() {char buf[256] "你好世界";printf("%d\n",buf[0]); //-60…

清除字符串空格

1.清除字符串中右邊的空格 從字符串尾部開始&#xff0c;找到非空格處&#xff0c;將下一個字符置為0即可。 //清除右邊空格 #include<stdio.h> int main() {char buf[] "hello world ";int len 0;//calculate the length of stringwhile(buf[len]);le…

淺談auto_ptr智能指針

引入智能指針&#xff1a;智能指針的實現原理&#xff1a; 資源分配即初始化RAII(Resource Acquisition Is Initialization)&#xff1a; 定義一個類來封裝資源的分配和釋放&#xff0c;在構造函數完成資源的分配和初始化&#xff0c;在析構函數完成資源的清理&#xff0c;可…

隨機數

隨機數產生器rand(),頭文件為#include<stdlib.h> #include<stdio.h> #include<stdlib.h>int main() {int value;int i;for(i 0; i < 10; i){value rand();printf("value %d\n",value);}return 0; } 運行結果&#xff1a; value 41 value 1…

多重繼承之虛繼承(主要是為了解決產生的數據冗余問題)

虛繼承 是面向對象編程中的一種技術&#xff0c;是指一個指定的基類&#xff0c;在繼承體系結構中&#xff0c;將其成員數據實例共享給也從這個基類型直接或間接派生的其它類。形式&#xff1a;在繼承定義中包含了virtual關鍵字的繼承關系&#xff0c;如下圖中&#xff0c;類A就…

通過syslog接收遠程日志

通過syslog接收遠程日志通過syslog接收遠程主機的日志&#xff0c;需要做一些環境配置。客戶機A通過syslog將日志信息發送到服務主機B&#xff08;或稱日志采集服務器&#xff09;。以下說明配置過程&#xff08;我的實驗環境是&#xff0c;客戶機A&#xff1a;Solaris 10&…

linux syslog服務器配置,自動發日志到另一臺日志服務器

1.客戶端:168.1.20.66修改/etc/syslog.conf 添加syslog.info 168.1.80.302.日志服務器:168.1.80.30修改/etc/sysconf/syslog 修改SYSLOGD_OPTIONS為 "-r -x -m 0" #-r表示允許接收外來的消息&#xff0c;-x表示不解析DNS, #-m 0表示時間戳標記間隔,如果指定只接…

Make文件(一)

基本規則&#xff1a; 目標&#xff1a;依賴 &#xff08;tab&#xff09;規則 目標&#xff1a;需要生成的目標文件 依賴&#xff1a;生成該目標所需的一些文件 規則&#xff1a;由依賴文件生成目標文件的手段 tab&#xff1a;每條規則前必須以tab開頭&#xff0c;使用空格不行…

移植驅動完畢后加載時的version magic報錯原因以及解決辦法

History:2012-02-17Author:yingru移植rt3070的AP驅動到裝有fedora14的PC機上時&#xff0c;模塊編譯完畢后&#xff0c;加載時提示invalid module format。PC機環境介紹&#xff1a;內核版本&#xff1a;2.6.35.6-45.fc14.i686命令行輸入dmesg查看最后的日志&#xff0c;發現如…