docs: update memory and threading API docs

This commit is contained in:
2026-03-20 02:35:24 +08:00
parent c5b17239ca
commit fd792b7df1
103 changed files with 2485 additions and 673 deletions

View File

@@ -1,43 +1,47 @@
# TaskGroup::Cancel
```cpp
void Cancel()
void Cancel();
```
**注意:** 此方法为**部分实现**,存在已知问题
取消任务组中所有尚未执行的任务。正在执行的任务将不受影响。
取消任务组中所有尚未开始执行的任务。已经执行完成的任务和正在执行的任务不受影响
**参数:**
**返回:**
**复杂度** O(n)
**当前状态:** 此方法会调用各任务的 `OnCancel()` 回调,但不会减少 `m_pendingCount` 计数器。因此调用 `Wait()` 会导致永久阻塞。建议在 `Cancel()` 后使用 `IsComplete()` 轮询或使用带超时的 `WaitFor()`
**线程安全** ✅ 线程安全
**示例:**
```cpp
TaskGroup* group = TaskSystem::Get().CreateTaskGroup();
#include "XCEngine/Threading/TaskGroup.h"
#include "XCEngine/Threading/Task.h"
#include <iostream>
#include <chrono>
for (int i = 0; i < 100; ++i) {
group->AddTask([i]() {
if (ShouldCancel()) {
return; // 检查取消状态
}
ProcessLongTask(i);
});
using namespace XCEngine::Threading;
int main() {
TaskGroup group;
for (int i = 0; i < 10; ++i) {
group.AddTask([i]() {
std::cout << "Task " << i << " executing\n";
std::this_thread::sleep_for(std::chrono::milliseconds(100));
});
}
std::this_thread::sleep_for(std::chrono::milliseconds(150));
std::cout << "Cancelling remaining tasks...\n";
group.Cancel();
group.Wait();
std::cout << "Tasks cancelled. Final progress: " << (group.GetProgress() * 100) << "%\n";
return 0;
}
// 如果用户点击取消按钮
if (userClickedCancel) {
group->Cancel();
printf("Tasks canceled. Progress: %.1f%%\n", group->GetProgress() * 100.0f);
}
// 注意Cancel() 后 Wait() 会永久阻塞,应使用 WaitFor()
group->WaitFor(std::chrono::seconds(1));
```
## 相关文档