Linux pthread 实现线程池学习


#pragma comment(lib, "pthreadVC2.lib")
#include "pthread.h"
#include <queue>
#include <map>
using namespace std;

#define I_MAX_THREAD 8
#define I_MAX_TASK 8
map<unsigned long, int> thread_status;//存储线程状态
typedef void(*lpFunc)(void *param);//函数指针
class thread_pool;//前置声明
class task;//前置声明
thread_pool *pool;//全局指针

//定义线程池类
class thread_pool
{
public:
	int max_thread;//最大线程数
	int max_task;//最大任务数
	bool blstop;//标记线程是否终止
	vector<pthread_t*> vecthread;//存储线程
	queue<task*> qtask;//任务队列
	pthread_mutex_t* mutex;//互斥量
	pthread_cond_t*  cond;//条件变量
	thread_pool(int imax_thread, int imax_task);
	thread_pool();
	~thread_pool();

	int addTask(void *func, void *param);//添加任务
private:

};

//定义任务类
class task
{
public:
	void *func;
	void *param;
};
//存储线程状态
void set_thread_status(int status)
{
	pthread_t t = pthread_self();//获取当前运行的线程ID
	unsigned long tid = (unsigned long)&t;
	thread_status[tid] = status;
}
//获取线程状态
int get_thread_status(unsigned long tid)
{
	for (map<unsigned long, int>::iterator it = thread_status.begin(); it != thread_status.end(); it++)
	{
		if (it->first == tid)
			return it->second;
	}
	return 0;
}
//获取线程ID
unsigned long get_threadID()
{
	pthread_t t = pthread_self();

	return (unsigned long)&t;
}

//线程执行函数
void* routine(void *args)
{
	thread_pool *pth = (thread_pool*)args;

	while (true)
	{
		pthread_mutex_lock(pth->mutex);
		while (pth->qtask.empty()&& !pth->blstop)//任务队列为空时,阻塞等待
		{
			pthread_cond_wait(pth->cond, pth->mutex);
		}

		if (pth->blstop)//线程终止
		{
			pthread_mutex_unlock(pth->mutex);
			break;
		}

		//满足条件被唤醒,执行任务
		task* t = pth->qtask.front();
		pth->qtask.pop();//执行过的任务需要从队列删除
		pthread_mutex_unlock(pth->mutex);//执行线程任务时,释放线程锁
		set_thread_status(1);
		((lpFunc)t->func)(t->param);
		set_thread_status(0);
	}
	return NULL;
}
//线程池类带参构造函数
thread_pool::thread_pool(int imax_thread, int imax_task):max_thread(imax_thread),max_task(imax_task)
{
	blstop = false;
	mutex = new pthread_mutex_t();
	cond = new pthread_cond_t();
	pthread_mutex_init(mutex,NULL);
	pthread_cond_init(cond,NULL);

	for (int i = 0; i < max_thread;++i)
	{
		vecthread.push_back(new pthread_t());
		pthread_create(vecthread[i], NULL, routine, this);
	}
}
//线程池类析构函数
thread_pool::~thread_pool()
{
	//唤醒所有线程
	pthread_cond_broadcast(cond);

	for (int i = 0; i < max_thread;++i)
	{
		pthread_join(*vecthread[i],NULL);
		delete vecthread[i];
	}

	vecthread.clear();
	thread_status.clear();

	while (!qtask.empty())
	{
		delete qtask.front();
		qtask.pop();
	}

	pthread_mutex_destroy(mutex);
	pthread_cond_destroy(cond);
	mutex = NULL;
	cond = NULL;
}

//添加任务到队列
int thread_pool::addTask(void *func, void *param)
{
	task *pt = new task();
	pt->func = func;
	pt->param = param;

	pthread_mutex_lock(this->mutex);
	if (qtask.size() >= max_task)
	{
		pthread_mutex_unlock(this->mutex);
		return -1;
	}
	qtask.push(pt);
	pthread_mutex_unlock(this->mutex);
	pthread_cond_signal(cond);
	return 1;
}

int run_in_threadPool(void *func, void *param)
{
	if (pool == NULL)
	{
		pool = new thread_pool(I_MAX_THREAD, I_MAX_TASK);
	}
	return pool->addTask(func, param);
}

void exit_all_thread()
{
	if (pool == NULL)
	{
		return;
	}

	pool->blstop = true;
	delete pool;
	pool = NULL;
}

//不使用线程池情况
class singleTask
{
public:
	singleTask();
	~singleTask();
	
	void* func;
	void* param;
private:

};

int task_Num = 0;
void * routine_single(void *args)
{
	pthread_detach(pthread_self());
	sched_yield();//让出资源,由系统调度
	task_Num++;

	singleTask *sgTask = (singleTask*)args;

	lpFunc func = (lpFunc)sgTask->func;
	func(sgTask->param);
	task_Num--;

	delete sgTask;

	return NULL;
}

int run_in_thread(void *func, void* param)
{
	int iRet = 0;

	if (task_Num >I_MAX_TASK)
	{
		return -2;
	}

	pthread_t tid;
	singleTask *sgTask = new singleTask();
	sgTask->func = func;
	sgTask->param = param;

	int err = pthread_create(&tid, NULL, routine_single, (void*)sgTask);
	if (err != 0)
	{
		iRet = -1;
	}

	return iRet;
}

int get_run_thread_size()
{
	return task_Num;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值