c++线程池简单实现

文章提供了一个C++实现的简单线程池类`CThreadPool`,包括线程对象`CThreadItem`,使用了`std::thread`,`std::mutex`和`std::condition_variable`进行线程同步。线程池可以创建指定数量的工作线程,将任务放入消息队列并通知工作线程处理,支持停止所有线程。在主函数中,创建线程池并模拟向队列添加任务,然后等待一段时间后停止线程池。
摘要由CSDN通过智能技术生成

c++线程池简单实现

#pragma once
#include<iostream>
#include<thread>
#include<mutex>
#include<condition_variable>
#include<vector>
#include<cstring>
#include<algorithm>
#include<string>
#include<Windows.h>
#include<time.h>

using namespace std;
class CThreadPool {

public:
	class CThreadItem {
	public:
		bool					is_running;
		thread					mythread;
		CThreadPool* p_This;
		//构造函数
		CThreadItem(CThreadPool* pthis) :p_This(pthis), is_running(false) {
			cout << this << endl;
		}
		//析构函数
		~CThreadItem() {}
	};

	static bool					m_shutdown;          //线程退出标志,false不退出,true退出
	int m_iThreadNum;

	CThreadPool();
	~CThreadPool();
	void Stopall();
	void Call();
	void Creat(int);
	void* ThreadFun(void*);

	void inMsgRecvQueueAndSignal(vector<int> resh, int nImageWidth, int nImageHeight, int start, int end);
	bool a = false;
	int b = 0;
	//储存数据
	vector<vector<int>>				m_msg;
	vector<vector<int>>             msg;
	vector<int> Width;
	vector<int> Height;
	vector<int> Start;
	vector<int> End;
	vector<vector<int>> index;
	///
	vector<CThreadItem*>		m_threadVec;

	std::mutex						m_pthreadMutex;
	condition_variable			m_pthreadCond;
};
bool CThreadPool::m_shutdown = false;
CThreadPool::CThreadPool() {

}
CThreadPool::~CThreadPool() {
lblfor1:
	for (auto iter = m_msg.begin(); iter != m_msg.end(); ++iter) {
		vector<int> buf = *iter;
		delete &buf;
		goto lblfor1;
	}
}

void CThreadPool::Call() {
	m_pthreadCond.notify_one();
}
//停止工作线程
void CThreadPool::Stopall() {
	if (m_shutdown) {
		return;
	}

	m_shutdown = true;
	m_pthreadCond.notify_all();

	for (auto iter = m_threadVec.begin(); iter != m_threadVec.end(); iter++)
	{
		(*iter)->mythread.join();
	}

	cout << "------\n";

	for (auto iter = m_threadVec.begin(); iter != m_threadVec.end(); iter++)
	{
		CThreadItem* tmp = *iter;
		cout << tmp << endl;
		delete tmp;
	}
	cout << "------\n";

	m_threadVec.clear();
	printf("cthreadpool::stopall()成功返回,线程池中线程全部正常结束!\n");
	return;
}
//创建线程池
void CThreadPool::Creat(int num)
{
	//工作线程个数
	m_iThreadNum = num;
	CThreadItem* pNew;
	cout << "------\n";
	//创建工作线程
	for (int i = 0; i < m_iThreadNum; ++i)
	{
		pNew = new CThreadItem(this);
		pNew->mythread = thread(&CThreadPool::ThreadFun, this, pNew);
		m_threadVec.push_back(pNew);
	}
	cout << "------\n";
lblfor:
	for (auto iter = m_threadVec.begin(); iter != m_threadVec.end(); ++iter)
	{		
		if (!((*iter)->is_running))
		{
			cout << "iter:" << *iter << endl;
			goto lblfor;
		}
	}


	cout << " Creat ok\n";
}
//工作线程
void* CThreadPool::ThreadFun(void* Data)
{
	CThreadItem* pThread = static_cast<CThreadItem*>(Data);
	CThreadPool* pool = pThread->p_This;
	//std::mutex m;
	while (1)
	{
		
		std::unique_lock<std::mutex> lck(m_pthreadMutex);
		//判断所有工作线程是否完全结束工作
		if (b == 10) {
			b = 0;
			a = true;
			A.push_back(msg.size());
			msg.clear();
		}
		else {
			a = false;
		}
		//让每个线程轮流取数据
		while (m_msg.size() == 0 && m_shutdown == false)
		{

			if (!pThread->is_running) {
				pThread->is_running = true;
			}
			m_pthreadCond.wait(lck);
		}

		if (m_shutdown)
		{
			lck.unlock();
			return (void*)0;
		}
		vector<int> jobbuf = m_msg.front();
		m_msg.erase(m_msg.begin());
		cout << "get:" << jobbuf[0] + 10 << endl;
		b = b + 1;
		msg.push_back({ jobbuf[0] + 10,jobbuf[1] });
		lck.unlock();
	}
	
	return (void*)0;
}
//加载数据
void CThreadPool::inMsgRecvQueueAndSignal(vector<int> rhs, int nImageWidth, int nImageHeight, int start, int end)
{
	m_pthreadMutex.lock();
	m_msg.push_back(rhs);
	Width.push_back(nImageWidth);
	Height.push_back(nImageHeight);
	Start.push_back(start);
	End.push_back(end);
	m_pthreadMutex.unlock();
	Call();
}





int main()
{
	// 检测是否有内存泄漏
	_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
	CThreadPool* pCThreadPool = new CThreadPool();
	pCThreadPool->Creat(4);
	vector<vector<int>> Mess = { {10,10},{11,11},{12,12},{13,13},{14,14},{15,15},{16,16},{17,17},{18,18},{19,19} };
	std::mutex lck;
	for (int j = 0; j < 10; j++) {
		for (int i = 0; i < 10; ++i)
		{
			vector<int> mess;
			mess = Mess[i];
			pCThreadPool->inMsgRecvQueueAndSignal(mess,1,1,1,1);
		}
		
		while (1) {
			lck.lock();
			
			if (A.size()!=0) {
				cout << "hello" << A[0] << endl;
				A.erase(A.begin());
				lck.unlock();
				Sleep(100);
				//pCThreadPool->m_pthreadCond.wait(lck1);
				//pCThreadPool->m_pthreadCond.notify_all();
				//lck.unlock();
				//cout <<"hello" << pCThreadPool->a<< endl;
				break;
			}
			lck.unlock();
			
		}
	}
	Sleep(5000);
	pCThreadPool->Stopall();
	delete pCThreadPool;
	getchar();
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值