Re: [Cppcms-users] Booster::aio's behavior
Brought to you by:
artyom-beilis
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 |