The io_service: Run function in ASIO exits when no task exists.

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.