Jonathan Boccara over at Fluent{C++} made a post a while ago titled A Simple Timer in C++. I felt things could be done… different so I decided to write my own version of the timer code.
First, I felt there’s no need to actually instantiate
timer objects; a simple function call to
set_timeout or
set_interval from
namespace timer should be sufficient.
Second, I didn’t like the way cancellation was done. Single
stop call interrupted all intervals and timeouts. How about a cancelation event per
set_timeout or
set_intervalcall?
Finally, I wanted the
set_timeout and
set_interval functions to accept any callable with any number of arguments.
That’s exactly how I designed my interface.
Usage example:
waiting 5s…
Program output.
timeout
interval
interval
interval
interval
waiting 5s…
interval
interval
interval
interval
interval
Program ended with exit code: 1
timer.h:
Updated event.h:
One Reply to “Simple timer”