Skip to content

Commit 8ebf49d

Browse files
committed
bugfix: 修复了辅助线程唤醒延时的问题
1 parent 3a6022a commit 8ebf49d

File tree

4 files changed

+32
-6
lines changed

4 files changed

+32
-6
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,9 @@ int main() {
8383
[2023.03.07 - v1.2.0 - Chunel]
8484
* 优化windows版本功能
8585
86-
[2023.10.07 - v1.2.1 - Chunel]
86+
[2023.11.09 - v1.2.1 - Chunel]
8787
* 更新执行策略,优化整体性能
88+
* 修复辅助线程唤醒延时的问题
8889
8990
------------
9091
#### 附录-2. 联系方式

src/UtilsCtrl/ThreadPool/Queue/UAtomicQueue.h

+25-2
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,11 @@ class UAtomicQueue : public UQueueObject {
8383
*/
8484
std::unique_ptr<T> popWithTimeout(CMSec ms) {
8585
CGRAPH_UNIQUE_LOCK lk(mutex_);
86-
if (!cv_.wait_for(lk, std::chrono::milliseconds(ms), [this] { return !queue_.empty(); })) {
86+
if (!cv_.wait_for(lk, std::chrono::milliseconds(ms),
87+
[this] { return (!queue_.empty()) || (!ready_flag_); })) {
88+
return nullptr;
89+
}
90+
if (queue_.empty() || !ready_flag_) {
8791
return nullptr;
8892
}
8993

@@ -135,10 +139,29 @@ class UAtomicQueue : public UQueueObject {
135139
return queue_.empty();
136140
}
137141

142+
/**
143+
* 功能是通知所有的辅助线程停止工作
144+
* @return
145+
*/
146+
CVoid reset() {
147+
ready_flag_ = false;
148+
cv_.notify_all();
149+
}
150+
151+
/**
152+
* 初始化状态
153+
* @return
154+
*/
155+
CVoid setup() {
156+
ready_flag_ = true;
157+
queue_ = {};
158+
}
159+
138160
CGRAPH_NO_ALLOWED_COPY(UAtomicQueue)
139161

140162
private:
141-
std::queue<std::unique_ptr<T>> queue_;
163+
std::queue<std::unique_ptr<T>> queue_; // 任务队列
164+
CBool ready_flag_ { true }; // 执行标记,主要用于快速释放 destroy 逻辑中,多个辅助线程等待的状态
142165
};
143166

144167
CGRAPH_NAMESPACE_END

src/UtilsCtrl/ThreadPool/UThreadPool.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ CStatus UThreadPool::init() {
4747

4848
monitor_thread_ = std::move(std::thread(&UThreadPool::monitor, this));
4949
thread_record_map_.clear();
50+
task_queue_.setup();
5051
primary_threads_.reserve(config_.default_thread_size_);
5152
for (int i = 0; i < config_.default_thread_size_; i++) {
5253
auto ptr = CGRAPH_SAFE_MALLOC_COBJECT(UThreadPrimary); // 创建核心线程数
@@ -148,6 +149,7 @@ CStatus UThreadPool::destroy() {
148149
primary_threads_.clear();
149150

150151
// secondary 线程是智能指针,不需要delete
152+
task_queue_.reset();
151153
for (auto &st : secondary_threads_) {
152154
status += st->destroy();
153155
}
@@ -192,7 +194,7 @@ CIndex UThreadPool::dispatch(CIndex origIndex) {
192194
CIndex realIndex = 0;
193195
if (CGRAPH_DEFAULT_TASK_STRATEGY == origIndex) {
194196
realIndex = cur_index_++;
195-
if (cur_index_ >= config_.default_thread_size_ || cur_index_ < 0) {
197+
if (cur_index_ >= config_.max_thread_size_ || cur_index_ < 0) {
196198
cur_index_ = 0;
197199
}
198200
} else {

src/UtilsCtrl/ThreadPool/UThreadPoolDefine.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ static const int CGRAPH_LONG_TIME_TASK_STRATEGY = -101;
5151
*/
5252
static const int CGRAPH_DEFAULT_THREAD_SIZE = 8; // 默认开启主线程个数
5353
static const int CGRAPH_SECONDARY_THREAD_SIZE = 0; // 默认开启辅助线程个数
54-
static const int CGRAPH_MAX_THREAD_SIZE = 16; // 最大线程个数
54+
static const int CGRAPH_MAX_THREAD_SIZE = 8; // 最大线程个数
5555
static const int CGRAPH_MAX_TASK_STEAL_RANGE = 7; // 盗取机制相邻范围
5656
static const bool CGRAPH_BATCH_TASK_ENABLE = false; // 是否开启批量任务功能
5757
static const int CGRAPH_MAX_LOCAL_BATCH_SIZE = 2; // 批量执行本地任务最大值
@@ -62,7 +62,7 @@ static const CMSec CGRAPH_PRIMARY_THREAD_EMPTY_INTERVAL = 1000;
6262
static const int CGRAPH_SECONDARY_THREAD_TTL = 10; // 辅助线程ttl,单位为s
6363
static const bool CGRAPH_MONITOR_ENABLE = false; // 是否开启监控程序
6464
static const CSec CGRAPH_MONITOR_SPAN = 5; // 监控线程执行间隔,单位为s
65-
static const CMSec CGRAPH_QUEUE_EMPTY_INTERVAL = 3; // 队列为空时,等待的时间。仅针对辅助线程,单位为ms
65+
static const CMSec CGRAPH_QUEUE_EMPTY_INTERVAL = 1000; // 队列为空时,等待的时间。仅针对辅助线程,单位为ms
6666
static const bool CGRAPH_BIND_CPU_ENABLE = false; // 是否开启绑定cpu模式(仅针对主线程)
6767
static const int CGRAPH_PRIMARY_THREAD_POLICY = CGRAPH_THREAD_SCHED_OTHER; // 主线程调度策略
6868
static const int CGRAPH_SECONDARY_THREAD_POLICY = CGRAPH_THREAD_SCHED_OTHER; // 辅助线程调度策略

0 commit comments

Comments
 (0)