How can I tell reliably if a boost thread has exited its run method?(如何可靠地判断 boost 线程是否已退出其 run 方法?)
问题描述
我认为 joinable 会表明这一点,但是,情况似乎并非如此.
I assumed joinable would indicate this, however, it does not seem to be the case.
在一个工人类中,我试图表明它仍在通过谓词处理:
In a worker class, I was trying to indicate that it was still processing through a predicate:
bool isRunning(){return thread_->joinable();}
一个已经退出的线程不是不能加入吗?我错过了什么...... boost thread::joinable 是什么意思?
Wouldn't a thread that has exited not be joinable? What am I missing... what is the meaning of boost thread::joinable?
推荐答案
由于您可以在线程终止后加入,所以 joinable() 仍然会返回 true,直到您调用 join() 或 detach().如果您想知道某个线程是否仍在运行,您应该能够调用 timed_join,等待时间为 0.请注意,这可能会导致竞争条件,因为线程可能会在调用后立即终止.
Since you can join a thread even after it has terminated, joinable() will still return true until you call join() or detach(). If you want to know if a thread is still running, you should be able to call timed_join with a wait time of 0. Note that this can result in a race condition since the thread may terminate right after the call.
这篇关于如何可靠地判断 boost 线程是否已退出其 run 方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何可靠地判断 boost 线程是否已退出其 run 方法?
基础教程推荐
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01