Boost timer.

Source: Internet
Author: User

    1. #include <iostream>
    2. #include <boost/asio.hpp>
    3. int main ()
    4. {
    5. Boost::asio::io_service io;
    6. Boost::asio::d eadline_timer timer (io, boost::p osix_time::seconds (3));
    7. Timer.wait ();
    8. Std::cout << "Hello, world!\n";
    9. return 0;
    10. }



One more asynchronous:

[CPP]View Plaincopy
  1. void print (const boost::system::error_code& /*e*/)
  2. {
  3. Std::cout << "Hello, world!\n";
  4. }
  5. int main ()
  6. {
  7. Boost::asio::io_service io;
  8. Boost::asio::d eadline_timer timer (io, boost::p osix_time::seconds (5));
  9. Timer.async_wait (&print);
  10. Io.run ();
  11. return 0;
  12. }



The basic usage was demonstrated last time, but it only happens once.

The question is how to define a recurring timer (it happens once in a certain time). , the following code is, the key is that the callback function changes the delay constant extension timer

[CPP]View Plaincopy
    1. void print (const boost::system::error_code& E,
    2. Boost::asio::d eadline_timer* t)
    3. {
    4. cout<<"DDD" <<endl;
    5. T->expires_at (T->expires_at () + boost::p osix_time::seconds (1));
    6. T->async_wait (Boost::bind (print,boost::asio::p laceholders::error,t));
    7. }
    8. void Test1 ()
    9. {
    10. Boost::asio::io_service io;
    11. Boost::asio::d eadline_timer t (io, boost::p osix_time::seconds (1));
    12. T.async_wait (Boost::bind (print,boost::asio::p laceholders::error,&t));
    13. Io.run ();
    14. }
    15. int _tmain (int argc, _tchar* argv[])
    16. {
    17. Test1 ();
    18. System ("pause");
    19. return 0;
    20. }

Boost timer.

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.