如何可靠地判断 boost 线程是否已退出其 run 方法?

2023-07-20C/C++开发问题
6

本文介绍了如何可靠地判断 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 方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

无法访问 C++ std::set 中对象的非常量成员函数
Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)...
2024-08-14 C/C++开发问题
17

从 lambda 构造 std::function 参数
Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)...
2024-08-14 C/C++开发问题
25

STL BigInt 类实现
STL BigInt class implementation(STL BigInt 类实现)...
2024-08-14 C/C++开发问题
3

使用 std::atomic 和 std::condition_variable 同步不可靠
Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)...
2024-08-14 C/C++开发问题
17

在 STL 中将列表元素移动到末尾
Move list element to the end in STL(在 STL 中将列表元素移动到末尾)...
2024-08-14 C/C++开发问题
9

为什么禁止对存储在 STL 容器中的类重载 operator&()?
Why is overloading operatoramp;() prohibited for classes stored in STL containers?(为什么禁止对存储在 STL 容器中的类重载 operatoramp;()?)...
2024-08-14 C/C++开发问题
6