c++中對應java ShutdownHook的退出處理器

來源:互聯網
上載者:User

標籤:system   調用   term   names   int   namespace   為什麼   tchar   shutdown   

最近學習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 << 1 << endl;}void ns_demo::terminateTest(){    cout<<"程式正在結束..."<<endl;    system("pause");}
#include "ns.h"using namespace std;#define GUID_LEN 64int _tmain(int argc, _TCHAR* argv[]){    atexit(ns_demo::terminateTest);    atexit(ns_demo::terminateTest);}

相同的處理器註冊了兩次,就會退出的時候調用兩遍,不做重複檢查,如下所示:

程式正在結束...請按任意鍵繼續. . .程式正在結束...請按任意鍵繼續. . .

 

c++中對應java ShutdownHook的退出處理器

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.