重命名线程池
This commit is contained in:
parent
3d811d3b20
commit
48a6140deb
@ -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()) {
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user