Qt更方便的使用线程池

使用Qt的线程池必须继承QRunnable这样有的时候用起来很麻烦

#include <memory>
#include <functional>
#include <QRunnable>

class CThreadPoolTask : public QRunnable
{
private:
	CThreadPoolTask(std::function<void()> funRun) : m_funRun(funRun) {}
public:

	//成员函数的对象时共享指针
	template<class Y, class T>
	static CThreadPoolTask* CreateTask(Y y, std::shared_ptr<T> obj)
	{
		return new CThreadPoolTask(std::bind(y, obj.get()));
	}

	template<class Y, class T, class A>
	static CThreadPoolTask* CreateTask(Y y, std::shared_ptr<T> obj, A a)
	{
		return new CThreadPoolTask(std::bind(y, obj, a));
	}

	template<class Y>
	static CThreadPoolTask* CreateTask(Y y)
	{
		return new CThreadPoolTask(std::bind(y));
	}

	template<class Y, class A>
	static CThreadPoolTask* CreateTask(Y y, A a)
	{
		return new CThreadPoolTask(std::bind(y, a));
	}

	template<class Y, class A, class A1>
	static CThreadPoolTask* CreateTask(Y y, A a, A1 a1)
	{
		return new CThreadPoolTask(std::bind(y, a, a1));
	}

	template<class Y, class A, class A1, class A2>
	static CThreadPoolTask* CreateTask(Y y, A a, A1 a1, A2 a2)
	{
		return new CThreadPoolTask(std::bind(y, a, a1, a2));
	}

	template<class Y, class A, class A1, class A2, class A3>
	static CThreadPoolTask* CreateTask(Y y, A a, A1 a1, A2 a2, A3 a3)
	{
		return new CThreadPoolTask(std::bind(y, a, a1, a2, a3));
	}

	template<class Y, class A, class A1, class A2, class A3, class A4>
	static CThreadPoolTask* CreateTask(Y y, A a, A1 a1, A2 a2, A3 a3, A4 a4)
	{
		return new CThreadPoolTask(std::bind(y, a, a1, a2, a3, a4));
	}

	template<class Y, class A, class A1, class A2, class A3, class A4, class A5>
	static CThreadPoolTask* CreateTask(Y y, A a, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5)
	{
		return new CThreadPoolTask(std::bind(y, a, a1, a2, a3, a4, a5));
	}

	template<class Y, class A, class A1, class A2, class A3, class A4, class A5, class A6>
	static CThreadPoolTask* CreateTask(Y y, A a, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6)
	{
		return new CThreadPoolTask(std::bind(y, a, a1, a2, a3, a4, a5, a6));
	}

	template<class Y, class A, class A1, class A2, class A3, class A4, class A5, class A6, class A7>
	static CThreadPoolTask* CreateTask(Y y, A a, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7)
	{
		return new CThreadPoolTask(std::bind(y, a, a1, a2, a3, a4, a5, a6, a7));
	}

	template<class Y, class A, class A1, class A2, class A3, class A4, class A5, class A6, class A7, class A8>
	static CThreadPoolTask* CreateTask(Y y, A a, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8)
	{
		return new CThreadPoolTask(std::bind(y, a, a1, a2, a3, a4, a5, a6, a7, a8));
	}

	template<class Y, class A, class A1, class A2, class A3, class A4, class A5, class A6, class A7, class A8, class A9>
	static CThreadPoolTask* CreateTask(Y y, A a, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8, A9 a9)
	{
		return new CThreadPoolTask(std::bind(y, a, a1, a2, a3, a4, a5, a6, a7, a8, a9));
	}

	virtual void run()
	{
		m_funRun();
	}

private:
	std::function<void()> m_funRun;
};

 

使用的例子

	//成员函数
	QThreadPool::globalInstance()->start(CThreadPoolTask::CreateTask(&CMainWindow::test, this));
	//成员函数带参数
	QThreadPool::globalInstance()->start(CThreadPoolTask::CreateTask(&CMainWindow::test1, this, 10));
	//普通函数
	QThreadPool::globalInstance()->start(CThreadPoolTask::CreateTask(MyPrint));
	//lambda表达式
	QThreadPool::globalInstance()->start(CThreadPoolTask::CreateTask([]() { qDebug() << "lambda"; }));

 

发布了50 篇原创文章 · 获赞 4 · 访问量 1万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览