<分区>
有没有办法确定线程是否仍在运行?由于多种原因,我无法使用 std::future
和 std::async
。
我想到了某种带有超时的 join
,例如尝试加入一个线程3秒,如果成功则认为线程结束,否则认为正在运行。
<分区>
有没有办法确定线程是否仍在运行?由于多种原因,我无法使用 std::future
和 std::async
。
我想到了某种带有超时的 join
,例如尝试加入一个线程3秒,如果成功则认为线程结束,否则认为正在运行。
最佳答案
最接近的是std::thread::joinable()
.从引用文献中引用:
Checks if the thread object identifies an active thread of execution. Specifically, returns
true
ifget_id() != std::thread::id()
. So a default constructedthread
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.
关于c++ - 检查 std::thread 是否仍在运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33943601/