0

-> My application is time-sensitive and I was looking for a notify mechanism instead of sleep

 main()
 {
 boost::this_thread::sleep_for(boost::chrono::milliseconds(600));
    std::cout << "waking up\n";
}

-> Please let me know if there is a way to handle without using sleep.

imran
  • 29
  • 2
  • 4

1 Answers1

4

You should use boost::asio::deadline_timer: http://www.boost.org/doc/libs/1_55_0/doc/html/boost_asio/reference/deadline_timer.html

void handler(const boost::system::error_code& error)
{
  if (!error)
  {
    // Timer expired.
  }
}

...

// Construct a timer with an absolute expiry time.
boost::asio::deadline_timer timer(io_service,
    boost::posix_time::time_from_string("2005-12-07 23:59:59.000"));

// Start an asynchronous wait.
timer.async_wait(handler);
sehe
  • 374,641
  • 47
  • 450
  • 633
  • 2
    ..or in case you still want to use `chrono` instead of `posix_time` for specifying timestamps: `boost::asio::basic_waitable_timer` . – ComicSansMS Jul 14 '14 at 11:31
  • In current versions of boost `boost::asio::basic_waitable_timer` is typedefed to `boost::asio::steady_timer` making it look as clean as `deadline_timer` (which is nothing but a typedef of `boost::asio::basic_waitable_timer`). – DeVadder Jul 17 '14 at 07:17
  • @ComicSansMS,DeVadder Thanks for both comments! The things you learn on SO, still worth it :) – sehe Jul 17 '14 at 07:44