[Cppcms-users] Booster::aio's behavior
Brought to you by:
artyom-beilis
|
From: CN <cn...@fa...> - 2015-09-30 02:37:39
|
The following program does not terminate until 10 seconds has passed.
I want to terminate it immediately once I type "kill -SIGTERM". Is that
possible? How do I achieve that?
-------
class MyClass
{
private:
booster::aio::io_service &io_service;
booster::aio::deadline_timer timer;
public:
MyClass(booster::aio::io_service &io_service)
: io_service(io_service),timer(io_service){}
void start()
{
io_service.set_timer_event(booster::ptime::now(),boost::bind(&MyClass::work,this,_1));
io_service.run();
}
void work(booster::system::error_code const &e)
{
if(e)
return;
//do_my_job(); Do periodic job here.
timer.expires_from_now(booster::ptime::seconds(10);
timer.async_wait(boost::bind(&MyClass::work,this,_1));
}
void stop()
{
io_service.stop();
}
};
cppcms::service *service;
void signal_handler(int signal_number)
{
service->shutdown();
}
int main(int argc,char **argv)
{
//init_signal_trap(); Initialize signal trap here.
while(true){
service=new cppcms::service(argc,argv);
service->applications_pool().mount(cppcms::applications_factory<my_app>(),cppcms::mount_point(".*",1));
booster::shared_ptr<MyClass> mc(new
MyClass(service->get_io_service()));
booster::thread t(boost::bind(&MyClass::start,mc));
service->run();
//Type "kill -SIGTERM" in OS so that signal_handler()
gets called here.
mc->stop();
t.join();
//Program does not reach here until 10 seconds passes!
if(caught_signal > 0){
switch (caught_signal){
case 1: //SIGHUP
case 14:{ //SIGALRM
//Restart application.
caught_signal=0;
break;
}
default:{
//Terminate program.
//case 2: //SIGINT
//case 15: //SIGTERM
//caught_signal=0;
return 0;
}
} //switch
} //if
} //while
}
-------
Best Regards,
CN
--
http://www.fastmail.com - The way an email service should be
|