std :: quick_exit和std :: abort有什么区别,为什么std :: quick_exit需要? [英] What is the difference between std::quick_exit and std::abort and why was std::quick_exit needed?

查看:712
本文介绍了std :: quick_exit和std :: abort有什么区别,为什么std :: quick_exit需要?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++ 11引入了一种新的完成程序执行的方法 - std :: quick_exit



N3242 18.5(p。461):


  [[noreturn]] void quick_exit )noexcept; 

效果:通过调用注册的函数at_quick_exit 按照注册的相反顺序称为
,除了在任何先前注册的函数
在注册时已被调用时,将调用函数
。由于调用 quick_exit ,对象不得销毁
。如果控制留下一个
注册函数,由 quick_exit 调用,因为函数不会
为抛出的异常提供处理程序, terminate ()
[注意: at_quick_exit 可以从不同的
线程调用注册的函数,因此注册的函数应该
不依赖具有线程存储持续时间的对象的身份。 -
end note]调用注册函数后, quick_exit 将调用
_Exit(status) 。 [注意:标准文件缓冲区不刷新。参见:ISO C 7.20.4.4。 - end note]


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):

[[noreturn]] void quick_exit(int status) noexcept;

Effects: Functions registered by calls to at_quick_exit are called in the reverse order of their registration, except that a function shall be called after any previously registered functions that had already been called at the time it was registered. Objects shall not be destroyed as a result of calling quick_exit. If control leaves a registered function called by quick_exit because the function does not provide a handler for a thrown exception, terminate() shall be called. [ Note: at_quick_exit may call a registered function from a different thread than the one that registered it, so registered functions should not rely on the identity of objects with thread storage duration. — end note ] After calling registered functions, quick_exit shall call _Exit(status). [ Note: The standard file buffers are not flushed. See: ISO C 7.20.4.4. — end note ]

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需要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆