The most concise solution is to define, in a header file shared by all your code:
template <typename T, void (T::*M)()>
void* thunk(
void* p)
{
T* pt = static_cast<T*>(p);
(pt->*M)();
return 0;
}
You probably want to define 4 versions: one each where the thunk returns void and void*
, and one each where the member function returns void and void*
. That way the compiler can match the best one, depending on the circumstances (and in fact it will complain if everything doesn't match.)
Then all you have to type every time you run into one of these situations is:
pthread_create(&pid, 0, &thunk<Whatever, &Whatever::doit>, &w);
This will even work when the method is private, as long as the method is referenced from within the class's code. (If not, I have to wonder why the code is referencing a private method.)