How to cast a double into std::chrono::milliseconds
One nice trick is to multiply your values with chrono literals:
using namespace std::chrono_literals;
double time = 82.0;
auto t_82ms = time * 1ms;
std::this_thread::sleep_for(t_82ms);
It also works the other way around:
double time = t_82ms / 1s; // time == 0.082
m_timer.expires_after
will accept any duration which is convertible to boost::asio::steady_timer::duration
it doesn't need to be std::chrono::milliseconds
(and if you don't want to discard the fractional milliseconds from your duration you shouldn't be converting to std::chrono::milliseconds
).
You can convert your double
into a std::chrono::duration
as follows:
double milliseconds = 0.1;
std::chrono::duration<double, std::milli> chrono_milliseconds{ milliseconds };
chrono_milliseconds
can't however be passed automatically into expires_after
as there is no automatic conversion from floating point durations to integer ones. You can fix this with a std::chrono::duration_cast
:
m_timer.expires_after(
std::chrono::duration_cast<boost::asio::steady_timer::duration>(chrono_milliseconds));