高性能服务器编程半同步/半反应堆的线程池模板

线程池实现的半同步/半反应堆模型,使用工作队列实现主线程和工作线程的通信,利用加锁(互斥锁、信号量)实现同步操作,有点类似于“生产者-消费者”的通信模型。

缺点:

1、利用锁实现工作线程对工作队列的访问,锁的加入降低了系统效率;

2、必须保证所有客户请求都是无状态的,因为同一个连接上的不同请求可能会由不同线程处理;

优点:

1、主线程和工作线程间没有耦合关系,通用性强;

2、利用操作系统自带的信号量唤醒机制,工作线程基本可以实现公平调度(工作线程负载均衡),程序实现简单。

#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 );//thread_number为线程池线程数,max_requests为工作队列中最大的等待数
    ~threadpool();
    bool append( T* request );//向工作队列中添加请求,需要加锁操作

private:
    static void* worker( void* arg );//工作线程的运行函数,不断从工作队列中取出任务并执行,同样需要加锁操作
    void run();

private:
    int m_thread_number;
    int m_max_requests;
    pthread_t* m_threads;//线程池数据
    std::list< T* > m_workqueue;//工作队列,FIFO
    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 )
        {
            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,有1个工作线程会被唤醒
    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();//信号量-1
        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

关于线程运行函数:

在C++程序中使用pthread_create函数时,第3个参数必须指向一个静态函数,不能是一个对象的成员方法。因此在threadpool中要把线程运行函数定义为static。同时,要在一个静态函数中使用类的动态成员(成员函数和成员变量),可以将类的对象作为参数传递给该静态函数,通过引用该对象调用方法或访问变量。此处在pthread_create创建线程时传入this指针

高性能服务器编程半同步/半反应堆的线程池模板

猜你喜欢

转载自www.cnblogs.com/xjyxp/p/11488908.html