| 171 | |
| 172 | |
| 173 | CVoid UThreadPool::monitor() { |
| 174 | while (is_monitor_) { |
| 175 | while (is_monitor_ && !is_init_) { |
| 176 | // 如果没有init,则一直处于空跑状态 |
| 177 | CGRAPH_SLEEP_SECOND(1) |
| 178 | } |
| 179 | |
| 180 | int span = config_.monitor_span_; |
| 181 | while (is_monitor_ && is_init_ && span--) { |
| 182 | CGRAPH_SLEEP_SECOND(1) // 保证可以快速退出 |
| 183 | } |
| 184 | |
| 185 | // 如果 primary线程都在执行,则表示忙碌 |
| 186 | bool busy = std::all_of(primary_threads_.begin(), primary_threads_.end(), |
| 187 | [](UThreadPrimaryPtr ptr) { return nullptr != ptr && ptr->is_running_; }); |
| 188 | |
| 189 | // 如果忙碌或者priority_task_queue_中有任务,则需要添加 secondary线程 |
| 190 | if (busy || !priority_task_queue_.empty()) { |
| 191 | createSecondaryThread(1); |
| 192 | } |
| 193 | |
| 194 | // 判断 secondary 线程是否需要退出 |
| 195 | for (auto iter = secondary_threads_.begin(); iter != secondary_threads_.end(); ) { |
| 196 | (*iter)->freeze() ? secondary_threads_.erase(iter++) : iter++; |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | CGRAPH_NAMESPACE_END |
nothing calls this directly
no outgoing calls
no test coverage detected