From 9e62ab7aafbf881fd571c2d53b548cb695458f18 Mon Sep 17 00:00:00 2001 From: Vladislav Vaintroub Date: Tue, 25 Apr 2023 14:24:39 +0200 Subject: [PATCH] MDEV-31095 tpool - do not create new worker, if thread creation is pending. Use an std::atomic_flag to track thread creation in progress. This is mainly a cleanup, the effect of this change was not measureable in my tests. --- tpool/tpool_generic.cc | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tpool/tpool_generic.cc b/tpool/tpool_generic.cc index c2d3f172817..fd97b4464af 100644 --- a/tpool/tpool_generic.cc +++ b/tpool/tpool_generic.cc @@ -278,6 +278,7 @@ class thread_pool_generic : public thread_pool /* maintenance related statistics (see maintenance()) */ size_t m_last_thread_count; unsigned long long m_last_activity; + std::atomic_flag m_thread_creation_pending= ATOMIC_FLAG_INIT; void worker_main(worker_data *thread_data); void worker_end(worker_data* thread_data); @@ -575,6 +576,7 @@ void thread_pool_generic::worker_main(worker_data *thread_var) m_worker_init_callback(); tls_worker_data = thread_var; + m_thread_creation_pending.clear(); while (get_task(thread_var, &task) && task) { @@ -720,11 +722,13 @@ static int throttling_interval_ms(size_t n_threads,size_t concurrency) /* Create a new worker.*/ bool thread_pool_generic::add_thread() { + if (m_thread_creation_pending.test_and_set()) + return false; + size_t n_threads = thread_count(); if (n_threads >= m_max_threads) return false; - if (n_threads >= m_min_threads) { auto now = std::chrono::system_clock::now();