pthread_exit() in main()
来源:互联网 发布:淘宝商品如何靠前 编辑:程序博客网 时间:2024/05/29 07:37
Most threads call pthread_exit()
implicitly on return from the thread start routine. Besides, pthread_exit()
also can be used to terminate the initial process thread in main()
, leaving other threads to continue operation. The process will go away automatically when the last thread terminates. If you don’t care about the process exit status, or if is difficult to know the created thread IDs(e.g. created by third party APIs), you can call
pthread_detach(pthread_self());
pthread_exit(NULL);
at the end of the main()
function.
However, you must be carefull when using pthread_exit()
in the main thread. Because after calling pthread_exit()
and before the process really terminate, the process becomes “zombie” - it still exists even though it is “dead”, just like a Unix/Linux process that’s terminated but hasn’t yet been “reaped” by a wait
operation. The zombie process may retain most or all of the system resources that it used when running, so it is not a good idea to leave threads in this state for longer than necessary. And obviously, zombie process cannot save you resources! So also don’t try pthread_exit()
for saving CPU and memory.
I also noticed a undocumented problem caused by pthread_exit()
- it may lead to failure of open procfs(/proc/
) files! If one of your threads would open /proc/mounts
(currently I only find this file will go wrong, and other procfs files like /proc/cpuinfo
or /proc/uptime
can be successfully opened) during its life, and pthread_exit()
is called after creating these threads in the main thread, you will meet the “Invalid argument” error because of functions like open("/proc/mounts",'r')
.
- pthread_exit() in main()
- pthread_exit()
- pthread_exit
- pthread_exit
- pthread_exit()
- pthread_exit()
- pthread_exit()
- pthread_exit
- pthread_exit()
- pthread_exit()
- Use pthread_exit() To Terminate main() With The Other Threads Alive
- pthread_exit简介
- pthread_join/pthread_exit
- pthread_exit手册
- pthread_join/pthread_exit
- pthread_exit函数
- pthread_self 、pthread_exit
- pthread_exit()函数
- Hduoj2955【01背包】
- jquery 点击某行变换背景颜色,其他行还原为原来颜色
- xcode6 关于iphone6、iphone6plus适配(一)
- 理解 Node.js 里的 process.nextTick()
- JavaScript类型的检测
- pthread_exit() in main()
- VS自带WPF,Silverlight控件样式和模板
- NGUI的切片Sliced Sprite的制作
- memcache初步认识(文稿)
- Ubuntu下Python版本管理 pyenv
- ubuntu 安装 TM2009 QQ2013
- Android双向滑动菜单完全解析,教你如何一分钟实现双向滑动特效
- js去掉字符串前后空格的五种方法
- U3d学习第4天--u3d脚本代码基础判断和循环语句