From 059b2b1539b070712e4c85eb5958f051df4625dd Mon Sep 17 00:00:00 2001 From: lisyarus Date: Thu, 4 Sep 2025 00:55:04 +0300 Subject: [PATCH] Use null util::function instead of a stop_execution exception when stopping threadpool threads to prevent exceptions from triggering when debugging --- libs/async/include/psemek/async/executor.hpp | 1 - libs/async/include/psemek/async/threadpool.hpp | 1 - libs/async/source/threadpool.cpp | 18 +++++------------- 3 files changed, 5 insertions(+), 15 deletions(-) diff --git a/libs/async/include/psemek/async/executor.hpp b/libs/async/include/psemek/async/executor.hpp index 74fab0a5..56e3c004 100644 --- a/libs/async/include/psemek/async/executor.hpp +++ b/libs/async/include/psemek/async/executor.hpp @@ -4,7 +4,6 @@ #include #include -#include namespace psemek::async { diff --git a/libs/async/include/psemek/async/threadpool.hpp b/libs/async/include/psemek/async/threadpool.hpp index 381d3d1a..99b1501b 100644 --- a/libs/async/include/psemek/async/threadpool.hpp +++ b/libs/async/include/psemek/async/threadpool.hpp @@ -4,7 +4,6 @@ #include #include -#include #include #include #include diff --git a/libs/async/source/threadpool.cpp b/libs/async/source/threadpool.cpp index c5b5d417..c4501f7f 100644 --- a/libs/async/source/threadpool.cpp +++ b/libs/async/source/threadpool.cpp @@ -9,13 +9,6 @@ namespace psemek::async { - namespace - { - - struct stop_execution{}; - - } - threadpool::threadpool(std::string const & name, std::size_t thread_count) : working_count_{0} { @@ -25,10 +18,13 @@ namespace psemek::async threads_.emplace_back([this, tname = std::move(tname)]() mutable { log::thread_registrator reg(std::move(tname)); - for (bool running = true; running;) + while (true) { auto task = task_queue_.pop(); + if (!task) + break; + { std::lock_guard lock{working_count_mutex_}; ++working_count_; @@ -38,10 +34,6 @@ namespace psemek::async { task(); } - catch (stop_execution const &) - { - running = false; - } catch (util::exception const & e) { log::error() << "Unhandled exception in threadpool executor: " << e; @@ -86,7 +78,7 @@ namespace psemek::async for (auto const & thread: threads_) { unused(thread); - task_queue_.push([]{ throw stop_execution{}; }); + task_queue_.push(nullptr); } threads_.clear(); }