I have many instances of std::this_thread::sleep_for(remaining_loop_time_duration).
To speed up simulation, we now were asked to use a special custom clock reference (queried using GetSimTime), which runs faster than real-time by a factor > 1.0.
To avoid large restructuring of my code I simply want to use a custom clock like this:
struct speedy_clock {
using duration = std::chrono::microseconds;
using rep = duration::rep;
using period = duration::period;
using time_point = std::chrono::time_point<motion_clock>;
static const bool is_steady = false;
static time_point now() noexcept {
SimTime time;
GetSimTime(&time);
unsigned int seconds_since_epoch = time.Seconds;
unsigned int microseconds_within_second = time.USec;
std::chrono::seconds seconds_duration(seconds_since_epoch);
std::chrono::microseconds microseconds_duration(microseconds_within_second);
// Calculate the total duration
std::chrono::microseconds total_duration = seconds_duration + microseconds_duration;
return ans_motion_util::motion_clock::time_point(total_duration);
}
};
My goal is of course, that all instances of std::this_thread::sleep_for() sleep with regard to the accelerated time GetSimTime provided via my speedy_clock. Is this doable or do I need to write my own version of sleep for?
I'm afraid you'll have to write a custom sleep function. std::this_thread::sleep_for sleeps on a std::chrono::duration. And std::chrono::duration is completely ignorant of the existence of clocks:
template <class Rep, class Period = ratio<1>>
class duration
{
Rep count_;
public:
duration() = default;
// ...
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With