From 5f528c1a1bf03177eabc9172ec74aaa5d21b66c1 Mon Sep 17 00:00:00 2001 From: tqcq <99722391+tqcq@users.noreply.github.com> Date: Sun, 31 Mar 2024 20:52:59 +0800 Subject: [PATCH] fix thread pool not start --- src/sled/system/thread_pool.cc | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/sled/system/thread_pool.cc b/src/sled/system/thread_pool.cc index 51773f5..cfa35f2 100644 --- a/src/sled/system/thread_pool.cc +++ b/src/sled/system/thread_pool.cc @@ -3,10 +3,11 @@ #include "sled/task_queue/task_queue_base.h" namespace sled { -ThreadPool::ThreadPool(int num_threads) +ThreadPool::ThreadPool(int num_threads) : delayed_thread_(sled::Thread::Create()) { if (num_threads == -1) { num_threads = std::thread::hardware_concurrency(); } scheduler_ = new sled::Scheduler(sled::Scheduler::Config().setWorkerThreadCount(num_threads)); + delayed_thread_->Start(); } ThreadPool::~ThreadPool() { delete scheduler_; } @@ -27,12 +28,19 @@ ThreadPool::PostDelayedTaskImpl(std::function &&task, const PostDelayedTaskTraits &traits, const Location &location) { + auto move_task_to_fiber = [task]() { task(); }; if (traits.high_precision) { - delayed_thread_->PostDelayedTaskWithPrecision(TaskQueueBase::DelayPrecision::kHigh, std::move(task), delay, - location); + delayed_thread_->PostDelayedTaskWithPrecision( + TaskQueueBase::DelayPrecision::kHigh, + std::move(move_task_to_fiber), + delay, + location); } else { - delayed_thread_->PostDelayedTaskWithPrecision(TaskQueueBase::DelayPrecision::kLow, std::move(task), delay, - location); + delayed_thread_->PostDelayedTaskWithPrecision( + TaskQueueBase::DelayPrecision::kLow, + std::move(move_task_to_fiber), + delay, + location); } }