跳转至

ev-hyprland-thread-waitpid-002

字段 值
项目 hyprwm/Hyprland
固定 commit 0aa6745f705f
路径 start/src/core/Instance.cpp
行段 L76-L87
特性 std::thread
许可证 BSD-3-Clause
代码来源 项目源码
级别 manual
审核 zyuhao7 @ 2026-11-03
测试代码 否
发现方式 manual

用法

用 std::thread 运行阻塞的 waitpid 循环,避免阻塞启动线程

为什么典型

waitpid 是阻塞调用,放在主线程会卡住启动流程;把循环交给独立线程后,主线程可以继续初始化。线程按值捕获 this,阻塞调用与界面初始化解耦。

摘录

    m_hlThread = std::thread([this] {
        while (true) {
            int status = 0;
            int ret    = waitpid(m_hlPid, &status, 0);
            if (ret == -1) {
                g_logger->log(Hyprutils::CLI::LOG_ERR, "Couldn't waitpid for hyprland: {}", strerror(errno));
                break;
            }

            if (WIFEXITED(status))
                break;
        }

备注

生产代码;线程成员随后由拥有者 join,lambda 捕获 this 访问成员状态。展示了“把阻塞系统调用移出主线程”的典型动机。

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