A βnormalβ approach is to pack the object and all the arguments of the function into a structure, allocate this structure on the heap, pass an instance of this structure to a function with C binding, and allow this function to call the member function of the object
struct wrap {
char * msg;
Foo ins;
wrap( char* m, const Foo& f ) : msg(m), ins(f) {}
};
extern "C" void* call_func( void *f )
{
std::auto_ptr< wrap > w( static_cast< wrap* >( f ) );
w->ins.func(w->msg);
return 0;
}
int main() {
wrap* w = new wrap( "Hi dude", Foo() );
pthread_t pt;
pthread_create( &pt, NULL, call_func, w );
}
source
share