Files
XCEngine/docs/api/threading/task-system/task-system.md
ssdfasd d34d040563 Fix broken links in Threading API docs
Fix 14 broken cross-references in docs/api/threading/:
- lambda-task path: lambdatask -> lambda-task (5 occurrences)
- task-system-config path: tasksystemconfig -> task-system-config (6 occurrences)
- read-write-lock self-ref: readwritelock -> read-write-lock (6 occurrences)
- task-system cross-method: createtaskgroup/destroytaskgroup -> create-task-group/destroy-task-group
- thread cross-method: getcurrentid/getid -> get-current-id/get-id

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-26 01:30:37 +08:00

84 lines
2.5 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) | 获取单例实例 |
## 公共方法
| 方法 | 描述 |
|------|------|
| [`TaskSystem()`](task-system.md) | 私有构造函数(单例) |
| [`~TaskSystem()`](task-system.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) - 模块总览