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 方法?
基础教程推荐
- CString 到 char* 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
