Skip to content

Commit

Permalink
bugfix: 修复了辅助线程唤醒延时的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
ChunelFeng committed Nov 9, 2024
1 parent 3a6022a commit 8ebf49d
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. 联系方式
Expand Down
27 changes: 25 additions & 2 deletions src/UtilsCtrl/ThreadPool/Queue/UAtomicQueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,11 @@ class UAtomicQueue : public UQueueObject {
*/
std::unique_ptr<T> 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;
}

Expand Down Expand Up @@ -135,10 +139,29 @@ class UAtomicQueue : public UQueueObject {
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<std::unique_ptr<T>> queue_;
std::queue<std::unique_ptr<T>> queue_; // 任务队列
CBool ready_flag_ { true }; // 执行标记,主要用于快速释放 destroy 逻辑中,多个辅助线程等待的状态
};

CGRAPH_NAMESPACE_END
Expand Down
4 changes: 3 additions & 1 deletion src/UtilsCtrl/ThreadPool/UThreadPool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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); // 创建核心线程数
Expand Down Expand Up @@ -148,6 +149,7 @@ CStatus UThreadPool::destroy() {
primary_threads_.clear();

// secondary 线程是智能指针,不需要delete
task_queue_.reset();
for (auto &st : secondary_threads_) {
status += st->destroy();
}
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions src/UtilsCtrl/ThreadPool/UThreadPoolDefine.h
Original file line number Diff line number Diff line change
Expand Up @@ -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; // 批量执行本地任务最大值
Expand All @@ -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; // 辅助线程调度策略
Expand Down

0 comments on commit 8ebf49d

Please sign in to comment.