vendredi 2 avril 2021

How to wait for either of two timers to finish (Boost Asio)

The code below prints to the console when both timer1 and timer2 have finished. How can I change it to print when either timer1 or timer2 finishes?

#include <iostream>
#include <boost/asio.hpp>
#include <boost/asio/spawn.hpp>

int main() {

  boost::asio::io_context io;

  boost::asio::deadline_timer timer1(io, boost::posix_time::seconds(5));
  boost::asio::deadline_timer timer2(io, boost::posix_time::seconds(1));

  boost::asio::spawn(io, [&](boost::asio::yield_context yield){
    timer1.async_wait(yield);
    timer2.async_wait(yield);
    std::cout << "Both timer1 and timer2 have finished" << std::endl;
  });

  io.run();

}

Aucun commentaire:

Enregistrer un commentaire