c++多线程 线程池的实现

目录

1,简易线程池

XThreadPool.cpp

javascript 复制代码
#include<iostream>
#include<thread>
#include<mutex>
#include<vector>
#include<list>

#include"XTasks.h"


class XThreadPool
{
public:
	void  Init(int num);    //初始化线程数量
	void  Start();          //启动所有线程,必须先调用Init();
	void  AddTask(XTask* task);  //添加任务
	XTask* GetTask();       //获取任务
private:
	void Run();             //线程池中线程的入口函数

private:
	int thread_num = 0;          //线程数量
	std::mutex m_mux;
	std::vector<std::thread*>  m_threads;
	std::list<XTask*>  m_tasks;  //任务列表
	bool isInit = false;         //判断有没有初始化线程池
	std::condition_variable cv;
};


//初始化线程数量
void  XThreadPool::Init(int num)
{
	unique_lock<mutex> lock(m_mux);
	this->thread_num = num;
	isInit = true;
	cout << "threadpool Init" << num << endl;
}

//启动所有线程,必须先调用Init();
void  XThreadPool::Start()
{
	unique_lock<mutex> lock(m_mux);
    if(!isInit)
    { 
		cout << "please  Init thread_num" << endl;
    }
	if (!m_threads.empty())
	{
		cerr << "thread poll has start!" << endl;
	}

	//启动线程
	for (int i = 0;i< thread_num;i++)
	{
		thread* th = new  thread(&XThreadPool::Run, this);
		m_threads.push_back(th);
	}
}

//添加任务
void  XThreadPool::AddTask(XTask* task)
{
	unique_lock<mutex> lock(m_mux);
	m_tasks.push_back(task);
	cv.notify_one();
}

XTask* XThreadPool::GetTask()
{
	unique_lock<mutex> lock(m_mux);
	if (m_tasks.empty())
	{
		cv.wait(lock);
	}
	if (m_tasks.empty())
	{
		return nullptr;
	}
	XTask* tmptask = m_tasks.front();
	m_tasks.pop_front();
	return tmptask;
}


//线程池中线程的入口函数
void XThreadPool::Run()
{
	cout << "begin XThreadPool Run" << this_thread::get_id() << endl;
	while (true)
	{
		XTask* task = GetTask();
		if (task == nullptr) {
			continue;
		}
		try {
			task->Run();
		}
		catch (...)
		{

		}
	}
	//cout << "end XThreadPool Run" << this_thread::get_id() << endl;
}

XTask.cpp

javascript 复制代码
#include<iostream>
#include<thread>


class  XTask
{
public:
	virtual int Run() = 0;
};

class MyTask :public XTask
{
	int Run() override;
};

int MyTask::Run()
{
    cout << "this threadID is " << this_thread::get_id() << endl;
    cout << "MyTask " << endl;
    return 0;
}

main.cpp

javascript 复制代码
#include<thread>
#include<iostream>

#include"XThreadPool.h"
#include"XTasks.h"
using namespace std;

int main()
{
	XThreadPool  thpool;
	thpool.Init(16);
	thpool.Start();

	MyTask task1;
	thpool.AddTask(&task1);

	getchar();
}
相关推荐
坚果派·白晓明14 分钟前
【鸿蒙PC】SDL3 适配:AtomCode + Skills 快速集成 NAPI 测试工具
c++·华为·ai编程·harmonyos·atomcode
huangdong_19 分钟前
淘宝商品SKU图自动分类技术深度解析:从DOM解析到智能归档
开发语言·javascript·ecmascript
阿正的梦工坊22 分钟前
【Rust】12-借用检查器与非词法生命周期
开发语言·后端·rust
qq_25183645740 分钟前
基于java Web网络订餐系统设计与实现 源码文档
java·开发语言·前端
秋91 小时前
3年经验Python后端转AI Engineer:3个月实战转型计划(2026版)
开发语言·人工智能·python
凡人叶枫1 小时前
Effective C++ 条款17:以独立语句将 newed 对象置入智能指针
java·linux·开发语言·c++·算法
飞天狗1111 小时前
零基础JavaWeb入门——第2课:让网页“活”起来 —— JSP是什么?
java·开发语言·前端·后端·web
醇氧2 小时前
【Linux】Java 服务生产级部署指南:实现常驻后台、开机自启与系统服务化管理
java·开发语言
凡人叶枫2 小时前
Effective C++ 条款16:成对使用 new 和 delete 时要采取相同形式
开发语言·c++·effective c++
不吃土豆的马铃薯2 小时前
C++ 高性能网络缓冲区 Buffer 源码解析
linux·服务器·开发语言·网络·c++