RE: [Boost-Users] boost::threads queation
This has little to do with boost (which, again, is why I can answer; at least in part) A member function cannot be addressed like a regular function. Ask yourself this question: With just a function pointer to go by, what will the compiler use for 'this'? The Good News: STL contains a template functor to take care of this for you: "std::mem_fun". I haven't messed with it personally, but the docs sure make it sound like it would do the trick. The Bad News: I'm not so sure the thread class will let you use it. I'm not familiar with the function0<> template, so I don't know whether or not std::mem_fun will work with boost::thread. Anybody care to chime in here? --Mark Storer Software Engineer Cardiff Software #include <disclaimer> typedef std::disclaimer<Cardiff> Discard; -----Original Message----- From: Peter [mailto:yg-boost-users@m.gmane.org] Sent: Monday, June 17, 2002 9:09 PM To: boost-users@yahoogroups.com Subject: [Boost-Users] boost::threads queation Is it possible to pass a pointer of a class member function into a thread ?? I have no problems passing functions (or pointers to functions rather) which are not members of any classes. However when I try to pass member functions, I get compile errors. I am using MSVS 6.0 on win 2000. The following is a simplified model of my situation. I am trying to create a thread inside a function of one class and pass into it, a pointer of a member function of another class: // all necessary includes; class testClass1 { public: // constructor testClass1() { // init code} ~testClass1() {} void fun1() { // some code} }; class testClass2 { testClass2() { // init code} ~testClass2() {} // SEE HERE PLEASE void fun2() { // some code.. boost::thread myThread(&object1.fun1) ; // PROBLEM HERE // more code... } testClass1 object1; }; int main(int argc, char **argv) { testClass2 class2; // some code... class2.fun2(); // more code.... return 0; } Any help would be greatly appreciated. Thanks in advance. Peter Info: http://www.boost.org Wiki: http://www.crystalclearsoftware.com/cgi-bin/boost_wiki/wiki.pl Unsubscribe: mailto:boost-users-unsubscribe@yahoogroups.com Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
--- In Boost-Users@y..., Mark Storer
This has little to do with boost (which, again, is why I can answer; at least in part)
A member function cannot be addressed like a regular function. Ask yourself this question: With just a function pointer to go by, what will the compiler use for 'this'?
The Good News: STL contains a template functor to take care of this for you: "std::mem_fun". I haven't messed with it personally, but the docs sure make it sound like it would do the trick.
The Bad News: I'm not so sure the thread class will let you use it.
I'm not familiar with the function0<> template, so I don't know whether or not std::mem_fun will work with boost::thread.
Any function object that takes no parameters and returns void will work here. This includes std::mem_fun. I might suggest boost::bind instead, though. It's a little more flexible then the adapters provided by the standard. Bill Kempf
Thanks guys. I asked the same question on the other newsgroup (for
developers i think) and someone suggested thread::bind as well. That worked
very well.
"bill_kempf"
--- In Boost-Users@y..., Mark Storer
wrote: This has little to do with boost (which, again, is why I can answer; at least in part)
A member function cannot be addressed like a regular function. Ask yourself this question: With just a function pointer to go by, what will the compiler use for 'this'?
The Good News: STL contains a template functor to take care of this for you: "std::mem_fun". I haven't messed with it personally, but the docs sure make it sound like it would do the trick.
The Bad News: I'm not so sure the thread class will let you use it.
I'm not familiar with the function0<> template, so I don't know whether or not std::mem_fun will work with boost::thread.
Any function object that takes no parameters and returns void will work here. This includes std::mem_fun. I might suggest boost::bind instead, though. It's a little more flexible then the adapters provided by the standard.
Bill Kempf
participants (3)
-
bill_kempf
-
Mark Storer
-
Peter