半同步/半反应堆线程池实现
2015-07-02 22:58
459 查看
半同步/半反应堆线程池:主线程监听listen socket和接收到的所有连接socket,当有客户端请求任务时,将任务对象插入到工作任务对象中;等待在任务队列上的工作线程通过竞争来取得任务对象并处理之。其中的工作任务队列完成了主线程与工作线程之间的解耦,但是由于同一客户连接的任务请求可能由不同的线程来处理,所以这要求所有的客户请求是无状态的。
具体细节,尽在代码中:
具体细节,尽在代码中:
#ifndef THREADPOOL_H #define THREADPOOL_H #include <list> #include <cstdio> #include <exception> #include <pthread.h> #include "locker.h" //线程池类:模板参数为任务对象 template< typename T > class threadpool { public: threadpool( int thread_number = 8, int max_requests = 10000 ); ~threadpool(); bool append( T* request );//主线程向任务队列中添加任务 private: //注意:(当把线程函数封装在类中,this指针会作为默认的参数被传进函数中,从而和线程函数参数(void*)不能匹配。 //解决: 线程函数作为静态函数,因为在C++中静态函数没有this指针) static void* worker( void* arg ); //工程线程运行函数:调用run函数,从工作队列中取出任务,进而调用任务对象的处理函数进程任务处理 void run(); private: int m_thread_number; //线程池中的线程数 int m_max_requests;//最大请求数量 pthread_t* m_threads;//线程数组 std::list< T* > m_workqueue;//请求队列 locker m_queuelocker;//请求队列的互斥锁 sem m_queuestat;//信号量:是否有任务需要处理 bool m_stop; //是否需要结束线程 }; template< typename T > threadpool< T >::threadpool( int thread_number, int max_requests ) : m_thread_number( thread_number ), m_max_requests( max_requests ), m_stop( false ), m_threads( NULL ) { if( ( thread_number <= 0 ) || ( max_requests <= 0 ) ) { throw std::exception(); } m_threads = new pthread_t[ m_thread_number ]; if( ! m_threads ) { throw std::exception(); } for ( int i = 0; i < thread_number; ++i ) { printf( "create the %dth thread\n", i ); if( pthread_create( m_threads + i, NULL, worker, this ) != 0 )//创建线程(注意线程函数为静态成员函数,且将this指针作为参数,以访问进程池的成员变量) { delete [] m_threads; throw std::exception(); } if( pthread_detach( m_threads[i] ) ) //将工作线程设置为脱离线程(当脱离线程退出时,系统会自动回收该线程的资源),否则需要其他线程来回收该线程的资源 { delete [] m_threads; throw std::exception(); } } } template< typename T > threadpool< T >::~threadpool() { delete [] m_threads; m_stop = true; } //主线程将新任务加入任务队列 template< typename T > bool threadpool< T >::append( T* request ) { m_queuelocker.lock();//由于任务队列被所有线程共享,因此需要先加锁 if ( m_workqueue.size() > m_max_requests ) { m_queuelocker.unlock(); return false; } m_workqueue.push_back( request ); m_queuelocker.unlock(); m_queuestat.post();//将信号量+1,如果大于0,则唤醒等待该信号量的工作线程 return true; } //线程函数 template< typename T > void* threadpool< T >::worker( void* arg ) { threadpool* pool = ( threadpool* )arg; pool->run(); return pool; } //工作线程的处理函数:从任务队列中取出任务对象,然后调用任务对象的逻辑处理函数完成客户请求任务 template< typename T > void threadpool< T >::run() { while ( ! m_stop ) { m_queuestat.wait();//等待信号量大于0 m_queuelocker.lock();//锁住任务队列 if ( m_workqueue.empty() ) { m_queuelocker.unlock(); continue; } T* request = m_workqueue.front();//取出任务对象 m_workqueue.pop_front(); m_queuelocker.unlock(); if ( ! request ) { continue; } request->process();//处理客户任务 } } #endif
相关文章推荐
- Topshelf创建Windows服务
- linux下的rbenv和rails安裝
- 在windows下安装apidocjs
- 精和泛的一点思考(跨学科思维)
- Cocos2D-x工程目录介绍
- 连载《一个程序猿的生命周期》-22.缺了一条腿的公司
- 女coder的乐园---14个专门面向女性开发人员的编程社区
- Android Auto CompleteTexview
- 使用新版(7.0.0以后)的butterknife注意事项
- 连载《一个程序猿的生命周期》-22.缺了一条腿的公司
- 【HM】第7课:Servlet
- 视频编解码学习(1):YUV格式学习
- 移动App应用开发的五大流行趋势
- CodeForces 368B Sereja and Suffixes
- android中list数据与json互转
- Eclipse for Mac 常用快捷
- 149.根据公式计算值
- git命令
- Python 2.7.9 Demo - 获取调用的参数
- 作业2.30