个人主页:Lei宝啊
愿所有美好如期而遇
线程池
实现线程类
#pragma once
#include <pthread.h>
#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <unistd.h>
#include "sem.hpp"
using namespace std;
class Thread
{
using func_t = function<void(string)>;
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<Thread *>(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;
};
任务类
这个就随便写了,用来简单测试。
#include <iostream>
using namespace std;
class Task
{
public:
void Compare()
{
cout << "compare" << endl;
}
void operator()()
{
Compare();
}
int a = 1;
int b = 2;
};
线程池类
#include "Thread_pool.hpp"
#include <queue>
const int g_pthreadnum = 6;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
//线程池
template<class T>
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<pthreadnum; i++)
{
string name = "pthread-" + to_string(i);
vthread.emplace_back(bind(&ThreadPool::Everydo,
this, placeholders::_1), name);
//每个线程都要执行这个方法,这个方法会将任务队列中的任务分配给他们
}
}
void Wait()
{
for(auto &e : vthread) e.Join();
}
void Quit()
{
state = false;
WakeUp_All();
}
void Start()
{
state = true;
for(auto &e : vthread) e.Start();
}
private:
//线程池需要什么?线程数量,保存线程的容器
int pthreadnum;
vector<Thread> vthread;
//一个任务队列,创建线程池对象时,线程创建好,我们需要从外部接收任务分配给线程执行
//我们希望能够接收任意类型的任务,仿函数,函数,lambda表达式等,所以使用模板
queue<T> 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);
}
};
测试
标签:void,C++,线程,func,pthread,cond,include From: https://blog.csdn.net/m0_74824254/article/details/139147629