ev-stdexec-condvar-worker-sleep-003¶
| 字段 | 值 |
|---|---|
| 项目 | NVIDIA/stdexec |
| 固定 commit | 6d7ad689f4d4 |
| 路径 | include/exec/static_thread_pool.hpp |
| 行段 | L1145-L1159 |
| 特性 | std::condition_variable |
| 许可证 | Apache-2.0 |
| 代码来源 | 项目源码 |
| 级别 | manual |
| 审核 | zyuhao7 @ 2026-11-03 |
| 测试代码 | 否 |
| 发现方式 | manual |
用法¶
线程池工作线程在无任务时用 condition_variable::wait 休眠
为什么典型¶
工作窃取失败后线程先改状态再等待;状态由原子量维护,条件变量负责把线程挂起,避免空转消耗 CPU。
摘录¶
std::unique_lock lock{mut_};
if (stop_requested_)
{
return result;
}
state expected = state::running;
if (state_.compare_exchange_weak(expected, state::sleeping, __std::memory_order_relaxed))
{
result = try_remote();
if (result.task)
{
return result;
}
set_sleeping();
cv_.wait(lock);
备注¶
生产头文件;这里用无谓词 wait,因为状态转换由前置的 compare_exchange_weak 完成,唤醒后仍需检查 owns_lock 与任务队列。