1 Star 0 Fork 13

qiuchangjie/ThreadPool

forked from He Lei/ThreadPool 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
queue_pool.h 1.78 KB
一键复制 编辑 原始数据 按行查看 历史
heleifz 提交于 2013-07-05 16:37 . first commit
// 利用 pthread 实现了 work queue + thread pool 模型
// work queue : 任务队列, 用户将需要执行任务放进队列后退出
// thread pool : 线程池, 包含若干个等待着的工作线程, 只要任务队列中有东西, 就取出来执行
//
// 要点:
// * 只能由一个线程来管理 ThreadPool (这个类本身不是 MT-safe 的)
// * 任务的添加是异步的, dispatch 后立刻返回, 返回后不能保证任务已经开始执行
// * 用 work_wrapper 来包装线程函数
// * 通过 condition variable 在进行线程等待与线程唤醒
// * 通过 clean up handler 来解锁并且释放资源
// * 使用 semaphore 构造的 barrier 对当前所有任务进行同步
// 返回实际建立的线程数
#include "pthread.h"
#include "semaphore.h"
#include <queue>
#include <stack>
typedef void* (*WorkFunc)(void*);
typedef void* Argument;
// 若创建的线程超过系统允许的范围则抛出异常
struct OutOfThreadError {};
// 若线程池的创建线程数为 0 则抛出异常
struct IllegalThreadNumber{};
class Thread;
class WorkQueue;
// 线程池
class ThreadPool {
friend class Thread;
public:
// 初始化一次后线程数就固定了
ThreadPool(size_t thread_num);
~ThreadPool();
// 开始扫描任务队列线程
void run();
// 往线程池的任务队列里添加任务
void dispatch(WorkFunc func, Argument arg);
// 同步目前队列里的所有任务, 返回后线程池空闲
bool sync();
private:
// 不允许默认初始化
ThreadPool() {};
static void cleanqueue(void* arg);
static void cleanup(void* arg);
static void* runner(void* arg);
int unfinished;
int total;
bool running;
sem_t* barrier;
pthread_t* run_tid;
pthread_cond_t* th_cond;
pthread_mutex_t* th_lock;
std::stack<Thread*> threads;
WorkQueue* job_queue;
};
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/qiuchangjie/threadpool.git
git@gitee.com:qiuchangjie/threadpool.git
qiuchangjie
threadpool
ThreadPool
master

搜索帮助

D67c1975 1850385 1daf7b77 1850385