http://www.cppblog.com/prayer/archive/2012/04/23/172427.html

这两天在看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);
}

c++ Pthread创建线程后必须使用join或detach释放线程资源的更多相关文章

  1. java 笔记(5) —— 线程,yield,join

    一.线程各个状态与转换: 新建状态:用new语句创建的线程对象处于新建状态,此时它和其它的java对象一样,仅仅在堆中被分配了内存 .就绪状态:当一个线程创建了以后,其他的线程调用了它的start() ...

  2. C# Thread.Join的报错情形——论执行完的线程能够成功执行Join吗

    结论: 能, 执行完的线程调用Join不会抛出错误. Thread.Join()是什么? Join()函数用于阻塞地等待线程结束, 其行为是在线程A中调用了线程B的Join()后, 线程A将一直阻塞在 ...

  3. [译]线程生命周期-理解Java中的线程状态

    线程生命周期-理解Java中的线程状态 在多线程编程环境下,理解线程生命周期和线程状态非常重要. 在上一篇教程中,我们已经学习了如何创建java线程:实现Runnable接口或者成为Thread的子类 ...

  4. pthread创建线程的简单演示

      使用pthread创建子线程的简单步骤 导入头文件 #import <pthread.h> 指定新线程标识符 使用pthread创建线程的函数 根据result = 0 与否判断子线程 ...

  5. C# WebService中任务处理线程创建子线程后

    protected void WriteLog(string message) { lock (lockObject) { var file = System.IO.File.AppendText(& ...

  6. 创建线程后马上CloseHandle(threadhandle)起什么作用

    原文:http://www.cnblogs.com/eddyshn/archive/2010/04/14/1711674.html HANDLE threadhandle = CreateThread ...

  7. Java Thread.join()详解--父线程等待子线程结束后再结束

    目录(?)[+] 阅读目录 一.使用方式. 二.为什么要用join()方法 三.join方法的作用 join 四.用实例来理解 打印结果: 打印结果: 五.从源码看join()方法   join是Th ...

  8. C++随笔(1)——关于C++11中的线程创建,join和detach

    主要是和之前的博文有关,之前在这里有一部分代码是通过创建新的进程来应对新的用户请求的,但是基本没怎么解释怎么用的,所以这里做点小笔记. join 首先引入的库: #include <thread ...

  9. linux多进/线程编程(7)——多线程1(线程的创建,回收,分离,设置线程属性等)

    参考资料: 1.博客1:https://blog.csdn.net/zhou1021jian/article/details/71531699 2.博客2:https://blog.csdn.net/ ...

随机推荐

  1. Windows安装pycrypto失败记录

    Windows 10家庭中文版,Python 3.6.4, 180824测试前端加密文本在后台揭秘,查询后发现,可以使用pycrypto模块实现,那么,安装它(pip),结果安装失败了. 本文暂时记录 ...

  2. 探秘Java类加载

    Java是一门面向对象的编程语言. 面向对象以抽象为基础,有封装.继承.多态三大特性. 宇宙万物,经过抽象,均可归入相应的种类.不同种类之间,有着相对井然的分别. Java中的类,便是基于现实世界中的 ...

  3. 5 个 Laravel Eloquent 小技巧

    1. 快速生成 Model & Migration 这并不是一个很多人知道的小技巧,为文章生成 Model 和 Migration. $ php artisan make:migration ...

  4. HDU 2512 一卡通大冒险(第二类斯特林数+贝尔数)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2512 题目大意:因为长期钻研算法, 无暇顾及个人问题,BUAA ACM/ICPC 训练小组的帅哥们大部 ...

  5. 《jquery实战》javascript 必知必会(1)

    A1 javascript对象的基本原理 JS 的 Object 与其他兄弟面向对象所定义的根本对象,几乎没有什么共同之处. JS 的 Object 一旦创建,它不持有任何数据,而且不表示什么语义. ...

  6. Notes for building gimp-print

    First try, build gimp-print on ubuntu. 1. Install all dependencies. sudo apt-get install libcups2-de ...

  7. Ubuntu出现apt-get: Package has no installation candidate问题

    今天在安装 vim 的时候出现了 Package 'vim' has no installation candidate的问题 解决方法如下:# apt-get update# apt-get upg ...

  8. .NetCore Linux环境下安装InfluxDB以及配置设置

    Linux下安装 确定需要安装的版本,我的linux是干净的,所以我需要先安装wget yum -y install wget 下载安装 wget https://dl.influxdata.com/ ...

  9. JS框架图

    一.JS框架

  10. 【Atcoder】ARC102 题解

    C - Triangular Relationship 题解 枚举一个数%K的值然后统计另两个 代码 #include <bits/stdc++.h> #define enter putc ...