I want to write a wrapper for boost thread to specialize a threading model. My run()
function is going to be a member function of the same class that is using boost::thread
as the aggregate thread object. Consider:
class Thread {
public:
Thread(...) : m_thread(&Thread::run, this) {}
private:
void run() { ... }
boost::thread m_thread;
};
This is potentially dangerous because this
is not yet fully constructed. However, if I can guarantee that all members of the object used by run()
are initialized prior-to initialization of the boost thread, could this actually be considered safe?
The only workaround I can think of that guarantees safety is to have a subclass that guarantees full construction of an object that can be used by the constructor of Thread
:
class Thread {
public:
Thread(...) : m_impl(...), m_thread(&ThreadImpl::run, &m_impl) {}
private:
class ThreadImpl {
ThreadImpl(...) { }
void run() { ... }
}
ThreadImpl m_impl;
boost::thread m_thread;
};
Is there a common way to do this? The ThreadImpl
class seems like a lot of overhead for such a trivial issue.