Files
XCEngine/docs/api/threading/task-group/add-task-callback.md

56 lines
1.3 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.
# TaskGroup::AddTask (Callback)
```cpp
uint64_t AddTask(Callback&& func, TaskPriority priority = TaskPriority::Normal);
```
将函数回调添加为任务。该方法接受一个可调用对象lambda、函数指针、std::function 等)作为任务执行体。
**参数:**
- `func` - 可调用对象,执行任务逻辑
- `priority` - 任务优先级,默认为 Normal
**返回:** 返回分配给任务的唯一 ID可用于后续添加依赖关系
**线程安全:** ✅ 线程安全
**示例:**
```cpp
#include "XCEngine/Threading/TaskGroup.h"
#include "XCEngine/Threading/Task.h"
#include <iostream>
using namespace XCEngine::Threading;
int main() {
TaskGroup group;
uint64_t task1 = group.AddTask(
[]() { std::cout << "Task 1 executing\n"; },
TaskPriority::Normal
);
uint64_t task2 = group.AddTask(
[]() { std::cout << "Task 2 executing (High priority)\n"; },
TaskPriority::High
);
uint64_t task3 = group.AddTask(
[]() { std::cout << "Task 3 executing (Low priority)\n"; },
TaskPriority::Low
);
group.AddDependency(task2, task1);
group.Wait();
return 0;
}
```
## 相关文档
- [TaskGroup 总览](task-group.md) - 返回类总览
- [AddTask(ITask)](add-task.md) - 添加 ITask 对象形式的任务
- [TaskPriority](task-priority.md) - 任务优先级说明