我有一个类对象,在某些情况下只需要启动一个线程。在析构函数中,无论何时有线程都可以方便地知道。问题是,我如何检测std :: thread对象是否是一个有效的线程。
class MyClass
{
public:
~MyClass()
{
// Attention pseudocode!!!!!
if(mythread is a valid object)
mythread.join();
if(mythread was a valid object in its lifetime)
Do some stuff
}
void DoSomeStuff()
{
// May or may not create a thread
}
private:
std::thread mythread;
};