Luc Danton <lucdan...@free.fr> wrote:
for(;;) {
func();
std::chrono::this_thread::sleep_for(dur);
}
Note that that will not achieve the desired effect if func() takes
a significant amount of time to execute.
For example, if you want func() called each 5 seconds, and func()
itself takes 2 seconds to execute, it will get called each 7 seconds
instead.
Of course if you want func() be called each 5 seconds regardless of
how long func() itself takes to execute, that's a more complicated
problem. (And of course there's always the question of what should be
done if func() takes more than 5 seconds to execute. One solution to
that is that if that's so, then the calling mechanism simply waits for
it to terminate without calling it again, so it's up to it to take less
than the desired call interval. However, the problem itself is still
complicated.)
instead of duration, will handily solve the problem.
design decision to make.