线程池C++

threadpool.h

#pragma once
#include <thread>
#include <mutex>
#include <queue>
#include <vector>
#include <condition_variable>
using namespace std;

using callback = void(*)(void*);

class Task
{
public:
	callback function;
	void* arg;
public:
	Task(callback f, void* arg) { function = f; this->arg = arg; }
};

class ThreadPool
{
public:
	ThreadPool(int min, int max);
	void Add(void(*func)(void*), void* arg);
	void Add(Task task);
	int Busynum();
	int Alivenum();
	~ThreadPool();

private:
	queue<Task> taskQ;
	thread managerID;   //管理者线程ID
	vector<thread> threadIDs;   //
	int minNum;   //最小线程数
	int maxNum;   //最大线程数
	int busyNum;   //忙的线程数
	int liveNum;    //存活的线程数
	int exitNum;    //要销毁的线程数

	mutex mutexPool;    //整个线程池的锁
	condition_variable cond;     //任务队列是否为空
	bool shutdown;    //是否销毁线程池,销毁为1,不销毁为0
	static void manager(void* arg);   //管理者线程
	static void worker(void* arg);   //工作线程
};

threadPool.cpp

#include <iostream>
#include <string.h>
#include<Windows.h>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <stdlib.h>
#include "threadPoll.h"

const int NUMBER = 2;

ThreadPool::ThreadPool(int min, int max)
{
	do
	{
		minNum = min;
		maxNum = max;
		busyNum = 0;
		liveNum = min;
		exitNum = 0;

		shutdown = false;
		managerID = thread(manager, this);

		threadIDs.resize(max);
		for (int i = 0; i < min; ++i)
		{
			threadIDs[i] = thread(worker, this);
		}
		return;
	} while (0);
}

ThreadPool::~ThreadPool()
{
	shutdown = true;
	//阻塞回收管理者线程
	if (managerID.joinable()) managerID.join();
	//唤醒阻塞的消费者线程
	cond.notify_all();
	for (int i = 0; i < maxNum; ++i)
	{
		if (threadIDs[i].joinable()) threadIDs[i].join();
	}
}

void ThreadPool::Add(Task t)
{
	unique_lock<mutex> lk(mutexPool);
	if (shutdown)
	{
		return;
	}
	//添加任务
	taskQ.push(t);
	cond.notify_all();
}

void ThreadPool::Add(callback f, void* a)
{
	unique_lock<mutex> lk(mutexPool);
	if (shutdown)
	{
		return;
	}
	//添加任务
	taskQ.push(Task(f, a));
	cond.notify_all();
}

int ThreadPool::Busynum()
{
	mutexPool.lock();
	int busy = busyNum;
	mutexPool.unlock();
	return busy;
}

int ThreadPool::Alivenum()
{
	mutexPool.lock();
	int alive = liveNum;
	mutexPool.unlock();
	return alive;
}

void ThreadPool::worker(void* arg)
{
	ThreadPool* pool = static_cast<ThreadPool*>(arg);
	while (true)
	{
		unique_lock<mutex> lk(pool->mutexPool);
		//当前任务队列是否为空
		while (pool->taskQ.empty() && !pool->shutdown)
		{
			//阻塞工作线程
			pool->cond.wait(lk);
			if (pool->exitNum > 0)
			{
				pool->exitNum--;
				if (pool->liveNum > pool->minNum)
				{
					pool->liveNum--;
					cout << "threadid: " << std::this_thread::get_id() << " exit......" << endl;
					lk.unlock();
					return;
				}
			}
		}

		//判断线程池是否关闭了
		if (pool->shutdown)
		{
			cout << "threadid: " << std::this_thread::get_id() << "exit......" << endl;
			return;
		}

		//从任务队列中去除一个任务
		Task task = pool->taskQ.front();
		pool->taskQ.pop();
		pool->busyNum++;
		//解锁
		lk.unlock();

		cout << "thread: " << std::this_thread::get_id() << " start working..." << endl;
		task.function(task.arg);
		//(*task.function)(task.arg);
		free(task.arg);
		task.arg = nullptr;

		cout << "thread: " << std::this_thread::get_id() << " end working..." << endl;
		lk.lock();
		pool->busyNum--;
		lk.unlock();
	}
}

void ThreadPool::manager(void* arg)
{
	ThreadPool* pool = static_cast<ThreadPool*>(arg);
	while (!pool->shutdown)
	{
		//每隔3秒检测一次
		Sleep(3000);

		//取出线程池中任务的数量和当前线程的数量
		unique_lock<mutex> lk(pool->mutexPool);
		int queuesize = pool->taskQ.size();
		int livenum = pool->liveNum;
		int busynum = pool->busyNum;
		lk.unlock();

		//添加线程
		//任务的个数>存活的线程个数 && 存活的线程数 < 最大线程数
		if (queuesize > livenum && livenum < pool->maxNum)
		{
			lk.lock();
			int count = 0;
			for (int i = 0; i < pool->maxNum && count < NUMBER && pool->liveNum < pool->maxNum; ++i)
			{
				if (pool->threadIDs[i].get_id() == thread::id())
				{
					cout << "Create a new thread..." << endl;
					pool->threadIDs[i] = thread(worker, pool);
					count++;
					pool->liveNum++;
				}
			}
			lk.unlock();
		}

		//销毁线程
		//忙的线程*2 < 存活的线程数 && 存活的线程数 >  最小的线程数
		if (busynum * 2 < livenum && livenum > pool->minNum)
		{
			lk.lock();
			pool->exitNum = NUMBER;
			lk.unlock();
			//让工作的线程自杀
			for (int i = 0; i < NUMBER; ++i) pool->cond.notify_all();
		}
	}
}

main

//main.cpp
#include "threadPoll.h"
#include <iostream>
#include <stdlib.h>
#include<Windows.h>

void taskFunc(void* arg)
{
	int num = *(int*)arg;
	cout << "thread: " << std::this_thread::get_id() << ", number=" << num << endl;
	Sleep(1000);
}

int main()
{
	ThreadPool pool(5, 10);
	int i;
	for (i = 0; i < 100; ++i)
	{
		int* num = new int(i + 100);
		pool.Add(taskFunc, (void*)num);
	}
	for (; i < 200; ++i)
	{
		Sleep(1000);
		int* num = new int(i + 100);
		pool.Add(taskFunc, (void*)num);
	}
	return 0;
}

参考:
1,https://subingwen.cn/linux/threadpool-cpp/#2-2-%E7%B1%BB%E5%AE%9A%E4%B9%89
2,https://blog.youkuaiyun.com/qq_36831356/article/details/120260823

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值