ev-spdlog-chrono-wait-for-002¶
| 字段 | 值 |
|---|---|
| 项目 | gabime/spdlog |
| 固定 commit | 79524ddd08a4 |
| 路径 | include/spdlog/details/mpmc_blocking_q.h |
| 行段 | L68-L79 |
| 特性 | std::chrono |
| 许可证 | MIT |
| 代码来源 | 项目源码 |
| 级别 | manual |
| 审核 | zyuhao7 @ 2026-11-03 |
| 测试代码 | 否 |
| 发现方式 | manual |
用法¶
用 chrono::milliseconds 限定条件变量的等待时长
为什么典型¶
有界等待把“等到有数据”与“最多等多久”分开:wait_for 接受 chrono 时长,超时返回 false,调用方可以据此在退出时排空队列而不是永久阻塞。
摘录¶
bool dequeue_for(T &popped_item, std::chrono::milliseconds wait_duration) {
{
std::unique_lock<std::mutex> lock(queue_mutex_);
if (!push_cv_.wait_for(lock, wait_duration, [this] { return !this->q_.empty(); })) {
return false;
}
popped_item = std::move(q_.front());
q_.pop_front();
}
pop_cv_.notify_one();
return true;
}
备注¶
生产头文件;日志后端的阻塞队列用带超时的出队支持优雅关闭,时长类型直接进入接口签名。