From a58a5feee3bea0cec5293cb672023dcc5b55f8fd Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Wed, 14 Aug 2019 17:12:59 +0200 Subject: [PATCH] Introduce ThreadPool#GetPending() --- lib/base/threadpool.cpp | 2 +- lib/base/threadpool.hpp | 19 ++++++++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/base/threadpool.cpp b/lib/base/threadpool.cpp index d7819ef4a..26787ab52 100644 --- a/lib/base/threadpool.cpp +++ b/lib/base/threadpool.cpp @@ -6,7 +6,7 @@ using namespace icinga; ThreadPool::ThreadPool(size_t threads) - : m_Threads(threads) + : m_Threads(threads), m_Pending(0) { Start(); } diff --git a/lib/base/threadpool.hpp b/lib/base/threadpool.hpp index deaf0439c..af351cd7a 100644 --- a/lib/base/threadpool.hpp +++ b/lib/base/threadpool.hpp @@ -3,6 +3,7 @@ #ifndef THREADPOOL_H #define THREADPOOL_H +#include "base/atomic.hpp" #include "base/exception.hpp" #include "base/logger.hpp" #include @@ -14,6 +15,7 @@ #include #include #include +#include namespace icinga { @@ -52,7 +54,11 @@ public: boost::shared_lock lock (m_Mutex); if (m_Pool) { - boost::asio::post(*m_Pool, [callback]() { + m_Pending.fetch_add(1); + + boost::asio::post(*m_Pool, [this, callback]() { + m_Pending.fetch_sub(1); + try { callback(); } catch (const std::exception& ex) { @@ -70,10 +76,21 @@ public: } } + /** + * Returns the amount of queued tasks not started yet. + * + * @returns amount of queued tasks. + */ + inline uint_fast64_t GetPending() + { + return m_Pending.load(); + } + private: boost::shared_mutex m_Mutex; std::unique_ptr m_Pool; size_t m_Threads; + Atomic m_Pending; }; } -- 2.49.0