| 69 | |
| 70 | |
| 71 | CStatus UThreadPool::submit(const UTaskGroup& taskGroup, CMSec ttl) { |
| 72 | CGRAPH_FUNCTION_BEGIN |
| 73 | CGRAPH_ASSERT_INIT(true) |
| 74 | |
| 75 | std::vector<std::future<CVoid>> futures; |
| 76 | for (const auto& task : taskGroup.task_arr_) { |
| 77 | futures.emplace_back(commit(task)); |
| 78 | } |
| 79 | |
| 80 | // 计算最终运行时间信息 |
| 81 | auto deadline = std::chrono::system_clock::now() |
| 82 | + std::chrono::milliseconds(std::min(taskGroup.getTtl(), ttl)); |
| 83 | |
| 84 | for (auto& fut : futures) { |
| 85 | const auto& futStatus = fut.wait_until(deadline); |
| 86 | switch (futStatus) { |
| 87 | case std::future_status::ready: break; // 正常情况,直接返回了 |
| 88 | case std::future_status::timeout: status += CStatus("thread status timeout"); break; |
| 89 | case std::future_status::deferred: status += CStatus("thread status deferred"); break; |
| 90 | default: status += CStatus("thread status unknown"); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | if (taskGroup.on_finished_) { |
| 95 | taskGroup.on_finished_(status); |
| 96 | } |
| 97 | |
| 98 | CGRAPH_FUNCTION_END |
| 99 | } |
| 100 | |
| 101 | |
| 102 | CStatus UThreadPool::submit(CGRAPH_DEFAULT_CONST_FUNCTION_REF func, CMSec ttl, |
no test coverage detected