multithreading - C++ Problem with creating thread using Bind -
i have little problem concerning thread , bind interaction.
it simple, have situation
class myclass { // ... // ... void dosome(); void doanother(myclass2, myclass3*); void run() { this->_myt = boost::shared_ptr<boost::thread>( new boost::thread(boost::bind(&myclass::dosome, this))); } // ... boost::shared_ptr<boost::thread> _myt; //... };
ok, till okay. understand bind able bind function or pointer function values, or better, argument values. when call on myclass object function run() new thread started. have question, why bind, in run, takes parameter this, when function dosome not use parameter? because there always, class functions, implicit argument pointer class???
ok, not problem. inside dosome, execution flow of first thread this:
void myclass::dosome() { myclass2 m; myclass3* x = new myclass3; boost::shared_ptr<boost::thread>(new boost::thread( boost::bind(&myclass::doanother, m, x, this))); // line x }
well want exec thread. first question: shared_prt smart pointer, means if dosome() gets out exec flow, thread executed scope persist, being shared_ptr... please tell me correct. second question: compiler gets mad instruction @ line x... problem bind, not passed there... why? member function (i talk doanother) , there 2 arguments plus this... problem?
thank you
you can think of implicit parameter. or can think of
bind
needs call member functionsomething->your_member()
, must tellsomething
is.you don't store
shared_ptr
anywhere, in second example, destroyed after it's created. thread object points destroyed too. thread's execution not bound thread object, work in same way:void myclass::dosome() { myclass2 m; myclass3* x = new myclass3; boost::thread(boost::bind(&myclass::doanother, this, m, x)); // line x }
this starts new thread , looses it, in code. thread continues run.
as see above,
this
should passed second parameter bind.
Comments
Post a Comment