Chrono-Based Software Timer

Medium C++ Timing Asked at Garmin
Solve this problem

Implement a periodic software timer using std::chrono that reports how many periods have elapsed, without drifting when a tick is serviced late.

Constraints

  • Period is a std::chrono::milliseconds value.
  • `int poll(time_point now)` returns the number of whole periods since the last poll.
  • Must not drift: the next deadline advances by exact periods, not from `now`.
  • A long stall must report every missed period, not just one.
  • No sleeping, no threads.
Starter codeC++
1#include <chrono>2 3class PeriodicTimer {4public:5    using Clock = std::chrono::steady_clock;6    explicit PeriodicTimer(std::chrono::milliseconds period);7    int poll(Clock::time_point now);8};