c++中对应java ShutdownHook的退出处理器

时间:2023-03-10 07:51:36
c++中对应java ShutdownHook的退出处理器

最近学习cpp(至于为什么,可参考http://www.cnblogs.com/zhjh256/p/6321972.html),c++标准库第二版5.8.2节的时候,发现c++有一个对应java ShutdownHook退出处理器的机制,atexit。可以在正常退出程序的时候调用注册的函数,如下所示:

namespace ns_demo {
void printN();
void terminateTest();
}
#include "stdafx.h"
#include <iostream>
#include "ns.h"
using namespace std;
void ns_demo::printN() {
cout << << endl;
} void ns_demo::terminateTest()
{
cout<<"程序正在结束..."<<endl;
system("pause");
}
#include "ns.h"
using namespace std;
#define GUID_LEN 64
int _tmain(int argc, _TCHAR* argv[])
{
atexit(ns_demo::terminateTest);
atexit(ns_demo::terminateTest);
}

相同的处理器注册了两次,就会退出的时候调用两遍,不做重复检查,如下所示:

程序正在结束...
请按任意键继续. . .
程序正在结束...
请按任意键继续. . .