Subj : Re: How to pass class-member function to pthread_create(....)? To : comp.programming.threads From : Joe Seigh Date : Fri Jul 15 2005 02:59 pm Vyacheslav Kononenko wrote: > Joe Seigh wrote: > >>You'll have to use a static function (with external "C" I believe) that >>is used as the function parameter for pthread_create. I will have to >>invoke the method. The object and other parameterw will have to be passed >>by arg. If just object, a simple cast will do, otherwise they have to be >>passed in a structure. >> >>-- >>Joe Seigh > > > It has to match signature that "pthread_create" expects so it has to be > static method or generic function and extern "C" is not necessary. For > example: > > class foo { > void thread_function(); > public: > static void *static_func( void * ptr) { reinterpret_cast( ptr > )->thread_function(); } > }; > > ... > foo *test; > pthread_create( thread, attr, foo::thread_func, test ); > ... > void * start(void * arg) { ((MyObj *)arg)->func(); return NULL; // or something returned from func } or void * start(void * arg) { struct somestruct *args = (struct somestruct *)arg; args->obj->func(args->arg1, ...); return NULL; } You have to explicitly construct the object method invocation. And pthreads is a *C* library. It can't make calls to C++ methods. If you figure out how to do that in a portable manner in C, let the posix pthreads committee know. -- Joe Seigh When you get lemons, you make lemonade. When you get hardware, you make software. .