
Hi all This mail is in continuation to the earlier mail I had written asking for help in writting a thread wrapper for boost.threads. We already have an existing interface to which we would like our wrapper for boost.threads to adhere to. We have a runnable class which is inherited by the applications wanting to create threads and they override the run method. This is how our application currently creates threads. Thanks Peter for your earlier mail explaining on how to use boost::shared_ptr and boost::bind Your code for a Runnable interface
struct Runnable { virtual ~Runnable() {} virtual void run() = 0; }; We have a class Runnable with nearly the same code as above. To make a dummy implementation, I made the run methd a normal function. Nothing derives from it.. run is what I want to execute in a thread.
void execute_in_thread( boost::shared_ptr<Runnable> pr ) { boost::thread th( boost::bind( &Runnable::run, pr ) ); } I am creating a thread in MyThread class using the following code.
--- MyThread.h ---- typedef boost::shared_ptr<Runnable> myPtr; class MyThread { public: MyThread( ); ~MyThread( ); private: myPtr p1; } --- MyThread.cpp ---- MyThread::MyThread( ) : p1( new Runnable() ) { cout << "MyThread::MyThread( ) constructor " << endl; boost::thread th( boost::bind( &Runnable::run, p1) ); } ---- Runnable.cpp --- // this is the only function in Runnable.h void Runnable::run() { cout << "Runnable::run() YUPPIE! "<< endl; } ------ end ----------- The code compiles, but nothing happens.. I am missing something here ?? Is there something else I need to do to start my thread?? Thanks Dhanvi