个人主页:Lei宝啊
愿所有美好如期而遇
线程池
实现线程类
#pragma once #include#include #include #include #include #include #include #include #include "sem.hpp" using namespace std; class Thread { using func_t = function ; public: void Excute() { _func(_threadname); } public: Thread(func_t func, const std::string &name="none-name") : _func(func), _threadname(name), _stop(true) {} static void *threadroutine(void *args) // 类成员函数,形参是有this指针的!! { Thread *self = static_cast (args); self->Excute(); return nullptr; } bool Start() { int n = pthread_create(&_tid, nullptr, threadroutine, this); if(!n) { _stop = false; return true; } else { return false; } } void Detach() { if(!_stop) { pthread_detach(_tid); } } void Join() { if(!_stop) { pthread_join(_tid, nullptr); } } std::string name() { return _threadname; } void Stop() { _stop = true; } ~Thread() {} private: pthread_t _tid; std::string _threadname; func_t _func; bool _stop; };
任务类
这个就随便写了,用来简单测试。
#includeusing namespace std; class Task { public: void Compare() { cout << "compare" << endl; } void operator()() { Compare(); } int a = 1; int b = 2; };
线程池类
#include "Thread_pool.hpp" #includeconst int g_pthreadnum = 6; pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t cond = PTHREAD_COND_INITIALIZER; //线程池 template class ThreadPool { public: ThreadPool(int num = g_pthreadnum) :pthreadnum(num) ,state(false) ,waitnum(0) {} void Everydo(string name) { while(true) { Lock(); while(qtask.empty() && state) { waitnum++; Wait_Pthread(); waitnum--; } if(qtask.empty() && !state) { UnLock(); cout << name << ": quit" << endl; break; } T task = qtask.front(); qtask.pop(); task(); UnLock(); } } bool Push(const T& t) { Lock(); bool ret = false; if(state) { qtask.push(t); if(waitnum > 0) WakeUp_sigle(); ret = true; } UnLock(); return ret; } void ThreadInit() { for(int i=0; i vthread; //一个任务队列,创建线程池对象时,线程创建好,我们需要从外部接收任务分配给线程执行 //我们希望能够接收任意类型的任务,仿函数,函数,lambda表达式等,所以使用模板 queue qtask; //一个状态,控制线程池的退出 bool state; int waitnum; void Lock() { pthread_mutex_lock(&mutex); } void UnLock() { pthread_mutex_unlock(&mutex); } void WakeUp_sigle() { pthread_cond_signal(&cond); } void WakeUp_All() { pthread_cond_broadcast(&cond); } void Wait_Pthread() { pthread_cond_wait(&cond, &mutex); } };
测试
还没有评论,来说两句吧...