📅  最后修改于: 2020-09-25 08:45:01             🧑  作者: Mango
当函数 quick_exit() 函数,将调用在at_quick_exit() 函数 。
extern int at_quick_exit (void (*func)(void));
此函数在
at_quick_exit() 函数返回:
#include
#include
using namespace std;
void bye()
{
cout << "Program Exiting via quick_exit()";
}
int main()
{
at_quick_exit(bye);
cout << "Inside Main" << endl;
quick_exit(0);
return 0;
}
运行该程序时,输出为:
Inside Main
Program Exiting via quick_exit()
可以注册多个函数以在快速退出时执行。
如果使用at_quick_exit()注册了多个功能,则将以相反的顺序执行它们,即,首先执行最后注册的函数 。相同函数可以多次注册。
可以使用at_quick_exit()注册的函数调用的数量取决于特定的库实现。但是,最小限制为32。
#include
#include
using namespace std;
void quick_exit1()
{
cout << "Exit Function 1" << endl;
}
void quick_exit2()
{
cout << "Exit Function 2" << endl;
}
void quick_exit3()
{
cout << "Exit Function 3" << endl;
}
int main()
{
int x1, x2, x3;
/* Executed at last*/
x1 = at_quick_exit(quick_exit1);
x2 = at_quick_exit(quick_exit2);
/* Executed at first */
x3 = at_quick_exit(quick_exit3);
if ((x1 != 0) or (x2 != 0) or (x3 != 0))
{
cout << "Registration Failed";
exit(1);
}
cout << "Registration successful" << endl;
quick_exit(0);
}
运行该程序时,输出为:
Registration successful
Exit Function 3
Exit Function 2
Exit Function 1
如果在终止时调用已注册的函数引发未处理的异常,则会调用Terminate() 函数 。
#include
#include
using namespace std;
void bye()
{
cout << "Generates Exception";
int a = 5, b = 0;
int x = a/b;
}
int main()
{
int x;
x = at_quick_exit(bye);
if (x != 0)
{
cout << "Registration Failed";
exit(1);
}
cout << "Registration successful" << endl;
quick_exit(0);
}
运行该程序时,输出为:
Registration successful
Generates Exception
[The program will terminate with error]