diff --git a/src/UtilsCtrl/ThreadPool/UThreadPool.cpp b/src/UtilsCtrl/ThreadPool/UThreadPool.cpp index f39acb6e..2eb3d016 100644 --- a/src/UtilsCtrl/ThreadPool/UThreadPool.cpp +++ b/src/UtilsCtrl/ThreadPool/UThreadPool.cpp @@ -209,16 +209,12 @@ CStatus UThreadPool::releaseSecondaryThread(CInt size) { CIndex UThreadPool::dispatch(const CIndex origIndex) { CIndex realIndex = 0; if (CGRAPH_DEFAULT_TASK_STRATEGY == origIndex) { - realIndex = cur_index_++; + realIndex = cur_index_.fetch_add(1, std::memory_order_relaxed) % config_.max_thread_size_; if (realIndex >= 0 && realIndex < config_.default_thread_size_ && primary_threads_[realIndex]->is_running_) { // 如果是默认调度,并且被放置到 正在running 的pt中,则切换为 trigger_one 的策略,防止阻塞 realIndex = CGRAPH_TRIGGER_ALL_THREAD_STRATEGY; } - - if (cur_index_ >= config_.max_thread_size_ || cur_index_ < 0) { - cur_index_ = 0; - } } else { realIndex = origIndex; } diff --git a/src/UtilsCtrl/ThreadPool/UThreadPool.h b/src/UtilsCtrl/ThreadPool/UThreadPool.h index adca1708..800ca1e8 100644 --- a/src/UtilsCtrl/ThreadPool/UThreadPool.h +++ b/src/UtilsCtrl/ThreadPool/UThreadPool.h @@ -199,7 +199,7 @@ class UThreadPool : public UThreadObject { private: CBool is_init_ { false }; // 是否初始化 - CInt cur_index_ = 0; // 记录放入的线程数 + std::atomic cur_index_ { 0 }; // 记录放入的线程数 UAtomicQueue task_queue_; // 用于存放普通任务 UAtomicPriorityQueue priority_task_queue_; // 运行时间较长的任务队列,仅在辅助线程中执行 std::vector primary_threads_; // 记录所有的主线程 diff --git a/test/Performance/test-performance-04.cpp b/test/Performance/test-performance-04.cpp index 049e93aa..677879ca 100644 --- a/test/Performance/test-performance-04.cpp +++ b/test/Performance/test-performance-04.cpp @@ -15,7 +15,7 @@ void test_performance_04() { const int layer = 8; const int nodePerLayer = 8; - const int runTimes = 100000; + const int runTimes = 300000; UThreadPoolConfig config; config.default_thread_size_ = nodePerLayer; diff --git a/tutorial/T01-Simple.cpp b/tutorial/T01-Simple.cpp index 006a2661..f3930350 100644 --- a/tutorial/T01-Simple.cpp +++ b/tutorial/T01-Simple.cpp @@ -42,7 +42,7 @@ void tutorial_simple() { /* 运行流图信息。初始化后,支持多次循环计算 */ for (int i = 0; i < 3; i++) { - status += pipeline->run(); + status = pipeline->run(); CGRAPH_ECHO("==== tutorial_simple, loop : [%d], and run status = [%d].", i + 1, status.getCode()); }