Files
XCEngine/docs/api/threading/task-system/task-system.md
ssdfasd f5a34f8adc docs: 重构 API 文档 - components 和 scene 模块
- components: 修复英文标题为中文,添加缺失组件文档
  - 新增 camera-component, light-component, audio-source-component, audio-listener-component 类总览
  - 修复 get-position.md 格式
  - 更新 components.md 模块总览
- scene: 修复方法文档格式,新增缺失方法
  - 修复 find.md, create-game-object.md 英文标题
  - 新增 FindByID, SerializeToString, DeserializeFromString 方法文档
  - 更新 scene.md 类总览方法列表
2026-03-26 01:50:27 +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/threading.md](../threading.md) - 模块总览