Files
2026-10-05 11:27:21 +02:00

38 lines
906 B
C++

#include "TimerSet.h"
#include "Timer.h"
#include <iostream>
TimerSet::TimerSet()
: previousTime(time(&previousTime)), pollingThread([this]() { poll10Hz(); }) // Exercise 5.1 start thread
{
pollingThread.detach(); // let it die on its own, without join
}
// Exercise 5.1 new function
void TimerSet::poll10Hz() {
std::chrono::duration<int, std::milli> pause(100);
while (true) {
std::this_thread::sleep_for(pause);
pollAllTimers();
}
}
void TimerSet::addATimer(Timer *t) { timers.push_back(t); }
void TimerSet::pollAllTimers() {
std::time_t timeNow;
if (time(&timeNow) > previousTime) {
previousTime = timeNow;
for (auto it = timers.begin(); it != timers.end(); it++) {
(*it)->tick();
}
}
}
TimerSet &TimerSet::Instance() {
static TimerSet pInstance;
return pInstance;
}