跳转至

ev-gw2radial-jthread-com-thread-001

字段 值
项目 Friendly0Fire/GW2Radial
固定 commit 3178575f74dc
路径 src/Core.cpp
行段 L121-L133
特性 std::jthread 与停止令牌
许可证 MIT
代码来源 项目源码
级别 manual
审核 zyuhao7 @ 2026-10-26
测试代码 否
发现方式 manual

用法

用 std::jthread 启动后台 COM 线程并在 lambda 中接收 stop_token

为什么典型

线程函数直接以 std::stop_token 为参数,循环用 stop_requested() 决定退出,无需额外的停止标志与手动 join。

摘录

    comThread_ = std::make_unique<std::jthread>(
        [this](std::stop_token stopToken)
        {
            ULONG_PTR contextToken;
            if (CoGetContextToken(&contextToken) == CO_E_NOTINITIALIZED)
            {
                HRESULT hr = CoInitializeEx(nullptr, COINIT_MULTITHREADED);
                if (hr != S_FALSE && hr != RPC_E_CHANGED_MODE && FAILED(hr))
                    CriticalMessageBox(L"Could not initialize COM library: error code 0x%X.", hr);
            }

            while (!stopToken.stop_requested())
            {

备注

生产路径;jthread 保存在 unique_ptr 中,析构时自动请求停止并 join。

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