#include <boost/thread.hpp>class BaseThreadObject{ protected://---------- Protected Properties ----------------- boost::thread* m_thread;//------------ Protected Methods ------------------ public://-- Constructors, Destructor & Support Methods --- BaseThreadObject(); virtual ~BaseThreadObject(); void Startup(); void Shutdown(); //-------- Get's -------------------------- //-------- Set's --------------------------//---------- Public Methods ----------------------- void operator()(); virtual void ThreadMain() = 0;};// ------------------------------------------------// ------------------------------------------------// ------------------------------------------------void BaseThreadObject::operator()(){ ThreadMain();}// ------------------------------------------------// ------------------------------------------------// ------------------------------------------------void BaseThreadObject::Startup(){ m_thread = new boost::thread(boost::ref(*this));// m_thread = new boost::thread(*this);}// ------------------------------------------------// ------------------------------------------------// ------------------------------------------------void BaseThreadObject::Shutdown(){ m_thread->join();}// ------------------------------------------------// ------------------------------------------------// ------------------------------------------------void BaseThreadObject::ThreadMain(){ // whatever}
The Startup launches the thread with the class as a ref, to prevent it from being copied (boost takes 10(!!) copies when launching). Also, I hoped that with a ref, that I could have preserved the inheritance, so I could call derived classes through virtual methods.
However, when I call a virtual method, even if it is implemented in the base class, like the operator()() calls ThreadMain() (which I hoped I could then freely derive for new classes, while preserving basic threading functionality in this baseclass alone), it will fail with this message:
"Unhandled exception at 0x00000000 in [prog-name].exe: 0xC0000005: Access violation reading location 0x00000000"
I have similar problems when I don't use the ref and allows it to copy. It will only copy the class, that is directly inputted, not the classes it derives from.
Is there any way I can create a class like this with boost, that retains an inheritance structure?