File tree 4 files changed +32
-6
lines changed
4 files changed +32
-6
lines changed Original file line number Diff line number Diff line change @@ -83,8 +83,9 @@ int main() {
83
83
[2023.03.07 - v1.2.0 - Chunel]
84
84
* 优化windows版本功能
85
85
86
- [2023.10.07 - v1.2.1 - Chunel]
86
+ [2023.11.09 - v1.2.1 - Chunel]
87
87
* 更新执行策略,优化整体性能
88
+ * 修复辅助线程唤醒延时的问题
88
89
89
90
------------
90
91
#### 附录-2. 联系方式
Original file line number Diff line number Diff line change @@ -83,7 +83,11 @@ class UAtomicQueue : public UQueueObject {
83
83
*/
84
84
std::unique_ptr<T> popWithTimeout (CMSec ms) {
85
85
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_) {
87
91
return nullptr ;
88
92
}
89
93
@@ -135,10 +139,29 @@ class UAtomicQueue : public UQueueObject {
135
139
return queue_.empty ();
136
140
}
137
141
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
+
138
160
CGRAPH_NO_ALLOWED_COPY (UAtomicQueue)
139
161
140
162
private:
141
- std::queue<std::unique_ptr<T>> queue_;
163
+ std::queue<std::unique_ptr<T>> queue_; // 任务队列
164
+ CBool ready_flag_ { true }; // 执行标记,主要用于快速释放 destroy 逻辑中,多个辅助线程等待的状态
142
165
};
143
166
144
167
CGRAPH_NAMESPACE_END
Original file line number Diff line number Diff line change @@ -47,6 +47,7 @@ CStatus UThreadPool::init() {
47
47
48
48
monitor_thread_ = std::move (std::thread (&UThreadPool::monitor, this ));
49
49
thread_record_map_.clear ();
50
+ task_queue_.setup ();
50
51
primary_threads_.reserve (config_.default_thread_size_ );
51
52
for (int i = 0 ; i < config_.default_thread_size_ ; i++) {
52
53
auto ptr = CGRAPH_SAFE_MALLOC_COBJECT (UThreadPrimary); // 创建核心线程数
@@ -148,6 +149,7 @@ CStatus UThreadPool::destroy() {
148
149
primary_threads_.clear ();
149
150
150
151
// secondary 线程是智能指针,不需要delete
152
+ task_queue_.reset ();
151
153
for (auto &st : secondary_threads_) {
152
154
status += st->destroy ();
153
155
}
@@ -192,7 +194,7 @@ CIndex UThreadPool::dispatch(CIndex origIndex) {
192
194
CIndex realIndex = 0 ;
193
195
if (CGRAPH_DEFAULT_TASK_STRATEGY == origIndex) {
194
196
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 ) {
196
198
cur_index_ = 0 ;
197
199
}
198
200
} else {
Original file line number Diff line number Diff line change @@ -51,7 +51,7 @@ static const int CGRAPH_LONG_TIME_TASK_STRATEGY = -101;
51
51
*/
52
52
static const int CGRAPH_DEFAULT_THREAD_SIZE = 8 ; // 默认开启主线程个数
53
53
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 ; // 最大线程个数
55
55
static const int CGRAPH_MAX_TASK_STEAL_RANGE = 7 ; // 盗取机制相邻范围
56
56
static const bool CGRAPH_BATCH_TASK_ENABLE = false ; // 是否开启批量任务功能
57
57
static const int CGRAPH_MAX_LOCAL_BATCH_SIZE = 2 ; // 批量执行本地任务最大值
@@ -62,7 +62,7 @@ static const CMSec CGRAPH_PRIMARY_THREAD_EMPTY_INTERVAL = 1000;
62
62
static const int CGRAPH_SECONDARY_THREAD_TTL = 10 ; // 辅助线程ttl,单位为s
63
63
static const bool CGRAPH_MONITOR_ENABLE = false ; // 是否开启监控程序
64
64
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
66
66
static const bool CGRAPH_BIND_CPU_ENABLE = false ; // 是否开启绑定cpu模式(仅针对主线程)
67
67
static const int CGRAPH_PRIMARY_THREAD_POLICY = CGRAPH_THREAD_SCHED_OTHER; // 主线程调度策略
68
68
static const int CGRAPH_SECONDARY_THREAD_POLICY = CGRAPH_THREAD_SCHED_OTHER; // 辅助线程调度策略
You can’t perform that action at this time.
0 commit comments