I occasionally come across the need for creating threads that use a C++ member function as its entry point. The traditional way of doing this is to define a static member function as part of the class, and then have that method call the member function. For instance:

This is fine for one-offs, but I dislike the need for a separate static member function for each thread I want to create, especially for objects that kick off multiple threads. It just doesn't seem very elegant to me, and it needlessly bloats the code.

In C++, member functions are always called with an implicit argument: this. Given that pthread_create allows us to pass a single argument to the thread, why can't we just do something like this?

Well, the answer lies in the type the compiler assigns to the function pointer for Thread2::memberFunction. In our case, the type is void *(Thread2::*)(void), while the type expected by pthread_create is void *(*)(void *). Attempting to cast the member function pointer doesn't work, since the compiler ends up complaining. However, there is a way we can make it work, if we can live with a bit of hackery, the risk of things blowing up in our face at any time, and probably some other caveats that I'm not presently aware of.

The solution is to define two function pointer types, and then circumvent the compiler by manually copying the function pointer to a variable of the appropriate type. Consider the following:

The above compiles and works correctly, at least on Mac OS X and Linux, and so far without having blown up in my face. Good times! What is happening behind the scenes here is, essentially that the compiler copies the data in t to p, circumventing the typecheck in the process. We could also have implemented the above using memcpy: memcpy(&p, &t, sizeof(void*)).

Now, as mentioned above, there are probably many good reasons not to do this. I have found this to be a good resource: Pointers to member functions. The FAQ lists a number of good reasons NOT to use them. I still do though, at least for the special case of creating threads.

About me
My name is Daniel Stødle. I live in Tromsø, Norway, at 69.66° North. By day, I work as a researcher at the Northern Research Institute in Norway; by night, I run my company SCSC. I do most of my development on and for Mac OS X. My research is geared towards interaction with and visualization of geospatial data. Read more on my personal page.