参考:http://hi.baidu.com/boahegcrmdghots/item/f3ca1a3c2d47fcc52e8ec2e1
/** autoptr.h** Created on: 2012-12-7* Author: kym*///智能指针模板#ifndef AUTOPTR_H_#define AUTOPTR_H_#include<iostream>template<class type>class autoptr{private:friend class thread_work;friend class thread_pool;type *arg;int count;autoptr(type *p):arg(p),count(1){}~autoptr(){if(arg != NULL){delete []arg;}}};#endif /* AUTOPTR_H_ */
/** thread_work.h** Created on: 2012-12-7* Author: kym*///执行类#ifndef THREAD_WORK_H_#define THREAD_WORK_H_#include"autoptr.h"typedef void *(* work_fun )( void *);class thread_work {private :autoptr <char> * arg ;work_fun fun ;public :thread_work ( work_fun f , char * p ): arg ( new autoptr <char> ( p )), fun ( f ){}thread_work ( const thread_work & orig ): arg ( orig . arg ), fun ( orig . fun ){++ arg -> count ;}thread_work & operator = ( const thread_work & orig ){++ orig . arg -> count ;if ((-- this -> arg -> count ) == 0 ){if ( this -> arg != NULL ){delete this -> arg ;}}this -> arg = orig . arg ;this -> fun = orig . fun ;return * this ;}void * run (){return this -> fun ( this -> arg -> arg );}~ thread_work (){if ((-- this -> arg -> count ) == 0 ){if ( this -> arg != NULL ){delete this -> arg ;}}}};#endif /* THREAD_WORK_H_ */
/** thread_pool.h** Created on: 2012-12-7* Author: kym*/#ifndef THREAD_POOL_H_#define THREAD_POOL_H_#include "thread_work.h"#include <pthread.h>#include <map>#include <deque>enum STATE { END = 0 , IDLE , BUSY };class uncopy {protected :uncopy (){}~ uncopy (){}private :uncopy ( const uncopy &);uncopy & operator = ( const uncopy &);};class thread_pool : private uncopy {private :pthread_cond_t cont ;pthread_mutex_t mutex ;std :: map < pthread_t , STATE > mEndlflag ;std :: deque < thread_work *> dwork ;int total ;int busy ;int curworknum ;bool shutdown ;public :thread_pool ( int max ): total ( max ), shutdown ( false ), busy ( 0 ), curworknum ( 0 ){}~ thread_pool (){this -> destory ();}private :int release ( int num , thread_pool * data );int realloc ( int num , thread_pool * data );static void * routin ( void * arg );public :int init ();void destory ();int add_work ( work_fun fun , char * arg );};#endif /* THREAD_POOL_H_ */
/** thread_pool.cpp** Created on: 2012-12-7* Author: kym*/#include "thread_pool.h"#define MAX_THREAD 1024#define MIN_THREAD 2//初始化,创建指定数目线程,放入池中int thread_pool::init(){if((pthread_mutex_init(&this->mutex,NULL) != 0) || (pthread_cond_init(&this->cont,NULL) != 0)){return 0;}int i;pthread_t pid;for(i = 0;i < this->total;++ i){pthread_create(&pid,NULL,&routin,this);pthread_detach(pid);this->mEndlflag[pid] = IDLE;}return 1;}//如果忙碌线程少于四分之一则释放int thread_pool::release(int num,thread_pool *data){int i = 0;std::map<pthread_t,STATE>::iterator iter = data->mEndlflag.begin();while((iter != data->mEndlflag.end()) && (i != num)){if(iter->second != BUSY){iter->second = END;++ i;}++ iter;}return 1;}//如果忙碌线程大于线程池总数,则增加int thread_pool::realloc(int num,thread_pool *data){int i;pthread_t pid;for(i = 0;i < num;++ i){pthread_create(&pid,NULL,routin,data);pthread_detach(pid);data->mEndlflag[pid] = IDLE;}return 1;}//静态函数,线程实际执行函数void *thread_pool::routin(void *arg){pthread_t pid = pthread_self();thread_pool *data = (thread_pool*)arg;while(1){pthread_mutex_lock(&data->mutex);while(data->curworknum == 0 && data->mEndlflag[pid] != END){pthread_cond_wait(&data->cont,&data->mutex);}if(data->mEndlflag[pid] == END){std::map<pthread_t,STATE>::iterator iter = data->mEndlflag.find(pid);if(iter != data->mEndlflag.end()){data->mEndlflag.erase(iter);}pthread_mutex_unlock(&data->mutex);std::cout<<pid<<"end"<<std::endl;return NULL;}if(data->busy == data->total - 1){if(data->total == MAX_THREAD){pthread_mutex_unlock(&data->mutex);continue;}data->realloc(data->total,data);data->total *= 2;}thread_work *q = data->dwork.front();data->dwork.pop_front();-- data->curworknum;++ data->busy;data->mEndlflag[pid] = BUSY;pthread_mutex_unlock(&data->mutex);q->run();delete q;pthread_mutex_lock(&data->mutex);data->mEndlflag[pid] = IDLE;if((-- data->busy <= data->total / 4) && (data->total != MIN_THREAD)){data->total = data->total / 2;data->release(data->total,data);pthread_mutex_unlock(&data->mutex);pthread_cond_broadcast(&data->cont);}else{pthread_mutex_unlock(&data->mutex);}}}//添加任务int thread_pool::add_work(work_fun fun,char *arg){thread_work *p = new thread_work(fun,arg);bool dosignal = false;pthread_mutex_lock(&this->mutex);this->dwork.push_front(p);dosignal = (this->curworknum == 0);++ this->curworknum;pthread_mutex_unlock(&this->mutex);if(dosignal){pthread_cond_broadcast(&this->cont);}return 1;}//释放整个线程池void thread_pool::destory(){pthread_mutex_lock(&this->mutex);if(!this->shutdown){std::map<pthread_t,STATE>::iterator miter = this->mEndlflag.begin();std::deque<thread_work *>::iterator diter = this->dwork.begin();while(diter != this->dwork.end()){delete (*diter);++ diter;}while(miter != this->mEndlflag.end()){miter->second = END;++ miter;}this->shutdown = true;}pthread_mutex_unlock(&this->mutex);pthread_cond_broadcast(&this->cont);}
/** main.cpp** Created on: 2012-12-7* Author: kym*/#include "thread_work.h"#include "thread_pool.h"#include <unistd.h>#include <deque>void * print ( void * a ){int * b = ( int *) a ;sleep ( 5 );std :: cout <<* b << std :: endl ;return NULL ;}int main (){char * a = new char [ sizeof ( int )];int * b = ( int *) a ;* b = 1 ;thread_pool p ( 4 );p . init ();p . add_work ( print , a );std :: cin >>* b ;p . destory ();while ( 1 );return 0 ;}