From 9fde27d214a50176c27e7fa62ff8afa0f777d089 Mon Sep 17 00:00:00 2001 From: Andrei Lobov Date: Tue, 21 Nov 2023 19:56:54 +0200 Subject: [PATCH 1/3] Start function --- core/utils/async_utils.cpp | 13 +++++++++---- core/utils/async_utils.hpp | 3 ++- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/core/utils/async_utils.cpp b/core/utils/async_utils.cpp index 60086a44f..912ab82e7 100644 --- a/core/utils/async_utils.cpp +++ b/core/utils/async_utils.cpp @@ -55,13 +55,18 @@ void busywait_mutex::unlock() noexcept { } template -ThreadPool::ThreadPool(size_t threads, basic_string_view name) - : name_{name} { +ThreadPool::ThreadPool(size_t threads, basic_string_view name) { + start(threads, name); +} + +template +void ThreadPool::start(size_t threads, basic_string_view name) { threads_.reserve(threads); for (size_t i = 0; i != threads; ++i) { threads_.emplace_back([&] { - if (!name_.empty()) { - set_thread_name(name_.c_str()); + if (!name.empty()) { + IRS_ASSERT(std::char_traits::length(name.data()) == name.size()); + set_thread_name(name.data()); } Work(); }); diff --git a/core/utils/async_utils.hpp b/core/utils/async_utils.hpp index 2d80a66d3..2e356ccb0 100644 --- a/core/utils/async_utils.hpp +++ b/core/utils/async_utils.hpp @@ -57,9 +57,11 @@ class ThreadPool { using Clock = std::chrono::steady_clock; using Func = fu2::unique_function; + ThreadPool() = default; explicit ThreadPool(size_t threads, basic_string_view name = {}); ~ThreadPool() { stop(true); } + void start(size_t threads, basic_string_view name = {}); bool run(Func&& fn, Clock::duration delay = {}); void stop(bool skip_pending = false) noexcept; // always a blocking call size_t tasks_active() const { @@ -95,7 +97,6 @@ class ThreadPool { bool WasStop() const { return state_ % 2 != 0; } - basic_string name_; std::vector threads_; mutable std::mutex m_; std::condition_variable cv_; From 4e0903fce35e5b4303faf1169b16f0fc9c122546 Mon Sep 17 00:00:00 2001 From: Andrei Lobov Date: Tue, 21 Nov 2023 19:59:32 +0200 Subject: [PATCH 2/3] wip --- core/utils/async_utils.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/core/utils/async_utils.cpp b/core/utils/async_utils.cpp index 912ab82e7..d5b34d819 100644 --- a/core/utils/async_utils.cpp +++ b/core/utils/async_utils.cpp @@ -61,6 +61,7 @@ ThreadPool::ThreadPool(size_t threads, basic_string_view name) { template void ThreadPool::start(size_t threads, basic_string_view name) { + IRS_ASSERT(threads_.empty()); threads_.reserve(threads); for (size_t i = 0; i != threads; ++i) { threads_.emplace_back([&] { From e67eba9dbf1dd01b95b79a9a32886d83657c491d Mon Sep 17 00:00:00 2001 From: Andrei Lobov Date: Tue, 21 Nov 2023 23:08:39 +0200 Subject: [PATCH 3/3] fix --- core/utils/async_utils.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/utils/async_utils.cpp b/core/utils/async_utils.cpp index d5b34d819..9c1bed9b7 100644 --- a/core/utils/async_utils.cpp +++ b/core/utils/async_utils.cpp @@ -64,7 +64,7 @@ void ThreadPool::start(size_t threads, basic_string_view name) { IRS_ASSERT(threads_.empty()); threads_.reserve(threads); for (size_t i = 0; i != threads; ++i) { - threads_.emplace_back([&] { + threads_.emplace_back([this, name] { if (!name.empty()) { IRS_ASSERT(std::char_traits::length(name.data()) == name.size()); set_thread_name(name.data());