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 
#include 
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 

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 
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 
#include 
#include 
#include 

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::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();
}


附上截图
C++多线程案列_第1张图片

你可能感兴趣的:(C++,C++,多线程,c++,算法,开发语言)