std::jthread 能自动 join 且支持协作式中断,相比 std::thread 更安全易用。它在析构时自动等待线程结束,避免资源泄漏,并通过 std::stop_token 实现线程取消,简化了多线程编程中的生命周期管理和中断处理。

在C++20中,std::jthread 是对 std::thread 的改进和补充,主要解决了资源管理和线程自动回收的问题。相比传统的 std::thread,std::jthread 能够自动调用 join(),避免因忘记调用 join 或 detach 导致的程序崩溃或未定义行为。
std::jthread 的核心优势
std::jthread(joining thread)具备以下关键特性:
- 自动 join:析构时自动等待线程结束,无需手动调用 join()
- 可协作中断:支持通过 std::stop_token 和 std::stop_source 实现线程的协作式中断
- 更安全的接口:减少资源泄漏和未定义行为的风险
基本使用方法
创建一个 jthread 和使用 thread 类似,但无需手动管理生命周期。
#include#include #include 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... 0Working... 1
Working... 2
支持线程中断的示例
std::jthread 最大的亮点是支持协作式中断。线程函数可以接收一个 std::stop_token,用于检测是否收到停止请求。
#include#include #include 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 0Step 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 对并发编程的重要增强。











