问题描述
C ++ 11引入了一种新的完成程序执行的方法 - std :: quick_exit
。
N3242 18.5(p。461):
如 std :: abort(void)
和 std :: _ Exit(int status)
不同之处仅在于将状态传递给父进程的能力,它引发了我的问题。
这意味着 std :: quick_exit
和 std :: abort
std :: quick_exit
调用使用 std :: at_quick_exit
注册的函数,并允许设置返回的状态? / p>
引入此函数的理由是什么?
良好的写作,我只是总结一下。添加此功能专门处理在使用线程时干净地结束程序的难度。本质上,退出是由高度异步的事件开始的,用户关闭用户界面,管理员关闭机器等。这发生不考虑程序启动的线程的状态,他们几乎总是在一个高度不可预测的状态。
在理想的世界中,程序的main()函数要求线程退出,通常通过发信号通知事件,等待线程结束,然后退出main(),以通过exit()进行干净关闭。但是,这种理想很难实现。 线程可以埋在系统调用的深处,比如说,等待一些I / O完成。或者它阻塞在需要由正确顺序的另一个线程信号通知的同步对象。结果很少愉快,真正的程序经常在退出时死锁。
这个问题有一个简单而非常诱人的解决方法:call _exit()。 Kaboom,程序结束后,操作系统扫描了弹片。但显然没有任何清理,有时候会出现像半文件或不完整的dbase事务这样的工件。
std :: quick_exit()提供了另一种选择。类似于_exit()但仍然有执行一些代码的选项,无论是在at_quick_exit注册。
C++11 introduces a new way of finishing program execution—std::quick_exit
.
Quoting the N3242 18.5 (p. 461):
As the definition of std::abort(void)
and std::_Exit(int status)
differ only in ability to pass the status to the parent process, it raises my question.
Does it mean that the only difference in semantics between std::quick_exit
and std::abort
are that std::quick_exit
calls functions registered using std::at_quick_exit
and allows to set the returned status?
What was the rationale for introducing this function?
There's a good write-up available here, I'll just summarize it. This feature was added to specifically deal with the difficulty of ending a program cleanly when you use threads. By nature, the exit is started by a highly asynchronous event, the user closing the user interface, the admin shutting down the machine, etcetera. This happens without regard to the state of the threads the program started, they are almost always in a highly unpredictable state.
In an ideal world, the program's main() function asks the threads to exit, typically by signaling an event, waits for the threads to end and then exits main() for a clean shutdown through exit(). That ideal is however very hard to achieve. A thread could be buried deep inside a system call, say, waiting for some I/O to complete. Or it is blocking on a synchronization object that needs to be signaled by another thread in the right order. The outcome is rarely pleasant, real programs often deadlock on exit. Or crash when the shutdown order is unexpected.
There's a simple and very tempting workaround for this problem: call _exit() instead. Kaboom, program ended, the operating system brooms up the shrapnel. But clearly without any cleanup at all, very messy sometimes with artifacts like a half-written file or an incomplete dbase transaction.
std::quick_exit() offers the alternative. Similar to _exit() but with still the option to execute some code, whatever was registered with at_quick_exit.
这篇关于std :: quick_exit和std :: abort有什么区别,为什么std :: quick_exit需要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!