多线程处理thread
来源:互联网 发布:bluebird.js 编辑:程序博客网 时间:2024/06/05 13:32
转载出: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);
}
0 0
- 多线程处理thread
- Python中thread 多线程处理
- Python模块学习 ---- thread 多线程处理
- Python模块学习 ---- thread 多线程处理
- Python模块学习 ---- thread 多线程处理
- Python模块学习: thread 多线程处理
- 多线程Thread
- 多线程 Thread
- 多线程Thread
- 多线程-thread
- Thread-多线程
- 多线程Thread
- 多线程THread
- Thread多线程
- Thread多线程
- J2me多线程中Thread类处理结束线程
- Android多线程,异步消息处理机制, Thread, AsyncTask 简单总结
- Java通过继承Thread类实现多线程 ,处理同一个共享资源
- 在python官网下载了安装包双击之后没有反应?
- 如何将本地文件上传到windows服务器
- 各种居中系列
- Redis与Memcached的区别
- DECLARE CURSOR (Transact-SQL)
- 多线程处理thread
- String处理工具类
- java跳出多重循环
- Nginx负载平衡 + Tomcat + 会话存储Redis配置要点
- 富士通dpk2085打印机驱动
- Android中两种获取状态栏高度的方法
- effective3关于函数对象的理解
- web网站css,js更新后客户浏览器缓存问题,需要刷新才能正常展示的解决办法
- Selenium 初学笔记--Selenium IDE-简单使用