What is the difference between std::quick_exit and std::abort and why was std::quick_exit needed?

后端 未结 3 1855
借酒劲吻你
借酒劲吻你 2020-12-03 02:21

C++11 introduces a new way of finishing program execution—std::quick_exit.

Quoting the N3242 18.5 (p. 461):

[[noreturn]] vo         


        
3条回答
  •  孤街浪徒
    2020-12-03 03:12

    std::abort will terminate your application without calling any functions registered using "at_exit/at_quick_exit". On the other hand, std::quick_exit will, as you pointed out, call the functions registered using std::at_quick_exit.

    std::abort typically aborts your application, this should be called when some abnormal situation happens and your application has to be closed without doing any cleanups. From the std::abort documentation:

    Causes abnormal program termination unless SIGABRT is being caught by a signal handler passed to signal and the handler does not return.

    When you want to perform some cleanups, std::quick_exit will be more appropiate. This last function also lets you stop your application gracefully, since it ends up calling std::_Exit instead of signaling a signal like std::abort(which signals SIGABRT, making the application stop abnormally).

    std::exit allows you to exit your application gracefully, while still cleaning up automatic, thread local and static variables. std::quick_exit does not. That's why there is a "quick_" in its name, it's faster since it skips the clean up phase.

    Therefore, there is an actual semantic difference between both functions. One stops the application abnormally, and the other performs a gracefull exit, allowing you to do some clean ups.

提交回复
热议问题