Spawning a thread on a member function
All, Does anyone know how to spawn a thread on member function of an existing object in its constructor. Clearly simple boost::bind does not work. I do not know how to tell the compiler it must take the member function. Thanks for help, Pshemek Kind regards Pshemek Przemyslaw Sliwa Global Foreign Exchange FX Risk Engine Development Merrill Lynch ( +44 (0)20 7995 4182 ) +44 (0)79 6325 2319 * Email: przemyslaw_sliwa@ml.com -------------------------------------------------------- If you are not an intended recipient of this e-mail, please notify the sender, delete it and do not read, act upon, print, disclose, copy, retain or redistribute it. Click here for important additional terms relating to this e-mail. http://www.ml.com/email_terms/ --------------------------------------------------------
Hello Sliwa, This should do it. class Simple { Simple() : th(boost::bind(&Simple::run, boost::ref(*this))) {} void run() { for(;;){ //Thread's work goes here. } } private: boost::thread th; }; _____ From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Sliwa, Przemyslaw (London) Sent: Friday, January 27, 2006 4:25 AM To: boost-users@lists.boost.org Subject: [Boost-users] Spawning a thread on a member function All, Does anyone know how to spawn a thread on member function of an existing object in its constructor. Clearly simple boost::bind does not work. I do not know how to tell the compiler it must take the member function. Thanks for help, Pshemek Kind regards Pshemek Przemyslaw Sliwa Global Foreign Exchange FX Risk Engine Development Merrill Lynch *** +44 (0)20 7995 4182 * +44 (0)79 6325 2319 *** Email: przemyslaw_sliwa@ml.com _____ If you are not an intended recipient of this e-mail, please notify the sender, delete it and do not read, act upon, print, disclose, copy, retain or redistribute it. Click here http://www.ml.com/email_terms/ for important additional terms relating to this e-mail. http://www.ml.com/email_terms/ _____
hello, just a question, I was actually trying the same thing (putting a thread in a class) today, but vc7.1 shows a warning on code like below: warning C4355: 'this' : used in base member initializer list
Simple() : th(boost::bind(&Simple::run, boost::ref(*this))) {}
And in the help it says
<quote>
The base-class constructors and class member
constructors are called before this constructor. In
effect, you've passed a pointer to an unconstructed
object to another constructor. If those other
constructors access any members or call member
functions on this, the result will be undefined. You
should using the this pointer until all construction
has completed
</quote>
suggestions/comments? Can I ignore this?
thanks,
Filip
--- Patrick
Hello Sliwa,
This should do it.
class Simple
{
Simple() : th(boost::bind(&Simple::run, boost::ref(*this)))
{}
void run()
{
for(;;){
//Thread's work goes here.
}
}
private:
boost::thread th;
};
_____
From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Sliwa, Przemyslaw (London) Sent: Friday, January 27, 2006 4:25 AM To: boost-users@lists.boost.org Subject: [Boost-users] Spawning a thread on a member function
All,
Does anyone know how to spawn a thread on member function of an existing object in its constructor.
Clearly simple boost::bind does not work. I do not know how to tell the compiler it must take the member function.
Thanks for help,
Pshemek
Kind regards
Pshemek
Przemyslaw Sliwa Global Foreign Exchange
FX Risk Engine Development Merrill Lynch *** +44 (0)20 7995 4182 * +44 (0)79 6325 2319 *** Email: przemyslaw_sliwa@ml.com
_____
If you are not an intended recipient of this e-mail, please notify the sender, delete it and do not read, act upon, print, disclose, copy, retain or redistribute it. Click here http://www.ml.com/email_terms/ for important additional terms relating to this e-mail. http://www.ml.com/email_terms/
_____
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com
You can ignore this one, since the "this" pointer isn't referenced while constructing a Simple object. If it would be, it isn't guranteed that all members are constructed, yet. Christian
This is even simpler. Is there is reason for "boost::ref(*this)" ? class Simple { Simple() : th( boost::bind( run, this )) {} void run() { for(;;){ //Thread's work goes here. } } private: boost::thread th; };
You are correct the "boost::ref(*this)" is not needed. Simple(boost::bind(&Simple::run, this)) {} -----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Christian Henning Sent: Friday, January 27, 2006 4:47 PM To: boost-users@lists.boost.org Subject: Re: [Boost-users] Spawning a thread on a member function This is even simpler. Is there is reason for "boost::ref(*this)" ? class Simple { Simple() : th( boost::bind( run, this )) {} void run() { for(;;){ //Thread's work goes here. } } private: boost::thread th; }; _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (4)
-
Christian Henning
-
Filip Peters
-
Patrick
-
Sliwa, Przemyslaw (London)