Need run a subroutine every 5 seconds, but measured at system clock marks. So, need start it every minute at 0, 5, 10, 15…. 45, 50, 55 second (exactly, with 0.1sec precistion).
Something like:
for(;;) {
do_sleep(); #time need to sleep to the next 5 second mark
run_this();
}
The run_this sub can be fast or slow (its runtime between 0.2 – 120 seconds). When it run more than 5 seconds – regardless its runtime, the next run must be at exact 5 second mark.
E.g. when the run_this
- ends at 11:11:12.3 need wait 2.7 second to the next run at 11:11:15
- when ends at 11:11:59.2 need wait only 0.8 seconds to the next at 11:12:00, and so on…
The question is: how to write the do_sleep?
For 0.1s precision you need to use Time::HiRes module. Something like: