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;
};