Io_service: The run function will be automatically returned when there are no tasks. This is not convenient for wtl projects. I have mentioned using a loop to run the run function in my previous article, that is not elegant. Today, I found a new method on the Internet and finally found that the io_service: Work class can enable the io_service: Run function to still not return when there is no task until the work object is destroyed.
Boost: ASIO: io_service Ios; Boost: ASIO: io_service: Work (IOS); // use the work object IOS. run (); // even if there is no task currently, IOS. run () will not return immediately
Or, the following example is directly copied from my program. This program uses the wtl framework.
boost::asio::io_service m_ios;std::shared_ptr<boost::thread> m_ios_thread;std::shared_ptr<tm_client> m_client_ptr;std::shared_ptr<boost::asio::io_service::work> m_work; m_work.reset(new boost::asio::io_service::work(m_ios));m_ios_thread.reset(new boost::thread(boost::bind(&boost::asio::io_service::run, &m_ios))); // resolve the hostnameboost::asio::ip::tcp::resolver resolver(m_ios);boost::asio::ip::tcp::resolver::query q(hostname, boost::lexical_cast<std::string>(x::tl::PORT_TMSRV));boost::asio::ip::tcp::resolver::iterator res_begin = resolver.resolve(q), res_end; if (res_begin != res_end){ // constructor client connection object m_client_ptr.reset(new tm_client(m_ios, tm_window_notifier(*this))); m_client_ptr->connect(*res_begin);}
For more information, see stopping.
The io_service from running out of work
From: http://btblog.net /? P = 50