Object oriented callbacks and differences in member vs non-member function pointers
Hi,
I have great respect for the contributors of this newsgroup and have a
question that I can't seem to find an answer for, I need to understand
if it there is anything in the C++ standard that prevents or dictates
how compilers implement function pointers for stand alone versus
member functions. Completely ignoring ABI and name mangling issues
here, one would imagine that the underlying address or function
pointer for member and non member functions would be identical in size
(surely it would be a word whose size won't change for a given
platform). My question is can we rely on this equivalence (in size of
function pointers for members versus non member function)? The reason
for asking this question is to find out if the following trick would
work portably without running afoul of C++ standard or not:
Let's declare a structure as follows:
union call_back
{
void (thread::*filler)();
void * (*address)(void *);
} routine;
This structure allows us to store the address of a member function of
class thread in routine.filler, while allowing us to pass
routine.address (to pthread_create() for example) as a callback
function with a non-member signature. The questions are: 1. Is there a
better way to pass a member function address where the underlying
library expects a non-member signature, perhaps a better way of
casting a member function pointer to non-member function pointer and
2. Are there cases where this approach may break?
Before you recommend using a static member function or non-member
dispatcher as a solution, I know about those, what I am trying to
achieve is stay object oriented in my callback framework: Take a quick
look at the following URL for a simple non-template version of what I
am trying to achieve along with some explanation:
http://metacoder.blogspot.com/2007/06/functor-based-multithread-callbacks-in.html
But a better more decoupled template based solution which I haven't
had time to write about on this blog is as under:
#include <iostream>
#include <pthread.h>
#include <unistd.h>
template <class task> class thread
{
task& worker;
pthread_t id;
pthread_attr_t attr;
union call_back
{
void (thread<task>::*filler)();
void * (*address)(void *);
} routine;
public:
thread(task& p_worker) : worker(p_worker)
{
routine.filler = &thread<task>::go;
pthread_attr_init(&attr);
}
void start()
{
pthread_create (&id, &attr, routine.address, this);
}
void go() { worker(); }
int join()
{
return pthread_join(id, NULL);
}
~thread()
{
pthread_attr_destroy(&attr);
}
};
class worker
{
std::string msg;
public:
worker(std::string p_msg) : msg(p_msg) {}
void operator () () { std::cout << msg; };
};
int main(int argc, char *argv[])
{
worker m1("Hello");
worker m2(" ");
worker m3("world");
thread<worker> t1(m1);
thread<worker> t2(m2);
thread<worker> t3(m3);
t1.start();
t2.start();
t3.start();
t1.join();
t2.join();
t3.join();
return 0;
}
I would appreciate if you could focus on the union routine and comment
on either a better way of doing this or atleast point out why it could
break under certain circumstance and whether it runs afoul of the
standard?
Thanks a lot.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]