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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jjjwwwjjjwww/article/details/54381392
#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;
}
展开阅读全文

没有更多推荐了,返回首页