| 90 | |
| 91 | |
| 92 | CStatus UThreadPool::submit(const UTaskGroup& taskGroup, const CMSec ttl) { |
| 93 | CGRAPH_FUNCTION_BEGIN |
| 94 | CGRAPH_ASSERT_INIT(true) |
| 95 | |
| 96 | std::vector<std::future<CVoid>> futures; |
| 97 | futures.reserve(taskGroup.getSize()); |
| 98 | for (const auto& task : taskGroup.task_arr_) { |
| 99 | futures.emplace_back(commit(task)); |
| 100 | } |
| 101 | |
| 102 | // 计算最终运行时间信息 |
| 103 | const auto& deadline = std::chrono::steady_clock::now() |
| 104 | + std::chrono::milliseconds(std::min(taskGroup.getTtl(), ttl)); |
| 105 | |
| 106 | for (auto& fut : futures) { |
| 107 | const auto& futStatus = fut.wait_until(deadline); |
| 108 | switch (futStatus) { |
| 109 | case std::future_status::ready: break; // 正常情况,直接返回了 |
| 110 | case std::future_status::timeout: status += CStatus("thread status timeout"); break; |
| 111 | case std::future_status::deferred: status += CStatus("thread status deferred"); break; |
| 112 | default: status += CStatus("thread status unknown"); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | if (taskGroup.on_finished_) { |
| 117 | taskGroup.on_finished_(status); |
| 118 | } |
| 119 | |
| 120 | CGRAPH_FUNCTION_END |
| 121 | } |
| 122 | |
| 123 | |
| 124 | CStatus UThreadPool::submit(CGRAPH_DEFAULT_CONST_FUNCTION_REF func, const CMSec ttl, |
no test coverage detected