Browse Source

bugfix: 修复了辅助线程唤醒延时的问题

ChunelFeng 10 months ago
parent
commit
8ebf49d1d8
4 changed files with 32 additions and 6 deletions
  1. +2
    -1
      README.md
  2. +25
    -2
      src/UtilsCtrl/ThreadPool/Queue/UAtomicQueue.h
  3. +3
    -1
      src/UtilsCtrl/ThreadPool/UThreadPool.cpp
  4. +2
    -2
      src/UtilsCtrl/ThreadPool/UThreadPoolDefine.h

+ 2
- 1
README.md View File

@ -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. 联系方式

+ 25
- 2
src/UtilsCtrl/ThreadPool/Queue/UAtomicQueue.h View File

@ -83,7 +83,11 @@ public:
*/
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;
}
@ -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<std::unique_ptr<T>> queue_;
std::queue<std::unique_ptr<T>> queue_; //
CBool ready_flag_ { true }; // destroy 线
};
CGRAPH_NAMESPACE_END

+ 3
- 1
src/UtilsCtrl/ThreadPool/UThreadPool.cpp View File

@ -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 {

+ 2
- 2
src/UtilsCtrl/ThreadPool/UThreadPoolDefine.h View File

@ -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; // 线ttls
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; // 线

Loading…
Cancel
Save