Files
XCEngine/docs/api/threading/task-system/task-system.md
ssdfasd 8df04c120f docs: 更新 API 文档 - 多模块修复和完善
- audio: 更新 audio-system 方法文档
- components: 新增 audio-listener/audio-source 组件方法文档,新增 remove-component 方法
- core: 更新 filewriter, types 文档
- math: 更新 box 方法文档
- memory: 更新 proxy-allocator 文档
- resources: 更新 loader 和 texture 文档
- rhi: 更新 opengl 设备、shader、swap-chain 文档
- threading: 更新 mutex 和 task-system 文档
2026-03-26 01:58:45 +08:00

82 lines
2.4 KiB
Markdown
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.
# TaskSystem
**命名空间**: `XCEngine::Threading`
**类型**: `class` (singleton)
**头文件**: `XCEngine/Threading/TaskSystem.h`
**描述**: 并行任务调度系统单例,提供优先级任务队列。
## 概述
`TaskSystem` 是 XCEngine 的核心并行任务调度系统。它创建多个工作线程,使用优先级队列调度任务。它还提供 `ParallelFor` 方法用于数据级并行,以及主线程任务队列。
**注意:** 当前实现的 `stealTasks` 配置项未生效,任务系统使用单一全局优先级任务队列。
## 单例访问
| 方法 | 描述 |
|------|------|
| [`Get`](get.md) | 获取单例实例 |
## 公共方法
| 方法 | 描述 |
|------|------|
| [`Initialize`](initialize.md) | 初始化任务系统 |
| [`Shutdown`](shutdown.md) | 关闭任务系统 |
| [`Submit(unique_ptr)`](submit.md) | 提交任务对象 |
| [`Submit(callback)`](submit.md) | 提交 lambda 任务 |
| [`CreateTaskGroup`](create-task-group.md) | 创建任务组 |
| [`DestroyTaskGroup`](destroy-task-group.md) | 销毁任务组 |
| [`Wait`](wait.md) | 等待指定任务完成(当前为空实现) |
| [`GetWorkerThreadCount`](get-worker-thread-count.md) | 获取工作线程数量 |
| [`ParallelFor`](parallelfor.md) | 并行执行 for 循环 |
| [`RunOnMainThread`](runonmainthread.md) | 将任务提交到主线程执行 |
| [`Update`](update.md) | 在主线程中处理主线程队列 |
## 使用示例
```cpp
// 初始化4 个工作线程)
TaskSystemConfig config;
config.workerThreadCount = 4;
TaskSystem::Get().Initialize(config);
// 提交任务
TaskSystem::Get().Submit([]() {
printf("Task 1 running\n");
});
TaskSystem::Get().Submit([]() {
printf("Task 2 running\n");
}, TaskPriority::High);
// 并行 for
TaskSystem::Get().ParallelFor(0, 1000, [](int32_t i) {
process(i);
});
// 主线程任务
TaskSystem::Get().RunOnMainThread([]() {
// 更新 UI 或其他主线程操作
});
// 主循环中调用 Update
while (running) {
TaskSystem::Get().Update(); // 处理主线程任务
}
// 关闭
TaskSystem::Get().Shutdown();
```
## 相关文档
- [ITask](../task/task.md) - 任务基类
- [LambdaTask](../lambda-task/lambda-task.md) - Lambda 任务
- [TaskGroup](../task-group/task-group.md) - 任务组
- [TaskSystemConfig](../task-system-config/task-system-config.md) - 配置
- [../threading.md](../threading.md) - 模块总览