ev-abseil-invoke-once-callable-002¶
| 字段 | 值 |
|---|---|
| 项目 | abseil/abseil-cpp |
| 固定 commit | 2065f4ded055 |
| 路径 | absl/base/call_once.h |
| 行段 | L180-L190 |
| 特性 | std::invoke 与 std::apply |
| 许可证 | Apache-2.0 |
| 代码来源 | 项目源码 |
| 级别 | manual |
| 审核 | zyuhao7 @ 2026-10-26 |
| 测试代码 | 否 |
| 发现方式 | manual |
用法¶
用 std::invoke 在 call_once 路径上调用任意可调用对象
为什么典型¶
call_once 的 Callable 可能是函数指针、仿函数或成员指针;std::invoke 统一处理并把参数按原值类别转发。
摘录¶
uint32_t old_control = kOnceInit;
if (control->compare_exchange_strong(old_control, kOnceRunning,
std::memory_order_relaxed) ||
base_internal::SpinLockWait(control, std::size(trans), trans,
scheduling_mode) == kOnceInit) {
std::invoke(std::forward<Callable>(fn), std::forward<Args>(args)...);
old_control =
control->exchange(base_internal::kOnceDone, std::memory_order_release);
if (old_control == base_internal::kOnceWaiter) {
base_internal::SpinLockWake(control, true);
}
备注¶
生产路径;在同步原语内部调用用户提供的可调用对象。