From db04ccb1377b552186e0c3932140fd52f2e9e2d9 Mon Sep 17 00:00:00 2001 From: Nuno Lopes Date: Mon, 1 Mar 2021 14:36:22 +0000 Subject: [PATCH] scoped_timer: skip extra unneded heap allocation --- src/util/scoped_timer.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/util/scoped_timer.cpp b/src/util/scoped_timer.cpp index 93e170836..644c234c4 100644 --- a/src/util/scoped_timer.cpp +++ b/src/util/scoped_timer.cpp @@ -34,7 +34,7 @@ Revision History: #endif struct scoped_timer_state { - std::thread * m_thread { nullptr }; + std::thread m_thread; std::timed_mutex m_mutex; event_handler * eh; unsigned ms; @@ -82,9 +82,11 @@ private: public: imp(unsigned ms, event_handler * eh) { workers.lock(); + bool new_worker = false; if (available_workers.empty()) { workers.unlock(); s = new scoped_timer_state; + new_worker = true; ++num_workers; } else { @@ -96,8 +98,8 @@ public: s->eh = eh; s->m_mutex.lock(); s->work = 1; - if (!s->m_thread) { - s->m_thread = new std::thread(thread_func, s); + if (new_worker) { + s->m_thread = std::thread(thread_func, s); } else { s->cv.notify_one(); @@ -146,8 +148,7 @@ void scoped_timer::finalize() { for (auto w : cleanup_workers) { ++deleted; - w->m_thread->join(); - delete w->m_thread; + w->m_thread.join(); delete w; } }