C++多线程案列

C++多线程案列

  • 话不多说,直接上代码:
# CMakeList.txt: ThreadDemo1 的 CMake 项目,在此处包括源代码并定义
# 项目特定的逻辑。
#
cmake_minimum_required (VERSION 3.8)

# 将源代码添加到此项目的可执行文件。

set(INC_LIST 
	./Task.h
	./Producer.h
	./ThreadPool.h
	./CommonCond.h

)

set(SRC_LIST
	./Task.cpp
	./Producer.cpp
	./ThreadPool.cpp
	./CommonCond.cpp
	./main.cpp

)


add_executable (ThreadDemo2 ${INC_LIST} ${SRC_LIST})

# TODO: 如有需要,请添加测试并安装目标。
#include "Producer.h"
#include "ThreadPool.h"



int main() {
	
	Producer* producer=new Producer();
	producer->produceTask(100000);
	
	ThreadPool::instance()->start();
	getchar();
	return 0;

}

#ifndef _COMMON_COND_H_
#define _COMMON_COND_H_

#include <condition_variable>
#include <mutex>
class  CommonCond {

protected:
static	std::condition_variable  m_cond;
static	std::mutex m_mutex;
};



#endif

#include "CommonCond.h"

std::condition_variable  CommonCond::m_cond;
std::mutex CommonCond::m_mutex;

#ifndef _PRODUCER_H_
#define _PRODUCER_H_

#include "CommonCond.h"

class Producer :public CommonCond{

public:
	void runProduce(int num);
	void produceTask(int taskNum);

};
#endif //_PRODUCER_H_
#include "Producer.h"
#include "ThreadPool.h"
#include "Task.h"
void Producer::runProduce(int num)
{
	/*std::thread  th(&Producer::produceTask,num,this,ThreadPool::instance());
	th.join();*/

}
void Producer::produceTask(int taskNum) {
	for (int i = 0; i < taskNum; i++)
	{
		
		ThreadPool::instance()->addTask(Task(i));
	}
	
}

#ifndef _TASK_H_
#define _TASK_H_
#include <mutex>

class Task {

public:
	explicit Task(int nNumber = 0);
	void operator ()(int id);


private:

	int m_nNumber;
	static std::mutex m_mutex;

};


#endif // !_TASK_H_

#include "Task.h"
#include <iostream>
std::mutex Task::m_mutex;
Task::Task(int nNumber):m_nNumber(nNumber)
{

}

void Task::operator()(int id)
{
	std::unique_lock<std::mutex>  lk(m_mutex);
	std::cout << "我是第:	"<< id<<"	个工作者" << "	我是第		" << m_nNumber << "		任务!" << std::endl;
}

#ifndef  _THREAD_POOL_H_
#define  _THREAD_POOL_H_
#include "CommonCond.h"
#include <vector>
#include <thread>
#include <queue>
#include <functional>

class ThreadPool :public CommonCond {
	using  Task = std::function<void(int)>;
public:
	static ThreadPool* instance();
	void addTask(Task task);

	void runTask(int id);
	void initThreadPool();
	~ThreadPool();
	void start();
protected:
	explicit ThreadPool();
private:
	static ThreadPool* m_instance;
	std::queue<Task>  m_tasks;
	std::vector<std::thread*> m_threads;
	bool	m_goFlag = false;
	bool    m_exitFlag = true;
};
#endif // ! _THREAD_POOL_H_

#include "ThreadPool.h"
#include "Task.h"
ThreadPool* ThreadPool::m_instance = nullptr;
ThreadPool* ThreadPool::instance()
{
	if (m_instance == nullptr) {
	
		m_instance = new  ThreadPool();
	}
	return m_instance;
}

void ThreadPool::addTask(Task taskPtr)
{
	m_tasks.push(taskPtr);
}

void ThreadPool::runTask(int id)
{
	while (m_exitFlag)
	{
		Task task;
		{
			std::unique_lock<std::mutex>  ulock(m_mutex);
			m_cond.wait(ulock, [=]() {
				return m_goFlag&&!m_tasks.empty();
				});


			task = m_tasks.front();
			m_tasks.pop();
			ulock.unlock();
			m_cond.notify_all();
		}
		task(id);
	}

}

void ThreadPool::initThreadPool()
{

	for (size_t i = 0; i < 10; i++)
	{
		std::thread* threadPtr=new std::thread(&ThreadPool::runTask, this,i);
		m_threads.push_back(threadPtr);
		threadPtr->detach();
	}
}

ThreadPool::~ThreadPool()
{
	if (!m_threads.empty()) {
		std::vector<std::thread*>::iterator iter = m_threads.begin();
		for  (; iter !=m_threads.end(); iter++)
		{
			delete (*iter);
			
		}
		
	}
}

void ThreadPool::start()
{
	/*if (!m_threads.empty()) {
		std::vector<std::thread*>::iterator iter = m_threads.begin();
		for (; iter != m_threads.end(); iter++)
		{
			(*iter)->detach();
		}
	}*/
	std::unique_lock<std::mutex>  ulock(m_mutex);
	m_goFlag = true;
	m_cond.notify_all();
}

ThreadPool::ThreadPool()
{
	initThreadPool();
}


附上截图
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值