Re: [asio-users] question about background processing
Brought to you by:
chris_kohlhoff
From: Cheng L. <rhy...@gm...> - 2007-02-27 08:54:42
|
Cheng LIAN wrote: > template<typename Handler> > void connect( const tcp::endpoint& ep, Handler handler ) { > timer_.expires_from_now( seconds( 5 ) ); > timer_.async_wait( > bind( &connector::handle_timer > , this > , placeholders::error ) ); > socket_.connect( ep, handler ); > } Oh, sorry! The connect method should start the timer thread: template<typename Handler> void connect( const tcp::endpoint& ep, Handler handler ) { start_timer_thread(); timer_.expires_from_now( seconds( 5 ) ); timer_.async_wait( bind( &connector::handle_timer , this , placeholders::error ) ); socket_.connect( ep, handler ); } The start_timer_thread() method is just the same as the start_work_thread() in detail/resolver_service.hpp: struct timer_io_service_runner { public: timer_io_service_runner( boost::asio::io_service& ios ) : io_service_( ios ) {} void operator()() { io_service_.run(); } private: boost::asio::io_service& io_service_; }; void start_timer_thread() { if( timer_thread_ == 0 ) { timer_thread_.reset( new boost::asio::detail::thread( timer_io_service_runner( *timer_io_service_ ) ) ); } } And the lazy initialization private thread starting strategy which is omitted in the given skeleton code should be the same as detail/resolver_service.hpp rather than the service demo. Apologize for my carelessness... |