一个简单的线程池实现

本文介绍了一个简单的Linux下的线程池实现方式,包括线程池类的设计与任务处理流程,并探讨了如何根据任务负载动态调整线程数量。

一个linux下简单的线程池实现

实现了大部分逻辑,有部分逻辑未实现,只是提供一个思路


线程池类


/*
 * threadpool.h
 *
 *  Created on: Oct 13, 2016
 *      Author: luokun
 */

#ifndef THREADPOOL_H_
#define THREADPOOL_H_
#include <pthread.h>
#include "task.h"
#include <deque>
#include <iostream>
using namespace std;
class CThreadpool
{
public:
      CThreadpool();
      ~CThreadpool();

      deque <CTask*> m_deque;


      bool shutdown;    //线程池启动标识
      int maxThreadsize; //最大线程数
      int minThreadsize; //最小线程数
      int curentThreadsize; //当前线程数
      int curentTasksize;  //当前任务数
      int maxTasksize;     //最大任务数
      int killThreadsize;  //需要杀掉的线程数



      pthread_mutex_t lockthread;  //互斥锁,用来线程控制
      pthread_cond_t q_not_empty;  //信号,当任务为空,突然为空,发送次信号,让线程处理
      pthread_cond_t q_not_full;   //信号,当任务满了停止添加任务,当任务可以重新增加,发送次信号

      pthread_t* threads;
      pthread_t pids; //线程id;

      void addTaskIntoPool(CTask* task);
      void poolInit(int useThreadsize);


private:

      void destoryPool();

};

#endif /* THREADPOOL_H_ */

</pre><pre name="code" class="cpp">
#include "threadpool.h"
#include <stdio.h>
#include <unistd.h>

const int DEFULT_TIME = 30;

CThreadpool::CThreadpool()
{
	shutdown = false;
	maxTasksize = 1000;      //最大任务数 为了测试,调小点
	maxThreadsize = 20;   //正常情况所通过配置来设置
	minThreadsize = 5;
	curentThreadsize  = 0;
	curentTasksize = 0;
	killThreadsize = 0;

	threads = new pthread_t[maxThreadsize];



	pthread_mutex_init(&lockthread,NULL);
	pthread_cond_init(&q_not_empty,NULL);
	pthread_cond_init(&q_not_full,NULL);

}

CThreadpool::~CThreadpool()
{
	//destoryPool(); //资源释放函数
}

void CThreadpool::addTaskIntoPool(CTask* task)
{
    pthread_mutex_lock(&lockthread);  //任务处理锁

    while(curentTasksize >= maxTasksize && !shutdown)//最大处理数量已经满了,不在添加任务
    {
        pthread_cond_wait(&q_not_full,&lockthread);
    }
    if(shutdown)
    {
    	//线程池关闭了
    	pthread_mutex_unlock(&lockthread);
    	return;
    }

    //操作任务,加任务锁
	m_deque.push_back(task);  //位部添加
	curentTasksize++;

	pthread_cond_signal(&q_not_empty); //提示其他线程,由数据可以处理
	pthread_mutex_unlock(&lockthread);


}
//任务处理
void *doworkTask(void* args)
{
	CThreadpool* pool = (CThreadpool*)args;
    while(true)
    {
    	pthread_mutex_lock(&(pool->lockthread));  //线程处理锁
    	while( 0 == pool->curentTasksize && !(pool->shutdown))  //没有数据
    	{
            pthread_cond_wait(&(pool->q_not_empty),&(pool->lockthread));
            if(pool->killThreadsize > 0)
            {
                 pool->killThreadsize--;
                 pool->curentThreadsize--;
                 pthread_mutex_unlock(&(pool->lockthread));  //线程处理锁
                 pthread_exit(NULL);
            }
    	}

        if(pool->shutdown)
        {
        	pthread_mutex_unlock(&(pool->lockthread));
        	pthread_exit(NULL);
        }

        CTask* a  = (pool->m_deque).at(0) ; //取得最先放入队列的数据
        deque <CTask*>::iterator it = pool->m_deque.begin();
        it = pool->m_deque.erase(it);
        pthread_cond_signal(&(pool->q_not_full));
        pool->curentTasksize--;
    	pthread_mutex_unlock(&(pool->lockthread));

        a->run();
        delete a;
    }
}

//线程管理
void* domangeThread(void* args)
{
	CThreadpool* pool = (CThreadpool*)args;

	while(!pool->shutdown)
	{
		sleep(DEFULT_TIME);



    	pthread_mutex_lock(&(pool->lockthread));  //任务处理锁
    	int curThreadsize = pool->curentThreadsize;
    	int curTasksize = pool->curentTasksize;
    	cout<<curThreadsize<<endl;


    	int killthreadsize   = curThreadsize- curTasksize/3;
    	int addThreadsize = curTasksize/6 -curThreadsize;
    	if(killthreadsize> 0)
    	{
             //需要杀掉的进程所
    		int minUsesize = curTasksize/3;
    		if(curTasksize/3 < pool->minThreadsize)
    		{
    			minUsesize = pool->minThreadsize;
    		}

    		pool->killThreadsize = curThreadsize - minUsesize;

    	}
    	else if(addThreadsize> 0)
    	{
    		int maxUsesize = curTasksize/3;
    		if(maxUsesize > pool->maxThreadsize)
    		{
    			maxUsesize = pool->maxThreadsize;
    		}

    		addThreadsize = maxUsesize - curThreadsize;


    	    for(int i = 0; i < addThreadsize; i++)
    	    {
    	    	pthread_create(&(pool->threads[pool->curentThreadsize]),NULL,doworkTask,(void*)pool);
    	    	pool->curentThreadsize++;
    	    }
    	}


    	else
    	{

    	}

    	pthread_mutex_unlock(&(pool->lockthread));

	}
}

void CThreadpool::poolInit(int useThreadsize)
{
    for(int i = 0; i < useThreadsize; i++)
    {
    	pthread_create(&threads[i],NULL,doworkTask,(void*)this);
    	curentThreadsize++;
    }

    pthread_t mangethid;
    pthread_create(&mangethid,NULL,domangeThread,(void*)this);

}







任务类是个父类,可以添加子类实现

/*
 * Task.h
 *
 *  Created on: Oct 14, 2016
 *      Author: luokun
 */

#ifndef TASK_H_
#define TASK_H_

class CTask
{
public:
     CTask(){};
     int m;
     virtual ~CTask(){};
     virtual void run();

};


#endif /* TASK_H_ */



/*
 * task.cpp
 *
 *  Created on: Oct 14, 2016
 *      Author: luokun
 */

#include "task.h"
#include <iostream>
#include <pthread.h>
using namespace std;



void CTask::run()
{
   //cout<<this->m<<endl;
	sleep(2);

}

#include "threadpool.h"
#include "task.h"
int main()
{
    CThreadpool test;
    test.poolInit(10);


    int s = 0;
    while(true)
    {
       CTask* task = new CTask();  //这里可以通过子类来处理不同的任务
       task->m = s;
       test.addTaskIntoPool(task);
       s++;
    }
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值