结合了条件量,互斥量,任务基类,任务链表,这里对线程池类进行了完整的封装,并且在底部附上了线程池的一个简易测试案例。下面是封装源码
phreadpool.h
#ifndef _PTHREADPOOL_H
#define _PTHREADPOOL_H
#include <list>
#include <iostream>
#include<unistd.h>
#include "PThreadCond.h"
#include "PThreadMutex.h"
#include "Task.h"
using namespace std;
class CPThreadPool
{
public:
CPThreadPool(int max = 20,int min = 3,int waitsec = 60);
~CPThreadPool();
static void *TaskThread(void *arg);
void AddTask(CTask *task);
void start();
void destory();
void CreateThread();
private:
bool m_stop;
int m_maxcount;
int m_mincount;
int m_waitcount;
int m_curcount;
int m_waitsec;
CPThreadCond m_cond;
CPThreadCond m_taskMutex;
list<CTask *> m_tasklist;
};
#endif