跳转至

ev-nano-latch-thread-names-001

字段 值
项目 nanocurrency/nano-node
固定 commit 6b5f63ce04c9
路径 nano/lib/thread_pool.hpp
行段 L127-L137
特性 闩锁、屏障与计数信号量
许可证 BSD-3-Clause
代码来源 项目源码
级别 manual
审核 zyuhao7 @ 2026-10-27
测试代码 否
发现方式 manual

用法

用 std::latch 等待线程池中的每个线程完成一次线程命名

为什么典型

一次性汇合、不需要复用,latch 比条件变量加计数器更直接;任务用 arrive_and_wait,发起方只调用 wait。

摘录

    void set_thread_names ()
    {
        for (auto i = 0u; i < num_threads; ++i)
        {
            boost::asio::post (*thread_pool_impl, [this] () {
                nano::thread_role::set (thread_name);
                thread_names_latch.arrive_and_wait ();
            });
        }
        thread_names_latch.wait ();
    }

备注

生产路径;线程池构造阶段的名字设置,计数在线程池构造函数中初始化为线程数。

查看固定 commit 源码 · 返回证据索引