首页 > 后端开发 > C++ > 正文

c++20中的std::jthread有什么用_c++20中std::jthread线程使用方法与示例

下次还敢
发布: 2025-10-30 18:55:03
原创
261人浏览过
std::jthread 能自动 join 且支持协作式中断,相比 std::thread 更安全易用。它在析构时自动等待线程结束,避免资源泄漏,并通过 std::stop_token 实现线程取消,简化了多线程编程中的生命周期管理和中断处理。

c++20中的std::jthread有什么用_c++20中std::jthread线程使用方法与示例

在C++20中,std::jthread 是对 std::thread 的改进和补充,主要解决了资源管理和线程自动回收的问题。相比传统的 std::thread,std::jthread 能够自动调用 join(),避免因忘记调用 join 或 detach 导致的程序崩溃或未定义行为。

std::jthread 的核心优势

std::jthread(joining thread)具备以下关键特性:

  • 自动 join:析构时自动等待线程结束,无需手动调用 join()
  • 可协作中断:支持通过 std::stop_tokenstd::stop_source 实现线程的协作式中断
  • 更安全的接口:减少资源泄漏和未定义行为的风险

基本使用方法

创建一个 jthread 和使用 thread 类似,但无需手动管理生命周期。

#include <iostream>
#include <thread>
#include <chrono>

void simple_task() {
    for (int i = 0; i < 3; ++i) {
        std::cout << "Working... " << i << std::endl;
        std::this_thread::sleep_for(std::chrono::milliseconds(500));
    }
}

int main() {
    std::jthread t(simple_task); // 自动 join
    // 析构时会自动等待任务完成
    return 0;
}
登录后复制

输出:

立即学习C++免费学习笔记(深入)”;

Working... 0
Working... 1
Working... 2

支持线程中断的示例

std::jthread 最大的亮点是支持协作式中断。线程函数可以接收一个 std::stop_token,用于检测是否收到停止请求。

芦笋演示
芦笋演示

一键出成片的录屏演示软件,专为制作产品演示、教学课程和使用教程而设计。

芦笋演示34
查看详情 芦笋演示
#include <iostream>
#include <thread>
#include <chrono>

void cancellable_task(std::stop_token stoken) {
    for (int i = 0; i < 10; ++i) {
        if (stoken.stop_requested()) {
            std::cout << "Task cancelled at step " << i << std::endl;
            return;
        }
        std::cout << "Step " << i << std::endl;
        std::this_thread::sleep_for(std::chrono::milliseconds(300));
    }
    std::cout << "Task completed normally." << std::endl;
}

int main() {
    std::jthread t(cancellable_task);

    std::this_thread::sleep_for(std::chrono::milliseconds(1200));

    std::cout << "Requesting stop..." << std::endl;
    t.request_stop(); // 发送停止请求

    return 0;
}
登录后复制

输出可能为:

Step 0
Step 1
Step 2
Step 3
Requesting stop...
Task cancelled at step 4

注意:线程函数必须接受 std::stop_token 作为第一个参数,否则无法响应中断请求。

获取 stop_token 并手动控制

你也可以在线程内部通过 get_stop_token() 获取 token,用于传递给其他函数。

void nested_task(std::stop_token stoken) {
    while (!stoken.stop_requested()) {
        std::cout << "Nested task running..." << std::endl;
        std::this_thread::sleep_for(std::chrono::milliseconds(400));
    }
}

void outer_task(std::stop_token stoken) {
    nested_task(stoken);
}
登录后复制

基本上就这些。std::jthread 让多线程编程更安全、简洁,尤其适合需要自动清理和可取消任务的场景。不再需要担心忘记 join,也不必使用复杂机制实现中断,是 C++20 对并发编程的重要增强。

以上就是c++++20中的std::jthread有什么用_c++20中std::jthread线程使用方法与示例的详细内容,更多请关注php中文网其它相关文章!

c++速学教程(入门到精通)
c++速学教程(入门到精通)

c++怎么学习?c++怎么入门?c++在哪学?c++怎么学才快?不用担心,这里为大家提供了c++速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号