docs: update memory and threading API docs
This commit is contained in:
89
docs/api/threading/task-group/wait-for.md
Normal file
89
docs/api/threading/task-group/wait-for.md
Normal file
@@ -0,0 +1,89 @@
|
||||
# TaskGroup::WaitFor
|
||||
|
||||
```cpp
|
||||
bool WaitFor(std::chrono::milliseconds timeout);
|
||||
```
|
||||
|
||||
等待任务组中的所有任务完成,最多等待指定的超时时间。
|
||||
|
||||
**参数:**
|
||||
- `timeout` - 最大等待时间
|
||||
|
||||
**返回:**
|
||||
- `true` - 所有任务在超时前已完成
|
||||
- `false` - 等待超时,仍有任务未完成
|
||||
|
||||
**线程安全:** ✅ 线程安全
|
||||
|
||||
**示例:**
|
||||
|
||||
```cpp
|
||||
#include "XCEngine/Threading/TaskGroup.h"
|
||||
#include "XCEngine/Threading/Task.h"
|
||||
#include <iostream>
|
||||
#include <chrono>
|
||||
|
||||
using namespace XCEngine::Threading;
|
||||
|
||||
int main() {
|
||||
TaskGroup group;
|
||||
|
||||
group.AddTask([]() {
|
||||
std::this_thread::sleep_for(std::chrono::seconds(5));
|
||||
std::cout << "Long running task completed\n";
|
||||
});
|
||||
|
||||
std::cout << "Waiting with 1 second timeout...\n";
|
||||
|
||||
if (group.WaitFor(std::chrono::milliseconds(1000))) {
|
||||
std::cout << "All tasks completed within timeout\n";
|
||||
} else {
|
||||
std::cout << "Timeout! Some tasks still running.\n";
|
||||
}
|
||||
|
||||
group.Wait();
|
||||
std::cout << "All tasks finally completed after second wait.\n";
|
||||
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
|
||||
## 相关文档
|
||||
|
||||
- [TaskGroup 总览](task-group.md) - 返回类总览
|
||||
- [Wait](wait.md) - 无限等待
|
||||
|
||||
## 详细描述
|
||||
|
||||
`WaitFor` 方法会阻塞当前线程,直到所有任务完成或超过指定的超时时间。
|
||||
|
||||
## 参数
|
||||
|
||||
| 参数 | 类型 | 描述 |
|
||||
|------|------|------|
|
||||
| `timeout` | `std::chrono::milliseconds` | 超时时间(毫秒) |
|
||||
|
||||
## 返回值
|
||||
|
||||
返回 `bool` 类型:
|
||||
- `true`: 所有任务在超时前完成
|
||||
- `false`: 发生超时
|
||||
|
||||
## 示例
|
||||
|
||||
```cpp
|
||||
TaskGroup* group = TaskSystem::Get().CreateTaskGroup();
|
||||
|
||||
group->AddTask([]() {
|
||||
std::this_thread::sleep_for(std::chrono::seconds(5));
|
||||
});
|
||||
|
||||
// 等待3秒超时
|
||||
if (group->WaitFor(std::chrono::milliseconds(3000))) {
|
||||
printf("All tasks completed on time\n");
|
||||
} else {
|
||||
printf("Wait timed out\n");
|
||||
}
|
||||
|
||||
TaskSystem::Get().DestroyTaskGroup(group);
|
||||
```
|
||||
Reference in New Issue
Block a user