检查 std::thread 是否仍在运行 [英] Check if std::thread is still running

查看:335
本文介绍了检查 std::thread 是否仍在运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法确定线程是否仍在运行?由于多种原因,我无法使用 std::futurestd::async.

Is there a way to determine if a thread is still running? I cannot use std::future and std::async for several reasons.

我想到了某种带有超时的join,例如尝试加入一个线程3秒,如果成功则认为该线程已完成,否则认为该线程正在运行.

I thought of some kind of join with timeout, e.g. try to join a thread for 3 seconds, if it succeeds, the thread is considered finished, otherwise it is considered running.

推荐答案

最接近的是 std::thread::joinable().从参考中引用:

The closest you can get is std::thread::joinable(). To cite from the reference:

检查线程对象是否标识了一个活动的执行线程.具体来说,如果 get_id() != std::thread::id(),则返回 true.所以默认构造的 thread 是不可连接的.
已完成代码执行但尚未加入的线程仍被视为活动的执行线程,因此是可加入的.

Checks if the thread object identifies an active thread of execution. Specifically, returns true if get_id() != std::thread::id(). So a default constructed thread is not joinable.
A thread that has finished executing code, but has not yet been joined is still considered an active thread of execution and is therefore joinable.

这篇关于检查 std::thread 是否仍在运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆