Re: [asio-users] immediately stop io_service from executingfurtherwork (but not with strands??)
Brought to you by:
chris_kohlhoff
From: Gruenke, M. <mgruenke@Tycoint.com> - 2012-06-26 17:07:46
|
I consider that a bug. I recommend filing it in the boost bugtracker. Matt -----Original Message----- From: Amir Taaki [mailto:zg...@ya...] Sent: Tuesday, June 26, 2012 12:43 PM To: asi...@li... Subject: Re: [asio-users] immediately stop io_service from executingfurtherwork (but not with strands??) I should note that the workaround I found was to use: service_.post(strand.wrap(handler)) Rather than: strand_.post(handler) ----- Original Message ----- From: Amir Taaki <zg...@ya...> To: "asi...@li..." <asi...@li...> Cc: Sent: Tuesday, June 26, 2012 6:40 PM Subject: Re: [asio-users] immediately stop io_service from executing furtherwork (but not with strands??) No, I attached some source code to demonstrate. Event takes 1 second to complete. If I submit 10 events to io_service, wait 5 seconds and call service_.stop() then 6 events complete (as expected). If I submit 10 events to io_service::strand, wait 5 seconds and call service_.stop() then the io_service continues on until *all* 10 events complete (unexpected). http://ideone.com/ND9tu ----- Original Message ----- From: "Gruenke, Matt" <mgruenke@Tycoint.com> To: asi...@li... Cc: Sent: Tuesday, June 26, 2012 6:10 PM Subject: Re: [asio-users] immediately stop io_service from executing furtherwork (but not with strands??) Are you saying that if I post one event to a strand, then call stop, and then post a second event (before the first has finished processing), that the second event will be processed before io_service::run() returns? If that's so, then I'd file a bug in the boost bug tracker. I just want to make sure that you're not expecting ioservice::stop() to suspend a handler mid-execution. That will not happen. ASIO only works on the granularity of entire callbacks. If you want to interrupt (and later resume) a single handler, you need to find a way to break it into multiple events, or to use something besides ASIO. Matt -----Original Message----- From: Amir Taaki [mailto:zg...@ya...] Sent: Monday, June 25, 2012 9:00 PM To: asi...@li... Subject: [asio-users] immediately stop io_service from executing furtherwork (but not with strands??) According to this link: http://www.boost.org/doc/libs/1_49_0/doc/html/boost_asio/reference/io_service.html In the section titled "Stopping the io_service from running out of work", it says that invocating stop() on the io_service will cause the run() function to return as soon as possible, abandoning unfinished operations without dispatching them. I created a simple program to test this and it works fine. After calling stop, all the waiting handlers are discarded and the io_service finishes immediately. See http://ideone.com/1dWDK However, if I then use a strand, it will not finish but continue on until *all* the pending work has been completed. This is really annoying because my use is serialising access to a shared resource. Each query takes a very long time, and there's many of them. This means that when the program wants to exit, it needs to wait for all these very long pending pieces of work to complete before the database can be shut down. Real code: https://gitorious.org/libbitcoin/libbitcoin/blobs/master/src/blockchain/bdb/bdb_blockchain.cpp Example testcase: http://ideone.com/ND9tu #include <iostream> #include <thread> #include <boost/asio.hpp> using boost::asio::io_service; class foo { public: foo(io_service& service) : strand_(service) {} void bar() { strand_.post( [this] { std::cout << __PRETTY_FUNCTION__ << std::endl; sleep(1); }); } private: io_service::strand strand_; }; int main() { io_service service; io_service::work* work = new io_service::work(service); std::thread t([&service] { service.run(); }); foo f(service); // Only 5 bar()s should get called, but instead all 10 are allowed to finish... for (size_t i = 0; i < 10; ++i) f.bar(); sleep(5); std::cout << "Closing" << std::endl; //delete work; //work = nullptr; service.stop(); t.join(); return 0; } ------------------------------------------------------------------------------ Live Security Virtual Conference Exclusive live event will cover all the ways today's security and threat landscape has changed and how IT managers can respond. Discussions will include endpoint security, mobile security and the latest in malware threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/ _______________________________________________ asio-users mailing list asi...@li... https://lists.sourceforge.net/lists/listinfo/asio-users _______________________________________________ Using Asio? List your project at http://think-async.com/Asio/WhoIsUsingAsio ------------------------------------------------------------------------------ Live Security Virtual Conference Exclusive live event will cover all the ways today's security and threat landscape has changed and how IT managers can respond. Discussions will include endpoint security, mobile security and the latest in malware threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/ _______________________________________________ asio-users mailing list asi...@li... https://lists.sourceforge.net/lists/listinfo/asio-users _______________________________________________ Using Asio? List your project at http://think-async.com/Asio/WhoIsUsingAsio ------------------------------------------------------------------------------ Live Security Virtual Conference Exclusive live event will cover all the ways today's security and threat landscape has changed and how IT managers can respond. Discussions will include endpoint security, mobile security and the latest in malware threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/ _______________________________________________ asio-users mailing list asi...@li... https://lists.sourceforge.net/lists/listinfo/asio-users _______________________________________________ Using Asio? List your project at http://think-async.com/Asio/WhoIsUsingAsio |