
是的,exit(0)是最佳解决方案。它将导致全局对象(以及函数中的静态对象)的析构函数运行,但不会导致堆栈分配或堆分配对象分析构造函数操作:

// At global scope
ClassWithDestruct globalObject;
void SomeFunction()
{
static ClassWithDestructor staticObject;
ClassWithDestructor stackObject;
ClassWithDestructor *heapObject = new ClassWithDestructor;
// On the following call to exit(), the destructors of 'globalObject' and
// 'staticObject' will run, but those of 'stackObject' and 'heapObject' will
// NOT run
exit(0);
}

关于它是否是线程安全的,这是一个很难回答的问题:您不应同时从多个线程调用exit,而应仅调用一次exit。如果任何析构函数由于退出而运行,或者在atexit运行时注册了任何函数,那么显然,如果这些函数处理其他线程可能使用的数据,则它们应该是线程安全的。

如果程序正常退出(例如,由于用户退出请求),则应调用exit或从main / WinMain返回,这等效于调用exit。如果您的程序异常退出(例如,由于访问冲突或断言失败),则应调用_exit或中止,它们将不会调用任何析构函数。
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/shoujiruanjian/article-318384-1.html
打不过
黄子韬作词作曲