Search code examples
c++boost

boost: How to sleep current thread until given ptime?


Not sure if I'm missing something, but how can I pass a boost::posix_time::ptime object to boost::this_thread::sleep_until()? More specifically: How can I convert from boost::posix_time::ptime to boost::chrono::time_point?

void do_magic( const boost::posix_time::ptime& wakeup_time )
{
    boost::this_thread::sleep_until( wakeup_time ); // not working

    do_more_magic();
}

I'm using boost version 1.62, if that is of any relevance.


Solution

  • In the meantime, this problem has been solved with the deadline_timer. There is a solution in boost::asio:

    // Construct a timer without setting an expiry time.
    boost::asio::deadline_timer timer(my_context);
    
    // Set an expiry time relative to now.
    timer.expires_from_now(boost::posix_time::seconds(5));
    
    // Wait for the timer to expire.
    timer.wait();