C++并发与多线程-线程之间传递任务

       线程之间传递回调函数或者任务的一种方法是使用std::packaged_task封装可调用对象,然后将std::packaged_task对象放入公共容器里面,然后被调用任务的线程持到std::packaged_task里面的future对象,当需要数据的时候调用future对象的get()或wait()方法即可阻塞并等待。执行任务的线程执行完可调用对象之后,该future就变为可获得的状态。被调用任务的线程就不再阻塞,继续执行。下面程序模拟了GUI收到更新消息并执行回调任务的例子。

//GUIThreadWorker.h

#pragma once
#include
#include
#include
class GUIThreadWorker {
private:
	GUIThreadWorker();
	~GUIThreadWorker();
	std::deque > tasks;
	std::mutex lock;
	bool done = false;
	std::thread gui_thread;
private:
	void start();
public:
	GUIThreadWorker(const GUIThreadWorker& other) = delete;
	GUIThreadWorker& operator=(const GUIThreadWorker& other) = delete;
	static GUIThreadWorker* getInstance();
	static GUIThreadWorker* instance;
	static std::mutex mtx;
	static std::once_flag s_flag;

	template std::future push_task(Func);
	void require_done();
};

template
inline std::future GUIThreadWorker::push_task(Func f)
{
	std::packaged_task task(f);
	std::future future = task.get_future();
	std::lock_guard lk(lock);
	tasks.push_back(std::move(task));
	return future;
}
//GUIThreadWorker.cpp

#include "pch.h"
#include "GUIThreadWorker.h"

GUIThreadWorker* GUIThreadWorker::instance = nullptr;
std::mutex GUIThreadWorker::mtx;
std::once_flag GUIThreadWorker::s_flag;

GUIThreadWorker::GUIThreadWorker() {
	std::thread t([this]() {
		start();
	});
	gui_thread = std::move(t);
	gui_thread.detach();
}

GUIThreadWorker::~GUIThreadWorker()
{
	tasks.clear();
	done = true;
}

void GUIThreadWorker::start()
{
	while (!done) {
		std::packaged_task task;
		{
			std::lock_guard lk(lock);
			if (tasks.empty())
				continue;
			task = std::move(tasks.front());
			tasks.pop_front();
		}
		task();
	}
}

GUIThreadWorker * GUIThreadWorker::getInstance()
{
	/*if (!instance) {
		std::lock_guard lk(mtx);
		if (!instance) {
			instance = new GUIThreadWorker();
		}
	}*/
	std::call_once(s_flag, []() {
		instance = new GUIThreadWorker();
	});
	return instance;
}

void GUIThreadWorker::require_done()
{
	tasks.clear();
	done = true;
}
//Main函数入口

#include "pch.h"
#include 
#include 
#include "GUIThreadWorker.h"


void func() {
	std::cout << "hello world!!" << std::endl;
}
void func1() {
	std::cout << "my name is CBee!!" << std::endl;
}

int main()
{
	GUIThreadWorker::getInstance()->push_task(func);
	std::this_thread::sleep_for(std::chrono::seconds(1));
	GUIThreadWorker::getInstance()->push_task(func1);
	return 0;
}

单例类GUIThreadWorker在创建的时候新建一个GUI线程,用来接收消息,刷新UI并执行回调。

运行结果:

C++并发与多线程-线程之间传递任务_第1张图片

你可能感兴趣的:(C++并发与多线程)