0
class scoped_thread
{
    std::thread t;
public:
    explicit scoped_thread( std::thread t_ ) :
        t( std::move( t_ ) )
    {
        if ( !t.joinable() )
            throw std::logic_error( "thread is not joinable" );
    }

    ~scoped_thread()
    {
        t.join();
    }
    scoped_thread( scoped_thread const& ) = delete;
    scoped_thread& operator=( scoped_thread const& ) = delete;
};

Question> What else should I define for the class of scoped_thread so that I can use it as one of the member variable as follows?

class T
{
...
   void printMe() {}
   void init()
   {
        m_thread = scoped_thread(std::thread(&T::printMe, this));
   }

private:
   scoped_thread m_thread;
};

Or I have to simply initialize the member variable in the list? I think this is not good way since the calling function will use uninitialized member variables.

class T
{
public:
   T() : m_thread(std::thread(&T::printMe, this)) {}
...
   void printMe() {
          // potential use some uninitialized member variables
   }
   void init()
   {
   }

private:
   scoped_thread m_thread;
};
T.C.
  • 133,968
  • 17
  • 288
  • 421
q0987
  • 34,938
  • 69
  • 242
  • 387
  • What do you want to achieve exactly ? – Richard Dally May 17 '16 at 13:52
  • 1
    It seems you're looking for the [Rule of 3/4/5](http://stackoverflow.com/questions/4782757/rule-of-three-becomes-rule-of-five-with-c11) – Brian Rodriguez May 17 '16 at 14:03
  • @RichardDally I want to define a scoped_thread member variable. – q0987 May 17 '16 at 14:20
  • 2
    @q0987 Clearly that is not exactly what you want to achieve, as you have examples of it being used as a member variable. When asked a question, repeating the title of the post above is unlikely to answer it. Give an actual [MCVE] that demonstrates what you tried, and why your solution isn't good enough. Your code should be your best attempt, you should actually try it, and you should get to the point where you find a problem you don't know how to solve. Also include motivation why you are doing it, outside of the technical problem you run into. – Yakk - Adam Nevraumont May 17 '16 at 14:46

0 Answers0