我创建了一个Service,但是自己关闭的时候需要发消息给Service通知自己关闭,如何能在用户使用Ctl+c关闭程序的时候退出,Service.
去监听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;
}
也许对你有帮助:https://blog.csdn.net/it_xiangqiang/category_10581430.html