2026-03-18 17:49:22 +08:00
|
|
|
|
# TaskSystem
|
|
|
|
|
|
|
|
|
|
|
|
**命名空间**: `XCEngine::Threading`
|
|
|
|
|
|
|
|
|
|
|
|
**类型**: `class` (singleton)
|
|
|
|
|
|
|
docs: 重构 API 文档结构并修正源码准确性
- 重组文档目录结构: 每个模块的概述页移动到模块子目录
- 重命名 index.md 为 main.md
- 修正所有模块文档中的错误:
- math: FromEuler→FromEulerAngles, TransformDirection 包含缩放, Box 是 OBB, Color::ToRGBA 格式
- containers: 新增 operator==/!= 文档, 补充 std::hash DJB 算法细节
- core: 修复 types 链接错误
- debug: LogLevelToString 返回大写, timestamp 是秒, Profiler 空实现标注, Windows API vs ANSI
- memory: 修复头文件路径, malloc vs operator new, 新增方法文档
- resources: 修复 Shader/Texture 链接错误
- threading: TaskSystem::Wait 空实现标注, ReadWriteLock 重入描述, LambdaTask 链接
- 验证: fix_links.py 确认 0 个断裂引用
2026-03-19 00:22:30 +08:00
|
|
|
|
**头文件**: `XCEngine/Threading/TaskSystem.h`
|
|
|
|
|
|
|
2026-03-19 00:31:14 +08:00
|
|
|
|
**描述**: 并行任务调度系统单例,提供优先级任务队列。
|
2026-03-18 17:49:22 +08:00
|
|
|
|
|
|
|
|
|
|
## 概述
|
|
|
|
|
|
|
2026-03-19 00:31:14 +08:00
|
|
|
|
`TaskSystem` 是 XCEngine 的核心并行任务调度系统。它创建多个工作线程,使用优先级队列调度任务。它还提供 `ParallelFor` 方法用于数据级并行,以及主线程任务队列。
|
|
|
|
|
|
|
2026-03-20 02:35:24 +08:00
|
|
|
|
**注意:** 当前实现的 `stealTasks` 配置项未生效,任务系统使用单一全局优先级任务队列。
|
2026-03-18 17:49:22 +08:00
|
|
|
|
|
|
|
|
|
|
## 单例访问
|
|
|
|
|
|
|
|
|
|
|
|
| 方法 | 描述 |
|
|
|
|
|
|
|------|------|
|
docs: 重构 API 文档结构并修正源码准确性
- 重组文档目录结构: 每个模块的概述页移动到模块子目录
- 重命名 index.md 为 main.md
- 修正所有模块文档中的错误:
- math: FromEuler→FromEulerAngles, TransformDirection 包含缩放, Box 是 OBB, Color::ToRGBA 格式
- containers: 新增 operator==/!= 文档, 补充 std::hash DJB 算法细节
- core: 修复 types 链接错误
- debug: LogLevelToString 返回大写, timestamp 是秒, Profiler 空实现标注, Windows API vs ANSI
- memory: 修复头文件路径, malloc vs operator new, 新增方法文档
- resources: 修复 Shader/Texture 链接错误
- threading: TaskSystem::Wait 空实现标注, ReadWriteLock 重入描述, LambdaTask 链接
- 验证: fix_links.py 确认 0 个断裂引用
2026-03-19 00:22:30 +08:00
|
|
|
|
| [`Get`](get.md) | 获取单例实例 |
|
2026-03-18 17:49:22 +08:00
|
|
|
|
|
|
|
|
|
|
## 公共方法
|
|
|
|
|
|
|
|
|
|
|
|
| 方法 | 描述 |
|
|
|
|
|
|
|------|------|
|
docs: 重构 API 文档结构并修正源码准确性
- 重组文档目录结构: 每个模块的概述页移动到模块子目录
- 重命名 index.md 为 main.md
- 修正所有模块文档中的错误:
- math: FromEuler→FromEulerAngles, TransformDirection 包含缩放, Box 是 OBB, Color::ToRGBA 格式
- containers: 新增 operator==/!= 文档, 补充 std::hash DJB 算法细节
- core: 修复 types 链接错误
- debug: LogLevelToString 返回大写, timestamp 是秒, Profiler 空实现标注, Windows API vs ANSI
- memory: 修复头文件路径, malloc vs operator new, 新增方法文档
- resources: 修复 Shader/Texture 链接错误
- threading: TaskSystem::Wait 空实现标注, ReadWriteLock 重入描述, LambdaTask 链接
- 验证: fix_links.py 确认 0 个断裂引用
2026-03-19 00:22:30 +08:00
|
|
|
|
| [`Initialize`](initialize.md) | 初始化任务系统 |
|
|
|
|
|
|
| [`Shutdown`](shutdown.md) | 关闭任务系统 |
|
|
|
|
|
|
| [`Submit(unique_ptr)`](submit.md) | 提交任务对象 |
|
|
|
|
|
|
| [`Submit(callback)`](submit.md) | 提交 lambda 任务 |
|
docs: fix naming conventions across threading, math, memory, core, and debug modules
threading/:
- Rename 19 camelCase method files to hyphenated names
- task-system: createtaskgroup→create-task-group, etc.
- tasksystemconfig: enabletaskprofiling→enable-task-profiling, etc.
- thread: getcurrentid→get-current-id, etc.
- task: addref→add-ref, getid→get-id, etc.
math/:
- Rename underscore operator files to hyphenated
- vector3: operator_add→operator-add, etc.
- matrix4: gettranslation→get-translation, etc.
- vector4: tovector3→to-vector3, constructor_vector3→constructor-vector3
- sphere: sphere_constructor→sphere-constructor, etc.
memory/:
- Remove duplicate memorymanager/ folder (kept manager/ which was correct)
core/:
- filewriter: Consolidate ctor-default.md and ctor-file.md into constructor.md
- Rename dtor.md→destructor.md
debug/:
- filelogsink: Rename construct.md→constructor.md, ~filelogsink.md→destructor.md
All overview pages updated with new file references.
2026-03-22 23:09:29 +08:00
|
|
|
|
| [`CreateTaskGroup`](create-task-group.md) | 创建任务组 |
|
|
|
|
|
|
| [`DestroyTaskGroup`](destroy-task-group.md) | 销毁任务组 |
|
2026-03-19 01:14:22 +08:00
|
|
|
|
| [`Wait`](wait.md) | 等待指定任务完成(当前为空实现) |
|
docs: fix naming conventions across threading, math, memory, core, and debug modules
threading/:
- Rename 19 camelCase method files to hyphenated names
- task-system: createtaskgroup→create-task-group, etc.
- tasksystemconfig: enabletaskprofiling→enable-task-profiling, etc.
- thread: getcurrentid→get-current-id, etc.
- task: addref→add-ref, getid→get-id, etc.
math/:
- Rename underscore operator files to hyphenated
- vector3: operator_add→operator-add, etc.
- matrix4: gettranslation→get-translation, etc.
- vector4: tovector3→to-vector3, constructor_vector3→constructor-vector3
- sphere: sphere_constructor→sphere-constructor, etc.
memory/:
- Remove duplicate memorymanager/ folder (kept manager/ which was correct)
core/:
- filewriter: Consolidate ctor-default.md and ctor-file.md into constructor.md
- Rename dtor.md→destructor.md
debug/:
- filelogsink: Rename construct.md→constructor.md, ~filelogsink.md→destructor.md
All overview pages updated with new file references.
2026-03-22 23:09:29 +08:00
|
|
|
|
| [`GetWorkerThreadCount`](get-worker-thread-count.md) | 获取工作线程数量 |
|
docs: 重构 API 文档结构并修正源码准确性
- 重组文档目录结构: 每个模块的概述页移动到模块子目录
- 重命名 index.md 为 main.md
- 修正所有模块文档中的错误:
- math: FromEuler→FromEulerAngles, TransformDirection 包含缩放, Box 是 OBB, Color::ToRGBA 格式
- containers: 新增 operator==/!= 文档, 补充 std::hash DJB 算法细节
- core: 修复 types 链接错误
- debug: LogLevelToString 返回大写, timestamp 是秒, Profiler 空实现标注, Windows API vs ANSI
- memory: 修复头文件路径, malloc vs operator new, 新增方法文档
- resources: 修复 Shader/Texture 链接错误
- threading: TaskSystem::Wait 空实现标注, ReadWriteLock 重入描述, LambdaTask 链接
- 验证: fix_links.py 确认 0 个断裂引用
2026-03-19 00:22:30 +08:00
|
|
|
|
| [`ParallelFor`](parallelfor.md) | 并行执行 for 循环 |
|
|
|
|
|
|
| [`RunOnMainThread`](runonmainthread.md) | 将任务提交到主线程执行 |
|
|
|
|
|
|
| [`Update`](update.md) | 在主线程中处理主线程队列 |
|
2026-03-18 17:49:22 +08:00
|
|
|
|
|
|
|
|
|
|
## 使用示例
|
|
|
|
|
|
|
|
|
|
|
|
```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();
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
## 相关文档
|
|
|
|
|
|
|
docs: 重构 API 文档结构并修正源码准确性
- 重组文档目录结构: 每个模块的概述页移动到模块子目录
- 重命名 index.md 为 main.md
- 修正所有模块文档中的错误:
- math: FromEuler→FromEulerAngles, TransformDirection 包含缩放, Box 是 OBB, Color::ToRGBA 格式
- containers: 新增 operator==/!= 文档, 补充 std::hash DJB 算法细节
- core: 修复 types 链接错误
- debug: LogLevelToString 返回大写, timestamp 是秒, Profiler 空实现标注, Windows API vs ANSI
- memory: 修复头文件路径, malloc vs operator new, 新增方法文档
- resources: 修复 Shader/Texture 链接错误
- threading: TaskSystem::Wait 空实现标注, ReadWriteLock 重入描述, LambdaTask 链接
- 验证: fix_links.py 确认 0 个断裂引用
2026-03-19 00:22:30 +08:00
|
|
|
|
- [ITask](../task/task.md) - 任务基类
|
2026-03-26 01:30:37 +08:00
|
|
|
|
- [LambdaTask](../lambda-task/lambda-task.md) - Lambda 任务
|
docs: 重构 API 文档结构并修正源码准确性
- 重组文档目录结构: 每个模块的概述页移动到模块子目录
- 重命名 index.md 为 main.md
- 修正所有模块文档中的错误:
- math: FromEuler→FromEulerAngles, TransformDirection 包含缩放, Box 是 OBB, Color::ToRGBA 格式
- containers: 新增 operator==/!= 文档, 补充 std::hash DJB 算法细节
- core: 修复 types 链接错误
- debug: LogLevelToString 返回大写, timestamp 是秒, Profiler 空实现标注, Windows API vs ANSI
- memory: 修复头文件路径, malloc vs operator new, 新增方法文档
- resources: 修复 Shader/Texture 链接错误
- threading: TaskSystem::Wait 空实现标注, ReadWriteLock 重入描述, LambdaTask 链接
- 验证: fix_links.py 确认 0 个断裂引用
2026-03-19 00:22:30 +08:00
|
|
|
|
- [TaskGroup](../task-group/task-group.md) - 任务组
|
2026-03-26 01:30:37 +08:00
|
|
|
|
- [TaskSystemConfig](../task-system-config/task-system-config.md) - 配置
|
2026-03-26 01:58:45 +08:00
|
|
|
|
- [../threading.md](../threading.md) - 模块总览
|