Member function as a thread function

Hi, I've had a good search on the mailing list and while some posts have got close to my problem and given me a few glues, my code does't quite work the way I want it to. My goal is to have a (non-static) class member function run in a child thread + passing a parameter to it. The code I've got at the moment goes something like this: void MyClass::spawn_threads() { for ( int ii = 0; ii < num_threads; ++ii) { threadGroup.create_thread( boost::bind( ChildThreadFunc, _1, ii)); } } void MyClass::ChildThreadFunc( int &threadNumber) { // Does some work } I get the following error on the create_thread line: error: no matching function call to bind(<unknown type>, boost::arg<1>&, int&) cadiates are: .... boost:bind(F, A1, A2)[ with F= void (MyClass::*)(int&), A1=boost:arg<1>, A2=int] I'm confused by the <unknown type> business. How do I tell it the correct type? Maybe someone could point out where I've erred. Cheers, Steve.

You need to correctly specify the member function pointer, and pass in
the 'this' pointer. Try boost::bind(&MyClass::ChildThreadFunc, this,
ii)
On 2/14/06, Steven Wooding
Hi,
I've had a good search on the mailing list and while some posts have got close to my problem and given me a few glues, my code does't quite work the way I want it to.
My goal is to have a (non-static) class member function run in a child thread + passing a parameter to it. The code I've got at the moment goes something like this:
void MyClass::spawn_threads() { for ( int ii = 0; ii < num_threads; ++ii) { threadGroup.create_thread( boost::bind( ChildThreadFunc, _1, ii)); } }
void MyClass::ChildThreadFunc( int &threadNumber) { // Does some work }
I get the following error on the create_thread line:
error: no matching function call to bind(<unknown type>, boost::arg<1>&, int&) cadiates are: .... boost:bind(F, A1, A2)[ with F= void (MyClass::*)(int&), A1=boost:arg<1>, A2=int]
I'm confused by the <unknown type> business. How do I tell it the correct type?
Maybe someone could point out where I've erred.
Cheers,
Steve.
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- Cory Nelson http://www.int64.org

Steven Wooding wrote:
Hi,
I've had a good search on the mailing list and while some posts have got close to my problem and given me a few glues, my code does't quite work the way I want it to.
My goal is to have a (non-static) class member function run in a child thread + passing a parameter to it. The code I've got at the moment goes something like this:
void MyClass::spawn_threads() { for ( int ii = 0; ii < num_threads; ++ii) { threadGroup.create_thread( boost::bind( ChildThreadFunc, _1, ii));
You need threadGroup.create_thread( boost::bind( &MyClass::ChildThreadFunc, this, ii ) ); here.
} }
void MyClass::ChildThreadFunc( int &threadNumber) { // Does some work }

On Tuesday 14 February 2006 22:34, Steven Wooding wrote:
void MyClass::spawn_threads() { for ( int ii = 0; ii < num_threads; ++ii) { threadGroup.create_thread( boost::bind( ChildThreadFunc, _1, ii)); } }
void MyClass::ChildThreadFunc( int &threadNumber) { // Does some work }
C++ question. Use &MyClass::ChildThreadFunction as bind's first argument. You will probably also run into trouble with passing the int by reference (why?). Pass it by value. Marc -- Marc Mutz -- marc@klaralvdalens-datakonsult.se, mutz@kde.org Klarälvdalens Datakonsult AB, Platform-independent software solutions

Marc Mutz wrote:
On Tuesday 14 February 2006 22:34, Steven Wooding wrote:
void MyClass::spawn_threads() { for ( int ii = 0; ii < num_threads; ++ii) { threadGroup.create_thread( boost::bind( ChildThreadFunc, _1, ii)); } }
void MyClass::ChildThreadFunc( int &threadNumber) { // Does some work }
C++ question. Use &MyClass::ChildThreadFunction as bind's first argument. You will probably also run into trouble with passing the int by reference (why?).
Because MyClass::ChildThreadFunc is run in a different thread, in a separate timeline, and when it runs the variable ii may no longer exist (?). That's my guess, anyway.
Pass it by value.
That does seem more appropriate. Regards, João

On Wednesday 15 February 2006 01:03, João Abecasis wrote:
C++ question. Use &MyClass::ChildThreadFunction as bind's first argument. You will probably also run into trouble with passing the int by reference (why?).
Because MyClass::ChildThreadFunc is run in a different thread, in a separate timeline, and when it runs the variable ii may no longer exist (?).
I'm sorry. This wasn't mean to be a GotW-style 'why?', but just short for "why did you do it that way?" :) But thanks nonetheless, yes, that's what I had in mind :) Marc -- Marc Mutz -- marc@klaralvdalens-datakonsult.se, mutz@kde.org Klarälvdalens Datakonsult AB, Platform-independent software solutions

Marc Mutz wrote:
On Wednesday 15 February 2006 01:03, João Abecasis wrote:
C++ question. Use &MyClass::ChildThreadFunction as bind's first argument. You will probably also run into trouble with passing the int by reference (why?).
Because MyClass::ChildThreadFunc is run in a different thread, in a separate timeline, and when it runs the variable ii may no longer exist (?).
I'm sorry. This wasn't mean to be a GotW-style 'why?', but just short for "why did you do it that way?" :)
But thanks nonetheless, yes, that's what I had in mind :)
Note that this is not a problem in this particular case; the reference is bound to the internal copy of ii that is stored by boost::bind into the created function object.

Thanks everyone. threadGroup.create_thread( boost::bind( &MyClass::ChildThreadFunc, this, ii ) ); Works fine. I've changed it to pass by value as it is only an int in my simple example, but could be something bigger later. Thanks for the insight on passing by reference. Cheers, Steve.

I have code which calls unary_function with the
count_if() algorithm. Like this:
class do_hasContainerBlock : public
unary_function
&)â
Can you please tell me how to fix that? __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com

yinglcs2@yahoo.com wrote:
int count = count_if (bdl.begin(), bdl.end(), bind( &BlockData::isContainer, _1) );
This looks like it should work, assuming bool BlockData::isContainer() const;
But this code does not copmile,I get this error: g++ -O0 -g3 -Wall -c -fmessage-length=0 -oBlockDataUtils.o ../BlockDataUtils.cpp ../BlockDataUtils.cpp: In function â?~bool hasContainerBlock(BlockDataList&)â?T: ../BlockDataUtils.cpp:95: error: no matching function for call to â?~bind(<unknown type>, const boost::lambda::lambda_functor
&)â?T
Since you are using the _1 placeholder from the Lambda library, you need to use boost::lambda::bind as well. Currently boost::bind and Lambda do not recognize each other's placeholders (and don't interoperate in general).

--- Peter Dimov
yinglcs2@yahoo.com wrote:
int count = count_if (bdl.begin(), bdl.end(), bind( &BlockData::isContainer, _1) );
This looks like it should work, assuming
bool BlockData::isContainer() const;
But this code does not copmile,I get this error: g++ -O0 -g3 -Wall -c -fmessage-length=0 -oBlockDataUtils.o ../BlockDataUtils.cpp ../BlockDataUtils.cpp: In function �?~bool hasContainerBlock(BlockDataList&)�?T: ../BlockDataUtils.cpp:95: error: no matching function for call to �?~bind(<unknown type>, const
boost::lambda::lambda_functor
&)�?T
Since you are using the _1 placeholder from the Lambda library, you need to use boost::lambda::bind as well. Currently boost::bind and Lambda do not recognize each other's placeholders (and don't interoperate in general).
I can't add this to my code:
using namespace boost::lambda::bind;
It said :
g++ -O0 -g3 -Wall -c -fmessage-length=0
-oBlockDataUtils.o ../BlockDataUtils.cpp
../BlockDataUtils.cpp:15: error: expected
namespace-name before â;â token
../BlockDataUtils.cpp:15: error: â<type error>â is
not a namespace
And I still can't get my code to compile:
code:
int count = count_if (bdl.begin(), bdl.end(), bind(
&BlockData::isContainer, _1));
error:
g++ -O0 -g3 -Wall -c -fmessage-length=0
-oBlockDataUtils.o ../BlockDataUtils.cpp
../BlockDataUtils.cpp: In function âbool
hasContainerBlock(BlockDataList&)â:
../BlockDataUtils.cpp:95: error: no matching function
for call to âbind(<unknown type>, const
boost::lambda::lambda_functor
&)â make: *** [BlockDataUtils.o] Error 1
Any help is appreicate.
_______________________________________________ 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

yinglcs2@yahoo.com wrote:
And I still can't get my code to compile: code: int count = count_if (bdl.begin(), bdl.end(), bind( &BlockData::isContainer, _1));
You need to post a complete example that fails. I don't see anything wrong with that line.
participants (7)
-
Christian Henning
-
Cory Nelson
-
João Abecasis
-
Marc Mutz
-
Peter Dimov
-
Steven Wooding
-
yinglcs2@yahoo.com