feat(cmat): Completed Ex51
This commit is contained in:
+36
-24
@@ -1,24 +1,36 @@
|
||||
#include "TimerSet.h"
|
||||
#include "Timer.h"
|
||||
#include <iostream>
|
||||
|
||||
TimerSet::TimerSet() : previousTime(time(NULL)) {}
|
||||
|
||||
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() {
|
||||
// Meyer's Singleton implementation
|
||||
static TimerSet pInstance;
|
||||
return pInstance;
|
||||
}
|
||||
#include "TimerSet.h"
|
||||
#include "Timer.h"
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
|
||||
TimerSet::TimerSet() : previousTime(time(NULL)), pollingThread([this]{poll10Hz();}) {
|
||||
pollingThread.detach();
|
||||
}
|
||||
|
||||
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() {
|
||||
// Meyer's Singleton implementation
|
||||
static TimerSet pInstance;
|
||||
return pInstance;
|
||||
}
|
||||
|
||||
void TimerSet::poll10Hz() {
|
||||
using namespace std::chrono_literals;
|
||||
while (true) {
|
||||
auto const pause = 100ms;
|
||||
pollAllTimers();
|
||||
std::this_thread::sleep_for(pause);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user