exit:c++非线程安全
来源:互联网 发布:淘宝的司法拍卖靠谱 编辑:程序博客网 时间:2024/06/06 04:46
exit调用会终止整个进程,在_exit的基础上执行一系列用户空间操作比如刷新缓冲区。_exit是直接交给内核,exit先执行清除操作再交给内核。exit或_exit时,系统无条件的停止剩下所有操作,清除包括PCB在内的各种数据结构,并终止本进程的运行。
因此exit在C++中非线程安全,看下面这个例子:
#include<iostream>#include<stdlib.h>#include<pthread.h>#include<unistd.h>using namespace std;void fun(){ exit(1);//###1### 刷新流,析构全局对象,1表示异常返回给调用者,调用者可以根据该值进行相应处理 //_exit(0);//###2### 不会刷新流等,不会析构全局对象}class test{ public: test(){ pthread_mutex_init(&mutex,NULL); } void doit(){ pthread_mutex_lock(&mutex); fun(); pthread_mutex_unlock(&mutex); } ~test(){ cout<<"~test"<<endl; pthread_mutex_lock(&mutex);//可能引起死锁 pthread_mutex_unlock(&mutex); } private: pthread_mutex_t mutex;};test one;//###3###exit会析构全局对象造成死锁,_exit不会析构全局对象int main(){ //test one;//###4###局部对象不会被exit/_exit终止析构 one.doit();}
程序输出:
~test
//出现死锁
- exit:c++非线程安全
- 非线程/线程安全
- 线程安全、非线程安全
- 线程安全与非安全
- SimpleDateFormat非线程安全
- SimpleDateFormat非线程安全
- Servlet非线程安全
- Java 非线程安全
- SimpleDateFormat 非线程安全
- ArrayList 非线程安全
- 非线程安全
- SimpleDateFormat非线程安全
- volatile---非线程安全
- SimpleDateFormat非线程安全
- c语言非线程安全函数引发的BUG一列
- Java线程安全和非线程安全
- Java线程安全和非线程安全
- Java线程安全和非线程安全
- 用 Hadoop 进行分布式数据处理,第 1 部分: 入门
- nginx 开机自动启动教程
- Android学习篇章33-Service基础
- OCP-1Z0-053-V12.02-159题
- linux下使用GCC生存和使用静态、动态库
- exit:c++非线程安全
- udx linux/win/android开发包发布
- [HGE]-源码分析-10 resource
- mmap 和 munap函数
- vsftpd自启动(开机启动)的三种方法
- android之横竖屏切换
- 话说Chrome进程体系与插件管理
- A Crash Course on the Depths of Win32 Structured Exception Handling
- 简述什么是JAVA中的多态