From 6636ff05e61eece1cb8ef44ecdf70dab6d4db950 Mon Sep 17 00:00:00 2001 From: Philippe Serreault Date: Wed, 8 Dec 2021 15:10:21 +0100 Subject: [PATCH] Allow custom callback to be executed by thread-pool's threads before joining them. This is similar to a change that was made a while ago ( https://github.com/gabime/spdlog/pull/208 ). --- include/spdlog/details/thread_pool-inl.h | 11 ++++++++--- include/spdlog/details/thread_pool.h | 1 + 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/include/spdlog/details/thread_pool-inl.h b/include/spdlog/details/thread_pool-inl.h index deee3766..c99af52d 100644 --- a/include/spdlog/details/thread_pool-inl.h +++ b/include/spdlog/details/thread_pool-inl.h @@ -13,7 +13,7 @@ namespace spdlog { namespace details { -SPDLOG_INLINE thread_pool::thread_pool(size_t q_max_items, size_t threads_n, std::function on_thread_start) +SPDLOG_INLINE thread_pool::thread_pool(size_t q_max_items, size_t threads_n, std::function on_thread_start, std::function on_thread_stop) : q_(q_max_items) { if (threads_n == 0 || threads_n > 1000) @@ -23,15 +23,20 @@ SPDLOG_INLINE thread_pool::thread_pool(size_t q_max_items, size_t threads_n, std } for (size_t i = 0; i < threads_n; i++) { - threads_.emplace_back([this, on_thread_start] { + threads_.emplace_back([this, on_thread_start, on_thread_stop] { on_thread_start(); this->thread_pool::worker_loop_(); + on_thread_stop(); }); } } +SPDLOG_INLINE thread_pool::thread_pool(size_t q_max_items, size_t threads_n, std::function on_thread_start) + : thread_pool(q_max_items, threads_n, on_thread_start, [] {}) +{} + SPDLOG_INLINE thread_pool::thread_pool(size_t q_max_items, size_t threads_n) - : thread_pool(q_max_items, threads_n, [] {}) + : thread_pool(q_max_items, threads_n, [] {}, [] {}) {} // message all threads to terminate gracefully join them diff --git a/include/spdlog/details/thread_pool.h b/include/spdlog/details/thread_pool.h index 86187e40..0687c650 100644 --- a/include/spdlog/details/thread_pool.h +++ b/include/spdlog/details/thread_pool.h @@ -84,6 +84,7 @@ public: using item_type = async_msg; using q_type = details::mpmc_blocking_queue; + thread_pool(size_t q_max_items, size_t threads_n, std::function on_thread_start, std::function on_thread_stop); thread_pool(size_t q_max_items, size_t threads_n, std::function on_thread_start); thread_pool(size_t q_max_items, size_t threads_n);