40 lines
860 B
Markdown
40 lines
860 B
Markdown
|
|
# TaskGroup::WaitFor
|
|||
|
|
|
|||
|
|
```cpp
|
|||
|
|
bool WaitFor(std::chrono::milliseconds timeout)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
等待任务组中所有任务完成,可在超时后返回。
|
|||
|
|
|
|||
|
|
**参数:**
|
|||
|
|
- `timeout` - 最大等待时间
|
|||
|
|
|
|||
|
|
**返回:** `bool` - 所有任务在超时前完成返回 true,超时返回 false
|
|||
|
|
|
|||
|
|
**复杂度:** O(n)
|
|||
|
|
|
|||
|
|
**示例:**
|
|||
|
|
|
|||
|
|
```cpp
|
|||
|
|
TaskGroup* group = TaskSystem::Get().CreateTaskGroup();
|
|||
|
|
|
|||
|
|
for (int i = 0; i < 5; ++i) {
|
|||
|
|
group->AddTask([i]() {
|
|||
|
|
Thread::Sleep(i * 100);
|
|||
|
|
printf("Task %d done\n", i);
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (group->WaitFor(std::chrono::seconds(1))) {
|
|||
|
|
printf("All tasks completed within 1 second\n");
|
|||
|
|
} else {
|
|||
|
|
printf("Timeout! Some tasks still running\n");
|
|||
|
|
printf("Progress: %.1f%%\n", group->GetProgress() * 100.0f);
|
|||
|
|
}
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 相关文档
|
|||
|
|
|
|||
|
|
- [TaskGroup 总览](task-group.md) - 返回类总览
|
|||
|
|
- [Wait](wait.md) - 无超时等待
|