48 lines
1.6 KiB
C++
48 lines
1.6 KiB
C++
#ifndef IVRTHREADPOOL_H
|
||
#define IVRTHREADPOOL_H
|
||
|
||
#include <functional>
|
||
#include "VrKernelTool_global.h"
|
||
|
||
/// 线程任务执行函数类型
|
||
/// @param pParam 用户自定义参数
|
||
typedef std::function<void (void *)> ThreadExecFunc;
|
||
|
||
/// 线程池错误码
|
||
struct VrThreadPoolError
|
||
{
|
||
enum Code : int
|
||
{
|
||
Ok = 0, // 成功
|
||
InvalidParam = -100, // 参数无效(空函数指针等)
|
||
ThreadBusy = -101, // 线程正忙(已有任务在执行)
|
||
NoAvailableThread = -102, // 无可用线程(线程池满或已停止)
|
||
};
|
||
};
|
||
|
||
/// 线程池抽象接口
|
||
class WDKERNELTOOL_EXPORT IVrThreadPool
|
||
{
|
||
public:
|
||
virtual ~IVrThreadPool() = default;
|
||
|
||
/// 创建线程池实例(工厂方法,指定最大线程数)
|
||
/// @param ppThreadPool [out] 接收线程池指针
|
||
/// @param maxThreadCount 最大线程数,0=硬件并发数,-1=不限制
|
||
/// @return true 成功, false 失败
|
||
static bool CreateThreadPool(IVrThreadPool** ppThreadPool, int maxThreadCount);
|
||
|
||
/// 提交任务到线程池(线程池满时自动排队等待)
|
||
/// @param fExecFunc 任务执行函数
|
||
/// @param pParam 传递给任务函数的参数
|
||
/// @return VrThreadPoolError::Ok 成功, 负值为错误码
|
||
virtual int ExecTask(ThreadExecFunc fExecFunc, void* pParam) = 0;
|
||
|
||
/// 等待所有已提交任务执行完成
|
||
/// @param timeoutMs 超时时间(毫秒),0 表示无限等待
|
||
/// @return true 所有任务已完成, false 超时
|
||
virtual bool WaitAllTaskFinish(int timeoutMs = 0) = 0;
|
||
};
|
||
|
||
#endif // IVRTHREADPOOL_H
|