From 48a6140deb030ec34439d184b34352713ac6e950 Mon Sep 17 00:00:00 2001 From: Nanako <469449812@qq.com> Date: Sun, 5 Jan 2025 22:28:21 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E5=91=BD=E5=90=8D=E7=BA=BF=E7=A8=8B?= =?UTF-8?q?=E6=B1=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../async/{queued_thread_pool.cpp => thread_pool.cpp} | 10 +++++----- src/core/async/{queued_thread_pool.h => thread_pool.h} | 10 +++++----- 2 files changed, 10 insertions(+), 10 deletions(-) rename src/core/async/{queued_thread_pool.cpp => thread_pool.cpp} (67%) rename src/core/async/{queued_thread_pool.h => thread_pool.h} (87%) diff --git a/src/core/async/queued_thread_pool.cpp b/src/core/async/thread_pool.cpp similarity index 67% rename from src/core/async/queued_thread_pool.cpp rename to src/core/async/thread_pool.cpp index 999dd18..0147eed 100644 --- a/src/core/async/queued_thread_pool.cpp +++ b/src/core/async/thread_pool.cpp @@ -1,19 +1,19 @@ -#include "queued_thread_pool.h" +#include "thread_pool.h" -queued_thread_pool::queued_thread_pool(const size_t num_threads) : stop(false) { +thread_pool::thread_pool(const size_t num_threads) : stop(false) { // 创建线程 for (std::size_t i = 0; i < num_threads; ++i) { - threads.emplace_back(&queued_thread_pool::worker_thread, this); + threads.emplace_back(&thread_pool::worker_thread, this); } } -queued_thread_pool::~queued_thread_pool() { +thread_pool::~thread_pool() { stop = true; // 唤醒所有线程 task_available.broadcast_signal(); } -void queued_thread_pool::worker_thread() { +void thread_pool::worker_thread() { while (true) { // 如果线程池停止且任务队列为空,则退出 if (stop && tasks.empty()) { diff --git a/src/core/async/queued_thread_pool.h b/src/core/async/thread_pool.h similarity index 87% rename from src/core/async/queued_thread_pool.h rename to src/core/async/thread_pool.h index 11f6fd2..6a79eb0 100644 --- a/src/core/async/queued_thread_pool.h +++ b/src/core/async/thread_pool.h @@ -10,21 +10,21 @@ #include "containers/safe_queue.h" #include "containers/safe_vector.h" -class queued_thread_pool { +class thread_pool { public: /** * @brief 创建一个线程池 * @param num_threads 线程池中线程的数量, 默认为 CPU 核心数减 1, 避免主线程被占用 */ - explicit queued_thread_pool(size_t num_threads = std::thread::hardware_concurrency() - 1); - ~queued_thread_pool(); + explicit thread_pool(size_t num_threads = std::thread::hardware_concurrency() - 1); + ~thread_pool(); /** * @brief 获取全局线程池 * @return 全局线程池 */ - static queued_thread_pool& global() { - static queued_thread_pool instance; + static thread_pool& global() { + static thread_pool instance; return instance; }