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 82 {
83 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 87 CallbackEntry c;
86 88 c.f = f;
87 89 c.interval = interval_ms;
... ... @@ -137,6 +139,9 @@ void Timer::process()
137 139 }
138 140  
139 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 145 CallbackEntry &c = callbacks.front();
141 146 c.updateExectedAt();
142 147 c.f();
... ...
... ... @@ -50,6 +50,7 @@ class Timer
50 50 int running = false;
51 51 Logger *logger = Logger::getInstance();
52 52 std::vector<CallbackEntry> callbacks;
  53 + std::mutex callbacksMutex;
53 54  
54 55 void sortAndSetSleeptimeTillNext();
55 56 void process();
... ...