diff --git a/src/Common/ThreadPool.cpp b/src/Common/ThreadPool.cpp index a92b2e3a04f1..718a1a8fa501 100644 --- a/src/Common/ThreadPool.cpp +++ b/src/Common/ThreadPool.cpp @@ -232,6 +232,7 @@ template template ReturnType ThreadPoolImpl::scheduleImpl(Job job, Priority priority, std::optional wait_microseconds, bool propagate_opentelemetry_tracing_context) { + LOG_DEBUG(&Poco::Logger::get("ThreadPoolImpl"),"size of lambda: {}", sizeof(job)); auto on_error = [&](const std::string & reason) { if constexpr (std::is_same_v) @@ -333,14 +334,18 @@ void ThreadPoolImpl::startThreads(bool async, std::unique_lock::iterator> promise_thread_it; - std::future::iterator> future_thread_id = promise_thread_it.get_future(); + std::shared_future::iterator> future_thread_id = promise_thread_it.get_future().share(); lock.unlock(); Stopwatch watch; + /// we use future here for 2 reasons: + /// 1) just passing a ref to list iterator after adding the thread to the list + /// 2) hold the thread work until the thread is added to the list, otherwise + /// is can get the lock faster and then can wait for a cond_variable forever auto thread = Thread([this, ft = std::move(future_thread_id)] mutable - { + { auto thread_it = ft.get(); worker(thread_it); });