c++ - boost asio timer : run 2 timers -


i try run asynchronous timer , synchronous timer :

here code :

boost::asio::io_service io;  boost::asio::steady_timer t1(io); boost::asio::steady_timer t2(io);  void callback(boost::system::error_code const&) {     std::cout << "foo" << std::endl;     t1.expires_from_now(boost::chrono::seconds(1));     t1.async_wait(&callback); }  int main(int argc, char **argv) {       t1.expires_from_now(boost::chrono::seconds(1));     t1.async_wait(&callback);      io.run();      t2.expires_from_now(boost::chrono::seconds(5));      t2.wait();      io.run();      std::cout << "finish" << std::endl;      return exit_success; } 

i foo printed 5 times, , finish printed. in code, foo printed every 1 second , finish never reached.

how achieve want ?

thanks

according the documentation of io_service::run:

the run() function blocks until work has finished , there no more handlers dispatched, or until io_service has been stopped.

since run blocks until there no more handlers dispatched, block until callback has finished. however, callback registers callback handler , run keep blocking until it's finished... ad infinitum

if want callback repeat 5 times, need not schedule new callback after fifth time.you can use simple counter , branch that.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -