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

  1. you can think of implicit parameter. or can think of bind needs call member function something->your_member(), must tell something is.

  2. 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.

  3. as see above, this should passed second parameter bind.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -