From 06fb5c7c6955fc323ba55203ca6afda26112b6d2 Mon Sep 17 00:00:00 2001 From: gabime Date: Wed, 19 Jun 2019 18:30:50 +0300 Subject: [PATCH] Close issue #1113 --- include/spdlog/details/thread_pool-inl.h | 15 +++++++++++++-- include/spdlog/details/thread_pool.h | 3 +++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/include/spdlog/details/thread_pool-inl.h b/include/spdlog/details/thread_pool-inl.h index 46c9cc44..bfb4908d 100644 --- a/include/spdlog/details/thread_pool-inl.h +++ b/include/spdlog/details/thread_pool-inl.h @@ -11,7 +11,8 @@ namespace spdlog { namespace details { -SPDLOG_INLINE thread_pool::thread_pool(size_t q_max_items, size_t threads_n) + +SPDLOG_INLINE thread_pool::thread_pool(size_t q_max_items, size_t threads_n, std::function on_thread_start) : q_(q_max_items) { if (threads_n == 0 || threads_n > 1000) @@ -21,10 +22,20 @@ SPDLOG_INLINE thread_pool::thread_pool(size_t q_max_items, size_t threads_n) } for (size_t i = 0; i < threads_n; i++) { - threads_.emplace_back(&thread_pool::worker_loop_, this); + threads_.emplace_back([this, on_thread_start] + { + on_thread_start(); + this->thread_pool::worker_loop_(); + }); } } +SPDLOG_INLINE thread_pool::thread_pool(size_t q_max_items, size_t threads_n): + thread_pool(q_max_items, threads_n, []{}) +{ + +} + // message all threads to terminate gracefully join them SPDLOG_INLINE thread_pool::~thread_pool() { diff --git a/include/spdlog/details/thread_pool.h b/include/spdlog/details/thread_pool.h index d8e821aa..f7904cf1 100644 --- a/include/spdlog/details/thread_pool.h +++ b/include/spdlog/details/thread_pool.h @@ -11,6 +11,7 @@ #include #include #include +#include namespace spdlog { class async_logger; @@ -118,7 +119,9 @@ 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); thread_pool(size_t q_max_items, size_t threads_n); + // message all threads to terminate gracefully join them ~thread_pool();