105 lines
2.1 KiB
C++
105 lines
2.1 KiB
C++
#ifndef MULTICOREMANAGER_H
|
|
#define MULTICOREMANAGER_H
|
|
|
|
#include <QObject>
|
|
#include <QThreadPool>
|
|
#include <QMap>
|
|
#include <QMutex>
|
|
#include <functional>
|
|
#include <QString>
|
|
|
|
/**
|
|
* @brief The MultiCoreManager class
|
|
* 全局多核心任务调度管理器,负责优化整个程序的多核心利用
|
|
*/
|
|
class MultiCoreManager : public QObject
|
|
{
|
|
Q_OBJECT
|
|
|
|
private:
|
|
static MultiCoreManager* m_instance;
|
|
static QMutex m_mutex;
|
|
|
|
explicit MultiCoreManager(QObject *parent = nullptr);
|
|
|
|
// 线程池
|
|
QThreadPool* m_threadPool;
|
|
|
|
// 任务统计
|
|
QMap<QString, int> m_taskCount;
|
|
mutable QMutex m_taskCountMutex;
|
|
|
|
// CPU核心数
|
|
int m_cpuCount;
|
|
|
|
public:
|
|
/**
|
|
* @brief 获取单例实例
|
|
*/
|
|
static MultiCoreManager* instance(QObject *parent = nullptr);
|
|
|
|
/**
|
|
* @brief 析构函数
|
|
*/
|
|
~MultiCoreManager();
|
|
|
|
/**
|
|
* @brief 提交任务到线程池
|
|
* @param task 任务函数
|
|
* @param taskType 任务类型标识
|
|
*/
|
|
void submitTask(std::function<void()> task, const QString& taskType = "general");
|
|
|
|
/**
|
|
* @brief 获取当前活跃线程数
|
|
*/
|
|
int activeThreadCount() const;
|
|
|
|
/**
|
|
* @brief 获取最大线程数
|
|
*/
|
|
int maxThreadCount() const;
|
|
|
|
/**
|
|
* @brief 设置最大线程数
|
|
*/
|
|
void setMaxThreadCount(int count);
|
|
|
|
/**
|
|
* @brief 获取任务统计信息
|
|
*/
|
|
QMap<QString, int> taskStatistics() const;
|
|
|
|
/**
|
|
* @brief 等待所有任务完成
|
|
* @param msecs 超时时间
|
|
*/
|
|
void waitForDone(int msecs = -1);
|
|
|
|
public slots:
|
|
/**
|
|
* @brief 清理线程池
|
|
*/
|
|
void clearThreadPool();
|
|
|
|
/**
|
|
* @brief 打印线程池状态信息
|
|
*/
|
|
void printStatus() const;
|
|
|
|
signals:
|
|
/**
|
|
* @brief 任务完成信号
|
|
* @param taskType 任务类型
|
|
*/
|
|
void taskCompleted(const QString& taskType);
|
|
|
|
/**
|
|
* @brief 状态更新信号
|
|
* @param activeThreads 活跃线程数
|
|
* @param maxThreads 最大线程数
|
|
*/
|
|
void statusUpdated(int activeThreads, int maxThreads);
|
|
};
|
|
|
|
#endif // MULTICOREMANAGER_H
|