Re: [Cppcms-users] Booster::aio's behavior
Brought to you by:
artyom-beilis
From: Artyom B. <art...@ya...> - 2015-09-30 10:32:30
|
Your problem is this io_service.set_timer_event(booster::ptime::now(),boost::bind(&MyClass::work,this,_1)); io_service.run(); You are using main io_service and calling it's "run()" member function also run is called by cppcms::service it is wrong and also you try to stop it. i.e. instead of booster::aio::io_service &io_service; just put your own booster::aio::io_service io_service and don't use main one (as you are running it in your own thread)and now you can start/stop it. Or use main event loop (with reference) than do not create a thread and do not start/stop io_service and do not create thread. Artyom Beilis -------------- CppCMS - C++ Web Framework: http://cppcms.com/ CppDB - C++ SQL Connectivity: http://cppcms.com/sql/cppdb/ From: CN <cn...@fa...> To: cpp...@li... Sent: Wednesday, September 30, 2015 5:37 AM Subject: [Cppcms-users] Booster::aio's behavior 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 ------------------------------------------------------------------------------ _______________________________________________ Cppcms-users mailing list Cpp...@li... https://lists.sourceforge.net/lists/listinfo/cppcms-users |