这个线程池是为了自己使用的,记录一下
以下为第一版,如果没注意,使用的过程中容易内存泄漏,而且各处延时较长
由于无法将第二版直接贴到后面,故第二版我后面新起了一篇文章
ThreadPool.h
#ifndef THREADPOOL_H
#define THREADPOOL_H
#include <thread>
/** 任务函数:function_param-任务函数的参数(指针,外部需动态malloc申请后传入,内部会销毁) */
typedef void( *TaskFunction)(void *function_param);
/** 线程池 */
class ThreadPool
{
public:
ThreadPool();
~ThreadPool();
/** 开始:MaxThreadNum-最大线程数,_MaxWaitTaskNum-最大等待中任务数量:返回0-成功,-1-创建工作线程失败,-2-管理线程创建失败 */
int start(int _MaxThreadNum = 10, int _MaxWaitTaskNum = 10000);
/** 结束 */
void stop();
/** 添加任务:_task_function-任务函数,_function_param-任务函数参数:返回0-成功,-1-传入参数存在空值,-2-结束标识为true,-3-等待中的任务数量已达上限 */
int AddTask(TaskFunction _task_function, void *_function_param);
private:
/** 最大线程数 */
int MaxThreadNum;
/** 最大等待中任务数量 */
int MaxWaitTaskNum;
/** 结束标识 */
bool StopMark;
/** 管理线程对象 */
std::thread ThreadManage;
/** 管理函数 */
void ManageFunction();
};
#endif
ThreadPool.cpp
#include "ThreadPool.h"
#include <queue>
#include <mutex>
#include <atomic>
/** 线程任务 */
class ThreadTask
{
public:
ThreadTask();
ThreadTask(TaskFunction _task_function, void *_function_param);
~ThreadTask();
/** 任务函数 */
TaskFunction task_function;
/** 任务函数的参数 */
void *function_param;
};
ThreadTask::ThreadTask()
{
task_function = nullptr;
function_param = nullptr;
}
ThreadTask::ThreadTask(TaskFunction _task_function, void *_function_param)
{
task_function = _task_function;
function_param = _function_param;
}
ThreadTask::~ThreadTask()
{
if (function_param != nullptr)
{
free(function_param);
function_param = nullptr;
}
}
/** 任务队列 */
static std::queue<ThreadTask *> s_QueueTask;
/** 任务队列锁 */
static std::mutex s_MutexQueueTask;
/** 获取程序运行时长(秒) */
static long GetAppClock()
{
clock_t ct = clock();
long lr = ct / CLOCKS_PER_SEC;
return lr;
}
/** 工作线程 */
class ThreadWork
{
public:
ThreadWork();
~ThreadWork();
/** 线程 */
std::thread _std_thread;
/** 线程状态:-1-主动结束线程,0-默认 */
int _thread_state;
/** 最后一次线程繁忙时刻时钟 */
std::atomic<long> _last_busy_clock;
};
ThreadWork::ThreadWork()
{
_thread_state = 0;
_last_busy_clock = GetAppClock();
}
ThreadWork::~ThreadWork()
{
_thread_state = -1;
if (_std_thread.joinable())
{
_std_thread.join();
}
}
/** 工作线程队列 */
static std::vector<ThreadWork *> s_VectorThreadWork;
/** 工作函数:param-参数 */
static void WorkFunction(void *param)
{
printf("WorkFunction:thread %d begin\n", std::this_thread::get_id());
ThreadWork *tw = (ThreadWork *)param;
while (tw->_thread_state != -1)
{
ThreadTask *tt = nullptr;
{
std::lock_guard<std::mutex> slgsm(s_MutexQueueTask);
if (s_QueueTask.size() > 0)
{
tt = s_QueueTask.front();
s_QueueTask.pop();
}
}
if (tt == nullptr)
{
std::this_thread::sleep_for(std::chrono::seconds(1));
}
else
{
tw->_last_busy_clock = GetAppClock();
tt->task_function(tt->function_param);
delete tt;
tt = nullptr;
}
}
printf("WorkFunction:thread %d end\n", std::this_thread::get_id());
}
ThreadPool::ThreadPool()
{
MaxThreadNum = 10;
MaxWaitTaskNum = 10000;
StopMark = true;
}
ThreadPool::~ThreadPool()
{
stop();
}
int ThreadPool::start(int _MaxThreadNum, int _MaxWaitTaskNum)
{
MaxThreadNum = _MaxThreadNum;
if (MaxThreadNum < 1)
{
MaxThreadNum = 1;
}
MaxWaitTaskNum = _MaxWaitTaskNum;
if (MaxWaitTaskNum < 1)
{
MaxWaitTaskNum = 1;
}
ThreadWork *tw = new ThreadWork();
tw->_std_thread = std::thread(WorkFunction, tw);
if (tw->_std_thread.joinable() == false)
{
delete tw;
tw = nullptr;
return -1;
}
s_VectorThreadWork.push_back(tw);
StopMark = false;
ThreadManage = std::thread(&ThreadPool::ManageFunction, this);
if (ThreadManage.joinable() == false)
{
stop();
return -2;
}
return 0;
}
void ThreadPool::stop()
{
StopMark = true;
if (ThreadManage.joinable())
{
ThreadManage.join();
}
{
std::lock_guard<std::mutex> slgsm(s_MutexQueueTask);
while (s_QueueTask.empty() == false)
{
ThreadTask *tt = s_QueueTask.front();
s_QueueTask.pop();
delete tt;
tt = nullptr;
}
}
while (s_VectorThreadWork.empty() == false)
{
ThreadWork *tw = s_VectorThreadWork.front();
s_VectorThreadWork.erase(s_VectorThreadWork.begin());
delete tw;
tw = nullptr;
}
}
int ThreadPool::AddTask(TaskFunction _task_function, void *_function_param)
{
if (_task_function == nullptr || _function_param == nullptr)
{
return -1;
}
if (StopMark == true)
{
return -2;
}
std::lock_guard<std::mutex> slgsm(s_MutexQueueTask);
if (s_QueueTask.size() >= MaxWaitTaskNum)
{
return -3;
}
s_QueueTask.push(new ThreadTask(_task_function, _function_param));
return 0;
}
void ThreadPool::ManageFunction()
{
printf("ThreadPool::ManageFunction:thread %d begin\n", std::this_thread::get_id());
while (StopMark == false)
{
unsigned int uis = 0;
{
std::lock_guard<std::mutex> slgsm(s_MutexQueueTask);
uis = s_QueueTask.size();
}
if (uis <= 0)
{
if (s_VectorThreadWork.size() > 1)
{
long CurClock = GetAppClock();
for (std::vector<ThreadWork *>::iterator it = s_VectorThreadWork.begin(); it != s_VectorThreadWork.end(); it++)
{
if ((CurClock - (*it)->_last_busy_clock) >= 60)
{
ThreadWork *tw = *it;
s_VectorThreadWork.erase(it);
delete tw;
tw = nullptr;
break;
}
}
}
std::this_thread::sleep_for(std::chrono::seconds(1));
}
else
{
if (s_VectorThreadWork.size() < MaxThreadNum)
{
ThreadWork *tw = new ThreadWork();
tw->_std_thread = std::thread(WorkFunction, tw);
if (tw->_std_thread.joinable() == false)
{
delete tw;
tw = nullptr;
printf("ThreadPool::ManageFunction:std::thread fail\n");
}
else
{
s_VectorThreadWork.push_back(tw);
}
}
else
{
std::this_thread::sleep_for(std::chrono::seconds(1));
}
}
}
printf("ThreadPool::ManageFunction:thread %d end\n", std::this_thread::get_id());
}
ThreadPoolTest.cpp
#include "ThreadPool.h"
#pragma warning(disable:4996)
struct StructTest
{
char ca[1024];
char cb[1024];
char cc[1024];
char cd[1024];
int ia;
int ib;
int ic;
int id;
};
void func(void *pa)
{
StructTest *st = (StructTest *)pa;
printf("*****%s %s %s %s %d %d %d %d*****\n", st->ca, st->cb, st->cc, st->cd, st->ia, st->ib, st->ic, st->id);
}
int main()
{
ThreadPool tp;
int ir = tp.start();
printf("ir=%d\n", ir);
if (ir == 0)
{
for (size_t i = 0; i < 100; i++)
{
StructTest *st = (StructTest *)malloc(sizeof(StructTest));
memset(st, 0, sizeof(StructTest));
st->ia = i + 1;
st->ib = i + 2;
st->ic = i + 100;
st->id = i + 200;
sprintf(st->ca, "num:%d", st->ia);
sprintf(st->cb, "num:%d", st->ib);
sprintf(st->cc, "num:%d", st->ic);
sprintf(st->cd, "num:%d", st->id);
tp.AddTask(func, (void *)st);
}
}
getchar();
return 0;
}