From 8ebf49d1d889b0a8f5a8493a8847665991114d61 Mon Sep 17 00:00:00 2001 From: ChunelFeng Date: Sat, 9 Nov 2024 23:34:16 +0800 Subject: [PATCH] =?UTF-8?q?bugfix:=20=E4=BF=AE=E5=A4=8D=E4=BA=86=E8=BE=85?= =?UTF-8?q?=E5=8A=A9=E7=BA=BF=E7=A8=8B=E5=94=A4=E9=86=92=E5=BB=B6=E6=97=B6?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 3 ++- src/UtilsCtrl/ThreadPool/Queue/UAtomicQueue.h | 27 +++++++++++++++++++++++++-- src/UtilsCtrl/ThreadPool/UThreadPool.cpp | 4 +++- src/UtilsCtrl/ThreadPool/UThreadPoolDefine.h | 4 ++-- 4 files changed, 32 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 597fdee..380d668 100644 --- a/README.md +++ b/README.md @@ -83,8 +83,9 @@ int main() { [2023.03.07 - v1.2.0 - Chunel] * 优化windows版本功能 -[2023.10.07 - v1.2.1 - Chunel] +[2023.11.09 - v1.2.1 - Chunel] * 更新执行策略,优化整体性能 +* 修复辅助线程唤醒延时的问题 ------------ #### 附录-2. 联系方式 diff --git a/src/UtilsCtrl/ThreadPool/Queue/UAtomicQueue.h b/src/UtilsCtrl/ThreadPool/Queue/UAtomicQueue.h index a8a0386..dc1d4d7 100644 --- a/src/UtilsCtrl/ThreadPool/Queue/UAtomicQueue.h +++ b/src/UtilsCtrl/ThreadPool/Queue/UAtomicQueue.h @@ -83,7 +83,11 @@ public: */ std::unique_ptr popWithTimeout(CMSec ms) { CGRAPH_UNIQUE_LOCK lk(mutex_); - if (!cv_.wait_for(lk, std::chrono::milliseconds(ms), [this] { return !queue_.empty(); })) { + if (!cv_.wait_for(lk, std::chrono::milliseconds(ms), + [this] { return (!queue_.empty()) || (!ready_flag_); })) { + return nullptr; + } + if (queue_.empty() || !ready_flag_) { return nullptr; } @@ -135,10 +139,29 @@ public: return queue_.empty(); } + /** + * 功能是通知所有的辅助线程停止工作 + * @return + */ + CVoid reset() { + ready_flag_ = false; + cv_.notify_all(); + } + + /** + * 初始化状态 + * @return + */ + CVoid setup() { + ready_flag_ = true; + queue_ = {}; + } + CGRAPH_NO_ALLOWED_COPY(UAtomicQueue) private: - std::queue> queue_; + std::queue> queue_; // 任务队列 + CBool ready_flag_ { true }; // 执行标记,主要用于快速释放 destroy 逻辑中,多个辅助线程等待的状态 }; CGRAPH_NAMESPACE_END diff --git a/src/UtilsCtrl/ThreadPool/UThreadPool.cpp b/src/UtilsCtrl/ThreadPool/UThreadPool.cpp index c4e7a53..ef256aa 100644 --- a/src/UtilsCtrl/ThreadPool/UThreadPool.cpp +++ b/src/UtilsCtrl/ThreadPool/UThreadPool.cpp @@ -47,6 +47,7 @@ CStatus UThreadPool::init() { monitor_thread_ = std::move(std::thread(&UThreadPool::monitor, this)); thread_record_map_.clear(); + task_queue_.setup(); primary_threads_.reserve(config_.default_thread_size_); for (int i = 0; i < config_.default_thread_size_; i++) { auto ptr = CGRAPH_SAFE_MALLOC_COBJECT(UThreadPrimary); // 创建核心线程数 @@ -148,6 +149,7 @@ CStatus UThreadPool::destroy() { primary_threads_.clear(); // secondary 线程是智能指针,不需要delete + task_queue_.reset(); for (auto &st : secondary_threads_) { status += st->destroy(); } @@ -192,7 +194,7 @@ CIndex UThreadPool::dispatch(CIndex origIndex) { CIndex realIndex = 0; if (CGRAPH_DEFAULT_TASK_STRATEGY == origIndex) { realIndex = cur_index_++; - if (cur_index_ >= config_.default_thread_size_ || cur_index_ < 0) { + if (cur_index_ >= config_.max_thread_size_ || cur_index_ < 0) { cur_index_ = 0; } } else { diff --git a/src/UtilsCtrl/ThreadPool/UThreadPoolDefine.h b/src/UtilsCtrl/ThreadPool/UThreadPoolDefine.h index 1cf36c1..c1f3d8b 100644 --- a/src/UtilsCtrl/ThreadPool/UThreadPoolDefine.h +++ b/src/UtilsCtrl/ThreadPool/UThreadPoolDefine.h @@ -51,7 +51,7 @@ static const int CGRAPH_LONG_TIME_TASK_STRATEGY = -101; */ static const int CGRAPH_DEFAULT_THREAD_SIZE = 8; // 默认开启主线程个数 static const int CGRAPH_SECONDARY_THREAD_SIZE = 0; // 默认开启辅助线程个数 -static const int CGRAPH_MAX_THREAD_SIZE = 16; // 最大线程个数 +static const int CGRAPH_MAX_THREAD_SIZE = 8; // 最大线程个数 static const int CGRAPH_MAX_TASK_STEAL_RANGE = 7; // 盗取机制相邻范围 static const bool CGRAPH_BATCH_TASK_ENABLE = false; // 是否开启批量任务功能 static const int CGRAPH_MAX_LOCAL_BATCH_SIZE = 2; // 批量执行本地任务最大值 @@ -62,7 +62,7 @@ static const CMSec CGRAPH_PRIMARY_THREAD_EMPTY_INTERVAL = 1000; static const int CGRAPH_SECONDARY_THREAD_TTL = 10; // 辅助线程ttl,单位为s static const bool CGRAPH_MONITOR_ENABLE = false; // 是否开启监控程序 static const CSec CGRAPH_MONITOR_SPAN = 5; // 监控线程执行间隔,单位为s -static const CMSec CGRAPH_QUEUE_EMPTY_INTERVAL = 3; // 队列为空时,等待的时间。仅针对辅助线程,单位为ms +static const CMSec CGRAPH_QUEUE_EMPTY_INTERVAL = 1000; // 队列为空时,等待的时间。仅针对辅助线程,单位为ms static const bool CGRAPH_BIND_CPU_ENABLE = false; // 是否开启绑定cpu模式(仅针对主线程) static const int CGRAPH_PRIMARY_THREAD_POLICY = CGRAPH_THREAD_SCHED_OTHER; // 主线程调度策略 static const int CGRAPH_SECONDARY_THREAD_POLICY = CGRAPH_THREAD_SCHED_OTHER; // 辅助线程调度策略