1. 程式人生 > >C++處理Ctrl+C中斷訊號

C++處理Ctrl+C中斷訊號

#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; }