diff --git a/timer.cpp b/timer.cpp index a19351a..a6815c6 100644 --- a/timer.cpp +++ b/timer.cpp @@ -82,6 +82,8 @@ void Timer::addCallback(std::function f, uint64_t interval_ms, const st { logger->logf(LOG_DEBUG, "Adding event '%s' to the timer with an interval of %ld ms.", name.c_str(), interval_ms); + std::lock_guard locker(this->callbacksMutex); + CallbackEntry c; c.f = f; c.interval = interval_ms; @@ -137,6 +139,9 @@ void Timer::process() } logger->logf(LOG_DEBUG, "Calling timed event '%s'.", callbacks.front().name.c_str()); + + std::lock_guard locker(this->callbacksMutex); + CallbackEntry &c = callbacks.front(); c.updateExectedAt(); c.f(); diff --git a/timer.h b/timer.h index 1567d58..6aae4c2 100644 --- a/timer.h +++ b/timer.h @@ -50,6 +50,7 @@ class Timer int running = false; Logger *logger = Logger::getInstance(); std::vector callbacks; + std::mutex callbacksMutex; void sortAndSetSleeptimeTillNext(); void process();