Ian Collins a écrit :
An off topic but common question. You must pass an extern "C" linkage
function to C library functions that expect a function pointer as one of
their parameters.
You *must* not pass a class member function and *should* not pass a
static member function.
I didn't know about the static member function. Why is that (some
portability issue on their representation I presume)?
You can use template along the following pattern (using intermediary
class or not, using richer templatization ...):
//! execute member function
template<class T>
void* pthread_class_threadifier(void* param)
{
//better to add try/catch otherwise, stack unwinding doesn't happen
in some cases
std::auto_ptr<std:

air<T*,void(T::*)()> > p(
reinterpret_cast<std:

air<T*,void(T::*)()>*>(param));
((p->first)->*(p->second))();
return NULL;
}
//! start thread on given method of given class
template<class T>
int pthread_create_class(pthread_t& thID, T& object, void (T::*method)())
{
std:

air<T*,void (T::*)()>* param=
new std:

air<T*,void (T::*)()>(&object,method);
int ret= pthread_create(&thID,NULL,
pthread_class_threadifier<T>,
reinterpret_cast<void*>(param));
if(ret)delete param;
return ret;
}
And then:
//test class
class A
{
public:
//this method will be threadified
void a()
{
std::cout<<"OK"<<std::endl;
}
};
int main()
{
A a1;
pthread_t pid;
//create thread
int ret=pthread_create_class(pid,a1,&A::a);
if (ret)
{
std::cerr<<"Error: "<<ret<<std::endl;
}
//wait a bit
::sleep(1);
return 0;
}
Michael