1. #ifndef THREADPOOL_H
    2. #define THREADPOOL_H
    3. #include <list>
    4. #include <cstdio>
    5. #include <exception>
    6. #include <pthread.h>
    7. #include "locker.h"
    8. // 线程池类,将它定义为模板类是为了代码复用,模板参数T是任务类
    9. template<typename T>
    10. class threadpool {
    11. public:
    12. /*thread_number是线程池中线程的数量,max_requests是请求队列中最多允许的、等待处理的请求的数量*/
    13. // 默认初始化线程池:池中线程数为8个,请求最多为10000
    14. threadpool(int thread_number = 8, int max_requests = 10000);
    15. ~threadpool(); // 析构
    16. bool append(T* request); // 添加任务
    17. private:
    18. /*工作线程运行的函数,它不断从工作队列中取出任务并执行之*/
    19. static void* worker(void* arg);
    20. void run();
    21. private:
    22. int m_thread_number; // 线程的数量
    23. pthread_t * m_threads; // 描述线程池的数组,大小为m_thread_number
    24. int m_max_requests; // 请求队列中最多允许的、等待处理的请求的数量
    25. std::list< T* > m_workqueue; // 请求队列
    26. locker m_queuelocker; // 保护请求队列的互斥锁
    27. sem m_queuestat; // 是否有任务需要处理
    28. bool m_stop; // 是否结束线程
    29. };
    30. // 构造函数,初始化线程池
    31. template< typename T >
    32. threadpool< T >::threadpool(int thread_number, int max_requests) :
    33. m_thread_number(thread_number), m_max_requests(max_requests), m_stop(false), m_threads(NULL) {
    34. // 判断输入参数是否合法
    35. if((thread_number <= 0) || (max_requests <= 0) ) {
    36. throw std::exception();
    37. }
    38. m_threads = new pthread_t[m_thread_number];
    39. if(!m_threads) {
    40. throw std::exception();
    41. }
    42. // 创建thread_number 个线程,并将他们设置为脱离线程
    43. for ( int i = 0; i < thread_number; ++i ) {
    44. printf( "create the %dth thread\n", i);
    45. if(pthread_create(m_threads + i, NULL, worker, this ) != 0) { // 创建失败抛出异常
    46. delete[] m_threads;
    47. throw std::exception();
    48. }
    49. if(pthread_detach( m_threads[i])) { // 分离失败抛出异常
    50. delete[] m_threads;
    51. throw std::exception();
    52. }
    53. }
    54. }
    55. // 析构
    56. template< typename T >
    57. threadpool< T >::~threadpool() {
    58. delete[] m_threads;
    59. m_stop = true;
    60. }
    61. template< typename T >
    62. bool threadpool< T >::append( T* request )
    63. {
    64. // 操作工作队列时一定要加锁,因为它被所有线程共享
    65. m_queuelocker.lock();
    66. if ( m_workqueue.size() > m_max_requests ) { // 如果队列已满,就不添加了,直接返回
    67. m_queuelocker.unlock();
    68. return false;
    69. }
    70. m_workqueue.push_back(request); // 将任务添加进队列
    71. m_queuelocker.unlock(); // 解锁
    72. m_queuestat.post(); // 信号量增加1
    73. return true;
    74. }
    75. // 开始工作
    76. template< typename T >
    77. void* threadpool< T >::worker( void* arg )
    78. {
    79. threadpool* pool = ( threadpool* )arg;
    80. pool->run();
    81. return pool;
    82. }
    83. // 从线程池中取线程去处理请求
    84. template< typename T >
    85. void threadpool< T >::run() {
    86. while (!m_stop) { // 当线程未收到结束信号
    87. m_queuestat.wait(); // 信号量-1,否则阻塞
    88. m_queuelocker.lock(); // 要操作队列,先加锁
    89. if (m_workqueue.empty()) { // 如果队列为空直接解锁等待请求
    90. m_queuelocker.unlock();
    91. continue;
    92. }
    93. T* request = m_workqueue.front(); // 有请求就取一个线程去处理
    94. m_workqueue.pop_front();
    95. m_queuelocker.unlock();
    96. if ( !request ) {
    97. continue;
    98. }
    99. request->process(); // 该子线程开始执行任务!!!
    100. }
    101. }
    102. #endif