4

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.

walnut
  • 21,629
  • 4
  • 23
  • 59
Sparkofska
  • 1,280
  • 2
  • 11
  • 34
  • Is there a specific reason you are not using `std` instead of boost? If you are constraint in the C++ version, you should probably add the correct version tag. – walnut Dec 11 '19 at 07:32
  • @walnut all the code around is using `boost::posix_time`. I don't want to change the whole code base. Converting to `std` could be an option, though. – Sparkofska Dec 11 '19 at 07:34
  • If `std::this_thread::sleep_until` is fine as well, then I guess [this question](https://stackoverflow.com/questions/4910373/interoperability-between-boostdate-time-and-stdchrono) might be a (almost?) duplicate. It should also be adjustable to the boost variant, but maybe there is a simpler mechanism. – walnut Dec 11 '19 at 07:42

1 Answers1

1

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();
user23573
  • 2,479
  • 1
  • 17
  • 36