LINUX编程学习笔记之有界线程池

学习复杂的系统设计以及提高程序处理效率的时候我们无法避免会碰到多线程的问题。那么其实对于复杂的系统,线程之间的切换给系统带来的额外开销是相当巨大的。那么这个时候,资源池的设计理念就隐约浮现在我们的脑海里了。何为线程池呢?简而言之就是我们自己定义一个包装类,将我们所需要的线程以及它的一套资源在我们的程序初始化时一次性申请,交与应用程序使用,当我们的程序在退出之前再调用相应的析构方式去释放之前的资源池即可。

那么一个简单的、通用的线程池该如何设计呢?

1、线程池的属性。

    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; 状态标志位

    线程池的初始化过程如下:

   

申明:
threadpool( int thread_number = 8, int max_requests = 1000 );

实现:
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();
        }
    }
}

2、线程池的工作队列,对有限的线程,同一时间只能处理有限的任务,那么未来得及处理的request把它放到一个等待队列里,等待后续线程的处理。常见的操作如下:

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();
    return true;
}
 

3、线程池的启动函数的实现,这里主要是考虑到线程安全的问题,具体实现如下:

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();
        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();
    }
}


4、process()函数的实现,这个就要针对这种线程池的工作具体内容去实现,可以设计为纯虚函数,在继承该通用线程池时去具体实现。


5、线程池的资源释放,析构函数:

template< typename T >
threadpool< T >::~threadpool()
{
    delete [] m_threads;
    m_stop = true;
}


看了其他的一些文章,其实对于线程池的设计还有很多方面可以改善和优化。比如工作线程的动态增加、减小,包括他们的处理机制等。后续都是我们的学习研究重点。其实本文主要讨论了一个通用的有界线程池的基本实现,希望给初学者带来一定的帮助。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值