Unverified Commit 1835e0cd authored by Gabi Melman's avatar Gabi Melman Committed by GitHub

Merge pull request #2206 from SBAM/v1.x

Add custom on_stop callback to thread-pool, executed by each thread before joining them.
parents b803e768 967b373d
...@@ -73,16 +73,20 @@ inline std::shared_ptr<spdlog::logger> create_async_nb(std::string logger_name, ...@@ -73,16 +73,20 @@ inline std::shared_ptr<spdlog::logger> create_async_nb(std::string logger_name,
} }
// set global thread pool. // set global thread pool.
inline void init_thread_pool(size_t q_size, size_t thread_count, std::function<void()> on_thread_start) inline void init_thread_pool(size_t q_size, size_t thread_count, std::function<void()> on_thread_start, std::function<void()> on_thread_stop)
{ {
auto tp = std::make_shared<details::thread_pool>(q_size, thread_count, on_thread_start); auto tp = std::make_shared<details::thread_pool>(q_size, thread_count, on_thread_start, on_thread_stop);
details::registry::instance().set_tp(std::move(tp)); details::registry::instance().set_tp(std::move(tp));
} }
// set global thread pool. inline void init_thread_pool(size_t q_size, size_t thread_count, std::function<void()> on_thread_start)
{
init_thread_pool(q_size, thread_count, on_thread_start, [] {});
}
inline void init_thread_pool(size_t q_size, size_t thread_count) inline void init_thread_pool(size_t q_size, size_t thread_count)
{ {
init_thread_pool(q_size, thread_count, [] {}); init_thread_pool(q_size, thread_count, [] {}, [] {});
} }
// get the global thread pool. // get the global thread pool.
......
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
namespace spdlog { namespace spdlog {
namespace details { namespace details {
SPDLOG_INLINE thread_pool::thread_pool(size_t q_max_items, size_t threads_n, std::function<void()> on_thread_start) SPDLOG_INLINE thread_pool::thread_pool(size_t q_max_items, size_t threads_n, std::function<void()> on_thread_start, std::function<void()> on_thread_stop)
: q_(q_max_items) : q_(q_max_items)
{ {
if (threads_n == 0 || threads_n > 1000) 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 ...@@ -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++) 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(); on_thread_start();
this->thread_pool::worker_loop_(); 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<void()> 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) 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 // message all threads to terminate gracefully join them
......
...@@ -84,6 +84,7 @@ public: ...@@ -84,6 +84,7 @@ public:
using item_type = async_msg; using item_type = async_msg;
using q_type = details::mpmc_blocking_queue<item_type>; using q_type = details::mpmc_blocking_queue<item_type>;
thread_pool(size_t q_max_items, size_t threads_n, std::function<void()> on_thread_start, std::function<void()> on_thread_stop);
thread_pool(size_t q_max_items, size_t threads_n, std::function<void()> on_thread_start); thread_pool(size_t q_max_items, size_t threads_n, std::function<void()> on_thread_start);
thread_pool(size_t q_max_items, size_t threads_n); thread_pool(size_t q_max_items, size_t threads_n);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment