diff --git a/lib/include/srslte/common/metrics_hub.h b/lib/include/srslte/common/metrics_hub.h index b35a853a4..ca6ff2426 100644 --- a/lib/include/srslte/common/metrics_hub.h +++ b/lib/include/srslte/common/metrics_hub.h @@ -31,6 +31,8 @@ #include "srslte/common/threads.h" #include "srslte/srslte.h" #include +#include +#include #include namespace srslte { @@ -54,7 +56,7 @@ template class metrics_hub : public periodic_thread { public: - metrics_hub() : m(nullptr), sleep_start(std::chrono::steady_clock::now()), periodic_thread("METRICS_HUB") {} + metrics_hub() : sleep_start(std::chrono::steady_clock::now()), periodic_thread("METRICS_HUB") {} bool init(metrics_interface* m_, float report_period_secs_ = 1.0) { m = m_; @@ -73,11 +75,17 @@ public: wait_thread_finish(); } - void add_listener(metrics_listener* listener) { listeners.push_back(listener); } + void add_listener(metrics_listener* listener) + { + std::unique_lock lock(mutex); + listeners.push_back(listener); + } private: void run_period() { + std::unique_lock lock(mutex); + // get current time and check how long we slept auto period_usec = std::chrono::duration_cast(std::chrono::steady_clock::now() - sleep_start); @@ -92,9 +100,10 @@ private: // store start of sleep period sleep_start = std::chrono::steady_clock::now(); } - metrics_interface* m; + metrics_interface* m = nullptr; std::vector*> listeners; std::chrono::steady_clock::time_point sleep_start; + std::mutex mutex; }; } // namespace srslte