重命名线程池

This commit is contained in:
Nanako 2025-01-05 22:28:21 +08:00
parent 3d811d3b20
commit 48a6140deb
2 changed files with 10 additions and 10 deletions

View File

@ -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) { 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; stop = true;
// 唤醒所有线程 // 唤醒所有线程
task_available.broadcast_signal(); task_available.broadcast_signal();
} }
void queued_thread_pool::worker_thread() { void thread_pool::worker_thread() {
while (true) { while (true) {
// 如果线程池停止且任务队列为空,则退出 // 如果线程池停止且任务队列为空,则退出
if (stop && tasks.empty()) { if (stop && tasks.empty()) {

View File

@ -10,21 +10,21 @@
#include "containers/safe_queue.h" #include "containers/safe_queue.h"
#include "containers/safe_vector.h" #include "containers/safe_vector.h"
class queued_thread_pool { class thread_pool {
public: public:
/** /**
* @brief 线 * @brief 线
* @param num_threads 线线, CPU 1, 线 * @param num_threads 线线, CPU 1, 线
*/ */
explicit queued_thread_pool(size_t num_threads = std::thread::hardware_concurrency() - 1); explicit thread_pool(size_t num_threads = std::thread::hardware_concurrency() - 1);
~queued_thread_pool(); ~thread_pool();
/** /**
* @brief 线 * @brief 线
* @return 线 * @return 线
*/ */
static queued_thread_pool& global() { static thread_pool& global() {
static queued_thread_pool instance; static thread_pool instance;
return instance; return instance;
} }