Re: [asio-users] immediately stop io_service from executing furtherwork (but not with strands??)
Brought to you by:
chris_kohlhoff
From: Marat A. <ab...@ma...> - 2012-06-26 21:04:34
|
> Did you try my example? > > http://ideone.com/ND9tu > > I'm using it under Linux on boost 1.48 Is there any (logical) difference between our examples (except MSVC 2010 fails to compile your example)? The workaround you wrote about ( service_.post(strand.wrap(handler)) ) doesn't help at Windows (Boost 1.49, MSVC 2010, Windows 7 Pro x64). At Ubuntu 12.04 (g++ 4.6.3, Boost 1.49) io_service::post() helps (I mean my example). > ~~~~~~~~~~~~~~~~~~~ > #if defined(WIN32) > #include <tchar.h> > #endif > > #include <cstdlib> > #include <iostream> > #include <boost/thread.hpp> > #include <boost/asio.hpp> > #include <boost/bind.hpp> > #include <boost/date_time/posix_time/posix_time_types.hpp> > > namespace asio_io_service_stop_test { > > const long seconds_until_stop = 5; > const long seconds_for_handler = 2; > const long handler_count = 50; > > void cyclic_handler(int counter) > { > std::cout << "In cyclic_handler #" << counter << std::endl; > boost::this_thread::sleep(boost::posix_time::seconds(seconds_for_handler)); > } > > void run_test() > { > boost::asio::io_service io_service; > boost::asio::io_service::strand strand(io_service); > for (int i = 0; i != handler_count; ++i) > { > // io_service.post() works here the same way as strand.post() > io_service.post(strand.wrap(boost::bind(cyclic_handler, i))); > //strand.post(boost::bind(cyclic_handler, i))); > } > > std::cout << "Startig work thread\n"; > boost::thread work_thread( > boost::bind(&boost::asio::io_service::run, &io_service)); > > std::cout << "Waiting " << seconds_until_stop > << " second(s) until call asio::io_service::stop()\n"; > boost::this_thread::sleep(boost::posix_time::seconds(seconds_until_stop)); > > std::cout << "Stopping io_service\n"; > io_service.stop(); > > std::cout << "io_service is stopped. Joining with work_thread\n"; > work_thread.join(); > std::cout << "work_thread finished\n"; > } > > } // namespace asio_io_service_stop_test > > #if defined(WIN32) > int _tmain(int /*argc*/, _TCHAR* /*argv*/[]) > #else > int main(int /*argc*/, char* /*argv*/[]) > #endif > { > asio_io_service_stop_test::run_test(); > return EXIT_SUCCESS; > } > ~~~~~~~~~~~~~~~~~~~ > > > ------------------------------------------------------------------------------ > 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 |