Thread: [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 |
From: CN <cn...@fa...> - 2015-09-30 02:48:16
|
Correct the typo in previous message. On Wed, Sep 30, 2015, at 10:37 AM, CN wrote: > cppcms::service *service; > void signal_handler(int signal_number) > { > service->shutdown(); > } > cppcms::service *service; int caught_signal=0; void signal_handler(int signal_number) { caught_signal=signal_number; service->shutdown(); } -- http://www.fastmail.com - The professional email service |
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 |
From: CN <cn...@fa...> - 2015-09-30 14:42:30
|
On Wed, Sep 30, 2015, at 06:32 PM, Artyom Beilis wrote: > 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. Now I know booster::aio::stop can only be called once. I will leave the call of stop() to cppcms::service. > 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. I have no problem using an independent instance of booster::aio::io_service in "MyClass". What confuses me is the statements: "Multiple threads may call io_service::run() to set up a pool of threads from which completion handlers may be invoked." and "Asynchronous completion handlers will only be called from threads that are currently calling io_service::run()." as mentioned here: http://www.boost.org/doc/libs/1_53_0/doc/html/boost_asio/overview/core/threads.html My understanding of the first statement is that boost::asio::io_service::run() can be called by multiple threads. If booster::aio::io_service is compatible with boost in this regard, then booster::aio::io_service::run() can also be called in main thread and the thread running the instance of "MyClass". Am I correct? > 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. Because MyClass::work() does time consuming job, I prefer to running it in another thread. >> **From:** CN <cn...@fa...> **To:** cppcms- >> us...@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(&MyClas- >> s::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>(),cp- >> pcms::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 [1]- The way an email service should be >> >> >> --------------------------------------------------------------------- >> --------- >> _______________________________________________ >> Cppcms-users mailing list Cpp...@li... >> https://lists.sourceforge.net/lists/listinfo/cppcms-users >> > ---------------------------------------------------------------------- > -------- > _________________________________________________ > Cppcms-users mailing list Cpp...@li... > https://lists.sourceforge.net/lists/listinfo/cppcms-users Links: 1. http://www.fastmail.com/ -- http://www.fastmail.com - Send your email first class |
From: Artyom B. <art...@ya...> - 2015-09-30 15:10:58
|
Ok... Boost.Asio's run() indeed can be called from different threads - a concept used by MW Windows IOCP. But it addsa significant restriction - ANY callback in the loop can be called by ANY thread making programming realnightmare. It is valid concept used with Windows IOCP but this programming model is not-so-recommended and hard to do in real world.In real world it is much better to keep the workers isolated in different threads. booster::aio::io_service::run() should be called from one thread only. Additionally it has different semantics - for example it does not exit as boost::run when no callbacks are given. I suggest to refer to booster::aio reference documentation. 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:42 PM Subject: Re: [Cppcms-users] Booster::aio's behavior On Wed, Sep 30, 2015, at 06:32 PM, Artyom Beilis wrote: 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. Now I know booster::aio::stop can only be called once. I will leave the call of stop() to cppcms::service. 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. I have no problem using an independent instance of booster::aio::io_service in "MyClass". What confuses me is the statements: "Multiple threads may call io_service::run() to set up a pool of threads from which completion handlers may be invoked." and "Asynchronous completion handlers will only be called from threads that are currently calling io_service::run()." as mentioned here: http://www.boost.org/doc/libs/1_53_0/doc/html/boost_asio/overview/core/threads.html My understanding of the first statement is that boost::asio::io_service::run() can be called by multiple threads. If booster::aio::io_service is compatible with boost in this regard, then booster::aio::io_service::run() can also be called in main thread and the thread running the instance of "MyClass". Am I correct? 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. Because MyClass::work() does time consuming job, I prefer to running it in another thread. 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 ------------------------------------------------------------------------------ _______________________________________________ Cppcms-users mailing list Cpp...@li... https://lists.sourceforge.net/lists/listinfo/cppcms-users -- http://www.fastmail.com - Send your email first class ------------------------------------------------------------------------------ _______________________________________________ Cppcms-users mailing list Cpp...@li... https://lists.sourceforge.net/lists/listinfo/cppcms-users |