1 Star 0 Fork 0

trff/MyTinyWebserver

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
threadpool.h 1.06 KB
一键复制 编辑 原始数据 按行查看 历史
trff 提交于 2024-08-29 09:00 . 线程池
#pragma once
#include <pthread.h>
#include <list>
#include "locker.h"
// 文件名称:threadpool.h
// 类:threadpool
// 功能:线程池
// 作者:TRF
// 线程池类,模板类,方便复用
template <typename T>
class Threadpool
{
private:
int m_thread_num; // 线程数量
pthread_t *m_threads; // 线程池数组,大小为m_thread_num
int m_max_requeats; // 请求队列中最多允许的,等待处理的请求数量
std::list<T *> m_workqueue; // 请求队列,存放待处理的请求
Locker m_queuelocker; // 互斥锁
Semaphore m_queuestat; // 信号量,用来判断是否有任务需要处理
bool m_stop; // 是否结束线程
static void *worker(void *arg); // 线程函数,static只能在本文件中定义,不能放在类外面
void run(); // 运行线程池
public:
Threadpool(int thread_num = 8, int max_requeats = 100000);
~Threadpool();
bool append(T *request); // 添加任务到请求队列
};
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/trff/MyTinyWebserver.git
git@gitee.com:trff/MyTinyWebserver.git
trff
MyTinyWebserver
MyTinyWebserver
master

搜索帮助

23e8dbc6 1850385 7e0993f3 1850385