GrabBag/Module/ThreadPool/Inc/IVrThreadPool.h

48 lines
1.6 KiB
C
Raw Normal View History

2026-03-28 10:49:55 +08:00
#ifndef IVRTHREADPOOL_H
#define IVRTHREADPOOL_H
2026-06-20 23:36:24 +08:00
2026-03-28 10:49:55 +08:00
#include <functional>
#include "VrKernelTool_global.h"
2026-06-20 23:36:24 +08:00
/// 线程任务执行函数类型
/// @param pParam 用户自定义参数
2026-03-28 10:49:55 +08:00
typedef std::function<void (void *)> ThreadExecFunc;
2026-06-20 23:36:24 +08:00
/// 线程池错误码
struct VrThreadPoolError
{
enum Code : int
{
Ok = 0, // 成功
InvalidParam = -100, // 参数无效(空函数指针等)
ThreadBusy = -101, // 线程正忙(已有任务在执行)
NoAvailableThread = -102, // 无可用线程(线程池满或已停止)
};
};
2026-03-28 10:49:55 +08:00
2026-06-20 23:36:24 +08:00
/// 线程池抽象接口
class WDKERNELTOOL_EXPORT IVrThreadPool
2026-03-28 10:49:55 +08:00
{
public:
2026-06-20 23:36:24 +08:00
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 成功, 负值为错误码
2026-03-28 10:49:55 +08:00
virtual int ExecTask(ThreadExecFunc fExecFunc, void* pParam) = 0;
2026-06-20 23:36:24 +08:00
/// 等待所有已提交任务执行完成
/// @param timeoutMs 超时时间毫秒0 表示无限等待
/// @return true 所有任务已完成, false 超时
virtual bool WaitAllTaskFinish(int timeoutMs = 0) = 0;
2026-03-28 10:49:55 +08:00
};
#endif // IVRTHREADPOOL_H