1
0
mirror of https://github.com/jaredtao/TaoQuick.git synced 2025-01-31 21:22:58 +08:00
2020-10-29 15:26:24 +08:00

40 lines
1.0 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#pragma once
#include "TaoCommonGlobal.h"
#include "ThreadCommon.h"
#include <QObject>
#include <QRunnable>
namespace TaoCommon {
// 对Qt线程池的简单封装适合一次性执行的任务。
// 用法: ThreadPool::getInstance()->work(workCall, resultCall)
// workCall是在新线程中执行的任务resultCall是任务执行完成后回到调用线程中用来处理执行结果。
// 不支持任务取消、暂停。
class ThreadObject : public QObject, public QRunnable
{
Q_OBJECT
public:
explicit ThreadObject(const WorkCallback &work);
void run() override;
signals:
void readyResult(bool);
private:
WorkCallback m_workCall;
};
class TAO_API ThreadPool : public QObject
{
Q_OBJECT
public:
static ThreadPool *getInstance()
{
static ThreadPool poll;
return &poll;
}
// workCall in sub thread, resultCall in main thread
void work(const WorkCallback &workCall, const WorkResultCallback &resultCall);
private:
ThreadPool() {}
};
} // namespace TaoCommon