Commit fdef7ff33f675d12b132bf9fcfaee1a4ea81a13f

Authored by Wiebe Cazemier
1 parent b18af18a

Mutex callbacks vector in timer

This is theoretical, because I never called addCallback() after having
started the timer.
Showing 2 changed files with 6 additions and 0 deletions
timer.cpp
@@ -82,6 +82,8 @@ void Timer::addCallback(std::function<void ()> f, uint64_t interval_ms, const st @@ -82,6 +82,8 @@ void Timer::addCallback(std::function<void ()> f, uint64_t interval_ms, const st
82 { 82 {
83 logger->logf(LOG_DEBUG, "Adding event '%s' to the timer with an interval of %ld ms.", name.c_str(), interval_ms); 83 logger->logf(LOG_DEBUG, "Adding event '%s' to the timer with an interval of %ld ms.", name.c_str(), interval_ms);
84 84
  85 + std::lock_guard<std::mutex> locker(this->callbacksMutex);
  86 +
85 CallbackEntry c; 87 CallbackEntry c;
86 c.f = f; 88 c.f = f;
87 c.interval = interval_ms; 89 c.interval = interval_ms;
@@ -137,6 +139,9 @@ void Timer::process() @@ -137,6 +139,9 @@ void Timer::process()
137 } 139 }
138 140
139 logger->logf(LOG_DEBUG, "Calling timed event '%s'.", callbacks.front().name.c_str()); 141 logger->logf(LOG_DEBUG, "Calling timed event '%s'.", callbacks.front().name.c_str());
  142 +
  143 + std::lock_guard<std::mutex> locker(this->callbacksMutex);
  144 +
140 CallbackEntry &c = callbacks.front(); 145 CallbackEntry &c = callbacks.front();
141 c.updateExectedAt(); 146 c.updateExectedAt();
142 c.f(); 147 c.f();
@@ -50,6 +50,7 @@ class Timer @@ -50,6 +50,7 @@ class Timer
50 int running = false; 50 int running = false;
51 Logger *logger = Logger::getInstance(); 51 Logger *logger = Logger::getInstance();
52 std::vector<CallbackEntry> callbacks; 52 std::vector<CallbackEntry> callbacks;
  53 + std::mutex callbacksMutex;
53 54
54 void sortAndSetSleeptimeTillNext(); 55 void sortAndSetSleeptimeTillNext();
55 void process(); 56 void process();