Skip to content

Commit 329e432

Browse files
committed
[bugfix] fix notify sleep 1s bug
1 parent aa872a8 commit 329e432

3 files changed

Lines changed: 14 additions & 7 deletions

File tree

src/UtilsCtrl/ThreadPool/Queue/UWorkStealingQueue.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,15 @@ class UWorkStealingQueue : public UQueueObject {
169169
return result; // 如果非空,表示盗取成功
170170
}
171171

172+
173+
/**
174+
* 判断当前队列是否为空
175+
* @return
176+
*/
177+
CBool isEmpty() const {
178+
return deque_.empty();
179+
}
180+
172181
UWorkStealingQueue() = default;
173182

174183
CGRAPH_NO_ALLOWED_COPY(UWorkStealingQueue)

src/UtilsCtrl/ThreadPool/Thread/UThreadPrimary.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,12 @@ class UThreadPrimary : public UThreadBase {
114114
* 休眠一定时间后,然后恢复执行状态,避免出现异常情况导致无法唤醒
115115
*/
116116
CVoid fatWait() {
117-
cur_empty_epoch_++;
117+
++cur_empty_epoch_;
118118
CGRAPH_YIELD();
119119
if (cur_empty_epoch_ >= config_->primary_thread_busy_epoch_) {
120120
CGRAPH_UNIQUE_LOCK lk(mutex_);
121121
cv_.wait_for(lk, std::chrono::milliseconds(config_->primary_thread_empty_interval_),
122-
[this] { return 0 == cur_empty_epoch_; });
122+
[this] { return 0 == cur_empty_epoch_ || !wsq_.isEmpty(); });
123123
cur_empty_epoch_ = 0;
124124
}
125125
}
@@ -131,9 +131,7 @@ class UThreadPrimary : public UThreadBase {
131131
* @return
132132
*/
133133
CVoid pushTask(UTask&& task) {
134-
while (!wsq_.tryPush(std::move(task))) {
135-
CGRAPH_YIELD();
136-
}
134+
wsq_.push(std::move(task));
137135
{
138136
CGRAPH_LOCK_GUARD lk(mutex_);
139137
cur_empty_epoch_ = 0;
@@ -257,7 +255,7 @@ class UThreadPrimary : public UThreadBase {
257255

258256
private:
259257
CInt index_ {0}; // 线程index
260-
std::atomic<CInt> cur_empty_epoch_ {0}; // 当前空转的轮数信息
258+
std::atomic<CInt> cur_empty_epoch_ {0 }; // 当前空转的轮数信息
261259
UWorkStealingQueue<UTask> wsq_ {}; // 内部队列信息
262260
std::vector<UThreadPrimary *>* pool_threads_ {}; // 用于存放线程池中的线程信息
263261
std::vector<CInt> steal_targets_ {}; // 被偷的目标信息

src/UtilsCtrl/ThreadPool/UThreadPoolDefine.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ static const CBool CGRAPH_BATCH_TASK_ENABLE = false;
5353
static const CInt CGRAPH_MAX_LOCAL_BATCH_SIZE = 2; // 批量执行本地任务最大值
5454
static const CInt CGRAPH_MAX_POOL_BATCH_SIZE = 2; // 批量执行通用任务最大值
5555
static const CInt CGRAPH_MAX_STEAL_BATCH_SIZE = 2; // 批量盗取任务最大值
56-
static const CInt CGRAPH_PRIMARY_THREAD_BUSY_EPOCH = 5; // 主线程进入wait状态的轮数,数值越大,理论性能越高,但空转可能性也越大
56+
static const CInt CGRAPH_PRIMARY_THREAD_BUSY_EPOCH = 3; // 主线程进入wait状态的轮数,数值越大,理论性能越高,但空转可能性也越大
5757
static const CMSec CGRAPH_PRIMARY_THREAD_EMPTY_INTERVAL = 1000; // 主线程进入休眠状态的默认时间
5858
static const CSec CGRAPH_SECONDARY_THREAD_TTL = 10; // 辅助线程ttl,单位为s
5959
static const CBool CGRAPH_MONITOR_ENABLE = false; // 是否开启监控程序

0 commit comments

Comments
 (0)