Your code sample is fine. You will need a thread function for each different non-static class method you want to call in separate threads.
boost:bind will not help you whatsoever... AfxBeginThread would have to be a C++ template function, otherwise it cant be compatible with boost::bind or C++11 lambdas with captures.
One Alternative is to create a struct, with an enum for each class/method combination you will have, but this still requires you to manually add code to both the enum and the callback function for each class/method combination. However its not that much less code than creating a separate thread function for each class/method combination.
struct ThreadData
{
LPVOID object;
enum ObjectCallType {
Foo_Foo,
Foo_Bar
} objectCallType;
LPVOID* param;
ThreadData( LPVOID pobject, ObjectCallType poct, LPVOID* pparam=0 )
:object(pobject), objectCallType(poct), param(pparam) {}
};
UINT MyThreadProc( LPVOID pParam )
{
TheadData* thData = (ThreadData*)pParam;
try
{
switch( thData->objectCallType )
{
case ThreadData::Foo_Foo:
Foo* foo = (Foo*)thData->object;
foo->foo();
break;
case ThreadData::Foo_Bar:
Foo* foo = (Foo*)thData->object;
foo->bar( thData->param );
break;
default:
throw std::exception("unhandled method call type");
}
}
catch( std::exception& e )
{
std::cerr << e.what() << std::endl;
delete thData;
return 1;
}
delete thData;
return 0;
}
//usage:
AfxBeginThread(MyThreadProc, new ThreadData(myFooObject,ThreadData::Foo_Bar,myFooCallParam));
Boost example (untested):
boost::thread myFooFooThread( boost::bind( &Foo::Foo, myFooObject ) );