主线程负责监听文件描述符接受socket的新链接,若当前监听的socket发生了读写事件,然后将任务插入到请求队列。工作线程从请求队列中取出任务,完成读写数据的处理。
线程池的定义:
template<typename T>
class threadpool{
public:
//thread_number是线程池中线程的数量
//max_requests是请求队列中最多允许的、等待处理的请求的数量
//connPool是数据库连接池指针
threadpool(connection_pool *connPool, int thread_number = 8, int max_request = 10000);
~threadpool();
//像请求队列中插入任务请求
bool append(T* request);
private:
//工作线程运行的函数
//它不断从工作队列中取出任务并执行之
static void *worker(void *arg);
void run();
private:
//线程池中的线程数
int m_thread_number;
//请求队列中允许的最大请求数
int m_max_requests;
//描述线程池的数组,其大小为m_thread_number
pthread_t *m_threads;
//请求队列
std::list<T *>m_workqueue;
//保护请求队列的互斥锁
locker m_queuelocker;
//是否有任务需要处理
sem m_queuestat;
//是否结束线程
bool m_stop;
};
线程池的创建与回收
构造函数中创建线程池,pthread_create函数中将类的对象作为参数传递给静态函数(worker),在静态函数中引用这个对象,并调用其动态方法(run)。具体的,类对象传递时用this指针,传递给静态函数后,将其转换为线程池类,并调用私有成员函数run。
template<typename T>
threadpool<T>::threadpool( connection_pool *connPool, int thread_number, int max_requests) :m_thread_number(thread_number), m_max_requests(max_requests), m_stop(false), m_threads(NULL),m_connPool(connPool){
if(thread_number<=0||max_requests<=0)
throw std::exception();
//线程id初始化
m_threads=new pthread_t[m_thread_number];
if(!m_threads)
throw std::exception();
for(int i=0;i<thread_number;++i)
{
//循环创建线程,并将工作线程按要求进行运行
if(pthread_create(m_threads+i,NULL,worker,this)!=0){
delete [] m_threads;
throw std::exception();
}
//将线程进行分离后,不用单独对工作线程进行回收
if(pthread_detach(m_threads[i])){
delete[] m_threads;
throw std::exception();
}
}
}