linux创建线程时,需注意线程内存回收问题!
来源:互联网 发布:叶启田心情无人知 编辑:程序博客网 时间:2024/04/27 23:55
有三种方法可以回收线程内存
1.设置线程属性
pthread_attr_t p_pth_attr;
if (0 != pthread_attr_init(&p_pth_attr))
{
perror("pthread_attr_init");
return -1;
}
if (0 != pthread_attr_setdetachstate(&p_pth_attr, PTHREAD_CREATE_DETACHED))
{//此处设置成分离线程内存,一旦线程结束,立刻回收内存
perror("pthread_attr_setdetachstate");
return -1;
}
2.在创建线程后调用pthread_join()
3.使用pthread_detach()
void *ThreadFunc()
{
static intcount = 1;
printf("Create thread %d/n", count);
pthread_detach (pthread_self());
count++;
}
具体方法,参见http://blog.sina.com.cn/s/blog_4b9216f50100cwtc.html
以上只要使用一种方法即可,不可多种同时使用
The pthread_detach() function indicates that system resources for the specified threadshould be reclaimed when the thread ends. If the thread is alreadyended, resources are reclaimed immediately. This routine does not causethe thread to end. After pthread_detach() has been issued, it is not valid to try to pthread_join() with the target thread.
Eventually, you should call pthread_join() or pthread_detach() for every thread that is created joinable (with a detachstate of PTHREAD_CREATE_JOINABLE)so that the system can reclaim all resources associated with thethread. Failure to join to or detach joinable threads will result inmemory and other resource leaks until the process ends.
具体说明,参见http://www.cnblogs.com/cy163/archive/2008/08/17/1269923.html
- linux创建线程时,需注意线程内存回收问题!
- 线程创建时内存注意释放
- Linux 创建线程注意点
- 线程创建未回收导致的内存泄漏问题(进程中的堆栈内存,并不是malloc出来的内存)
- linux线程回收
- linux线程资源回收
- Activity已销毁,创建的线程未回收问题
- Activity已销毁,创建的线程未回收问题
- linux下的线程的创建退出及回收
- linux下的线程的创建退出及回收
- 线程退出和线程资源回收问题
- linux线程资源回收的问题(虚拟内存)
- linux线程资源回收方法
- linux线程资源回收方法
- linux线程资源回收方法
- linux线程资源回收方法
- linux线程资源回收方法
- linux线程资源回收方法
- JAVA端口扫描
- Java 中对象的序列化技术
- Java注释规范
- FOJ--1214--Density Map
- [DirectShow] 002 - How To Play a File
- linux创建线程时,需注意线程内存回收问题!
- ExtJS 自带API文档如何打开?
- VC6.0下透明窗体的实现
- 如何调试OCX控件
- 窗口站(Window station)和桌面(desktop)
- 代理服务器及其主要功能--zl
- J2ME新手入门9要点
- 如何判断一个字符全部包含数字
- [DirectShow] 003 - DirectShow System Overview