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.mzph.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;发现如…