C++处理Ctrl+C中断信号

来源:互联网 发布:特效视频软件app 编辑:程序博客网 时间:2024/05/13 00:04
#include <iostream>#include <csignal>using namespace std;static volatile int keepRunning = 1; void sig_handler( int sig ){    if ( sig == SIGINT)    {        keepRunning = 0;    }}int main( ){    // 不要忘记在主线程中注册这个信号!!!    signal( SIGINT, sig_handler );    while( keepRunning )    {      cout << "Running" << endl;    }    cout << "Terminated by Ctrl+C signal." << endl;    cout << "Finishes data saving or some other work, and then exits."    return 0;}
0 0