pthread_detach(pthread_self())的作用
来源:互联网 发布:五线谱识谱软件 编辑:程序博客网 时间:2024/05/22 06:47
转自http://blog.csdn.net/zjc156m/article/details/9028643
pthread_detach(pthread_self())linux线程执行和windows不同,pthread有两种状态joinable状态和unjoinable状态,
如果线程是joinable状态,当线程函数自己返回退出时或pthread_exit时都不会释放线程所占用堆栈和线程描述符(总计8K多)。只有当你调用了pthread_join之后这些资源才会被释放。
若是unjoinable状态的线程,这些资源在线程函数退出时或pthread_exit时自动会被释放。
unjoinable属性可以在pthread_create时指定,或在线程创建后在线程中pthread_detach自己, 如:pthread_detach(pthread_self()),将状态改为unjoinable状态,确保资源的释放。或者将线程置为 joinable,然后适时调用pthread_join.
其实简单的说就是在线程函数头加上 pthread_detach(pthread_self())的话,线程状态改变,在函数尾部直接pthread_exit线程就会自动退出。省去了给线程擦屁股的麻烦
eg:
pthread_t tid;
int status = pthread_create(&tid, NULL, ThreadFunc, NULL);
if(status != 0)
{
perror("pthread_create error");
}
pthread_detach(tid);
0 0
- pthread_detach(pthread_self())的作用
- pthread_testcancel,pthread_cleanup_push,pthread_cleanup_pop,pthread_detach(pthread_self())的作用
- pthread_detach(pthread_self())
- 关于pthread_detach(pthread_self())
- pthread_detach(pthread_self());用法举例
- 关于pthread_detach(pthread_self())
- 关于pthread_detach(pthread_self())
- pthread_detach的作用
- thread_detach(threadid)和pthread_detach(pthread_self())
- pthread_join和pthread_detach的作用
- pthread_join和pthread_detach的作用
- POSIX线程,线程的客户/服务通信(pthread_join,pthread_exit,pthread_detach,pthread_self)
- linux下多线程 之 pthread_detach(pthread_self())
- linux下多线程 之 pthread_detach(pthread_self())
- linux下多线程 之 pthread_detach(pthread_self())
- linux下多线程 之 pthread_detach(pthread_self())
- linux下多线程 之 pthread_detach(pthread_self())
- linux下多线程 之 pthread_detach(pthread_self())
- 聊聊排名算法
- const小总结
- Android读书笔记---View事件的分发机制(一)
- python 快排
- Dijkstra算法
- pthread_detach(pthread_self())的作用
- APK反编译并且用自己的keystore签名生成新的apk
- BeanPostProcessors使用总结
- 《Java编程思想》学习笔记18——并发编程(一)
- Odoo8在TreeView左上角增加自定义按钮以及通过继承生成自定义的View_Mode
- java web工程经常遇到加了包之后发现提示ClassNotFound
- jQueryEasyUI_简介和教程素材
- 多项选择控件学习
- Cgroup基础概念