How to check if std :: thread is really

I have a class object, which in some cases only requires a thread to start. In the destructor, it would be convenient for me to know whenever there is / was a thread. The question is how to determine if the object was std :: thread or is a valid 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;
};
+4
source share
1 answer

I believe you are looking for a function joinable:

~MyClass()
{
    if (t.joinable()) { t.join(); }
}
+7
source

All Articles