C++ Boost ASIO simple periodic timer?

前端 未结 4 1300
逝去的感伤
逝去的感伤 2020-12-03 04:40

I want a very simple periodic timer to call my code every 50ms. I could make a thread that sleeps for 50ms all the time (but that\'s a pain)... I could start looking into Li

相关标签:
4条回答
  • 2020-12-03 05:23

    As I had some issues with prior answers, here is my example:

    #include <boost/asio.hpp>
    #include <boost/bind.hpp>
    #include <boost/date_time/posix_time/posix_time.hpp>
    #include <iostream>
    
    void print(const boost::system::error_code&, boost::asio::deadline_timer* t,int* count)
    {
        if (*count < 5)
        {
            std::cout << *count << std::endl;
            ++(*count);
            t->expires_from_now(boost::posix_time::seconds(1));
            t->async_wait(boost::bind(print, boost::asio::placeholders::error, t, count));
        }
    }
    
    int main()
    { 
        boost::asio::io_service io;
        int count = 0;
        boost::asio::deadline_timer t(io, boost::posix_time::seconds(1));
    
        t.async_wait(boost::bind(print, boost::asio::placeholders::error, &t, &count));
    
        io.run();
        std::cout << "Final count is " << count << std::endl;
    
        return 0;
    
    }
    

    it did what it supposed to do: counting to five. May it help someone.

    0 讨论(0)
  • 2020-12-03 05:28

    To further expand on this simple example. It will block the execution as was said in the comments, so if you want more io_services running, you should run them in a thread like so...

    boost::asio::io_service io_service;
    boost::asio::io_service service2;
    timer.async_wait(tick);
    boost::thread_group threads;
    threads.create_thread(boost::bind(&boost::asio::io_service::run, &io_service));
    service2.run();
    threads.join_all();
    
    0 讨论(0)
  • 2020-12-03 05:38

    A very simple, but fully functional example:

    #include <iostream>
    #include <boost/asio.hpp>
    
    boost::asio::io_service io_service;
    boost::posix_time::seconds interval(1);  // 1 second
    boost::asio::deadline_timer timer(io_service, interval);
    
    void tick(const boost::system::error_code& /*e*/) {
    
        std::cout << "tick" << std::endl;
    
        // Reschedule the timer for 1 second in the future:
        timer.expires_at(timer.expires_at() + interval);
        // Posts the timer event
        timer.async_wait(tick);
    }
    
    int main(void) {
    
        // Schedule the timer for the first time:
        timer.async_wait(tick);
        // Enter IO loop. The timer will fire for the first time 1 second from now:
        io_service.run();
        return 0;
    }
    

    Notice that it is very important to call expires_at() to set a new expiration time, otherwise the timer will fire immediately because it's current due time already expired.

    0 讨论(0)
  • 2020-12-03 05:44

    The second example on Boosts Asio tutorials explains it.
    You can find it here.

    After that, check the 3rd example to see how you can call it again with a periodic time intervall

    0 讨论(0)
提交回复
热议问题