pthread_create创建线程后必须使用join或detach释放线程资源[内存泄漏]

简介: 原文:http://wifihack.net/blog/2009/12/pthread-create-join-detach-release-thread/这两天在看Pthread 资料的时候,无意中看到这样一句话(man pthread_detach):Either pth...

原文:http://wifihack.net/blog/2009/12/pthread-create-join-detach-release-thread/

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

相关文章
|
1月前
|
存储 JSON 监控
Higress Controller**不是将配置信息推送到Istio的内存存储里面的**。
【2月更文挑战第30天】Higress Controller**不是将配置信息推送到Istio的内存存储里面的**。
14 1
|
2月前
|
存储 编译器 C语言
C语言:数据在内存中的存储形式
C语言:数据在内存中的存储形式
|
1月前
|
存储 C语言
C语言--------数据在内存中的存储
C语言--------数据在内存中的存储
26 0
|
7天前
|
存储 NoSQL Oracle
Oracle 12c的内存列存储:数据的“闪电侠”
【4月更文挑战第19天】Oracle 12c的内存列存储以超高速度革新数据处理,结合列存储与内存技术,实现快速查询与压缩。它支持向量化查询和并行处理,提升效率,但需合理配置以平衡系统资源。作为数据管理员,应善用此功能,适应业务需求和技术发展。
|
17天前
|
存储 C语言
数据在内存中的存储2
数据在内存中的存储2
|
17天前
|
存储 编译器
数据在内存中的存储1
数据在内存中的存储
|
28天前
|
存储 编译器 程序员
【C语言】整形数据和浮点型数据在内存中的存储
【C语言】整形数据和浮点型数据在内存中的存储
16 0
|
1月前
|
存储 Windows 容器
浮点数在内存中的存储
浮点数在内存中的存储
18 2
|
1月前
|
存储 小程序 C语言
【深度剖析数据在内存中的存储】C语言
【深度剖析数据在内存中的存储】C语言
|
1月前
|
存储 小程序 C语言
【C语言进阶】深度剖析数据在内存中的存储
【C语言进阶】深度剖析数据在内存中的存储