zoukankan      html  css  js  c++  java
  • 高性能服务器编程半同步/半反应堆的线程池模板

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

    缺点:

    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 &lt; thread_number; ++i )
    {
        printf( "create the %dth thread
    ", 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指针

    高性能服务器编程半同步/半反应堆的线程池模板
    无欲则刚 关心则乱
  • 相关阅读:
    Windows 文件过滤驱动经验总结
    修改disk驱动监控文件系统的IO特征
    STATUS_MUTANT_NOT_OWNED
    网络连接存储(Networkattached storage,NAS)简介
    缓存管理器
    LINUX softraid 管理
    过滤驱动的问题自我总结
    Visual Studio 2010 (vs2010) 全屏功能的改进
    在 .NET 中,后++ 运算符产生的一个小问题
    在 Visual Studio 2008 中去掉某些文件的只读属性
  • 原文地址:https://www.cnblogs.com/xjyxp/p/11488908.html
Copyright © 2011-2022 走看看