I know it is impossible to directly pass C++ class method
to pthread_create() but is there any simple way to do that ?
The only possible solution at the moment I have found is
some kind of wrapper that requires too much tweaking to get
it into use.
Any ideas ?
--
Greetings, Hristo.
It depends on what you mean by "C++ class method." If you mean a static
member function, then all you have to do is write a non-member function
that calls the static member function, and label the non-member function
'extern "C"'. If you mean a non-static member function, then what you
typically do is write a non-member function (again 'extern "C"') that
interprets its argument as a pointer to an object of some type that
you're interested in, and calls a member function on that object:
class C
{
public:
void show();
};
extern "C" void *f(void *arg)
{
((C*)arg)->show();
}
--
Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
If you already understand something to be impossible, why are you
asking for a simple way to do it?
Anyway, your question is about mixed-language programming using C++
and C libraries. In particular, it's about binding a C++ object as a
callback function needed by a C library. As such, it is topical in
comp.lang.c++.
The comp.programming.threads FAQ also has these entries:
063: What's a godo way of writing threaded C++ classes?
068: How would we put a C++ object into a thread?'
>The only possible solution at the moment I have found is
>some kind of wrapper that requires too much tweaking to get
>it into use.
Writing pretty-looking C++ is not the concern of this newsgroup.
If you have a solution but don't like how it looks, try asking, again,
in comp.lang.c++.
Kaz Kylheku wrote:
--
Greetings, Hristo.
Yes, a C wrapper.
> The only possible solution at the moment I have found is
> some kind of wrapper that requires too much tweaking to get
> it into use.
Post your best attempt and we'll fix it up for you.
> Any ideas ?
Use a wrapper function with C linkage. Have it call a static member
function of the class.
DS
Well, personally, I am not quite sure with respect to all
possible concerns of *this* newsgroup... however, among my
personal concerns writing pretty-working multithreaded C++
programs is on the top of my concerns list. Hence, may I
ask you Kaz (this (the 3rd) time here, since asking it TWO
times on *comp.lang.c++* resulted in NO responses so far)
the following question:
http://groups.google.com/groups?as_umsgid=c29b5e33.0202050759.2b622ae2%40posting.google.com
http://groups.google.com/groups?as_umsgid=3C613C42.8DA490E3%40web.de
"P.S. Kaz, are there any chances/plans that you or some
other linux*threads* developer/maintainer would address
the issue of thread-cancellation/thread-exit
(pthread_exit) and *C++*-stack-unwinding? ;-)"
Please answer!
The problem:
http://groups.google.com/groups?as_umsgid=3A8D120B.73749CEC%40compaq.com
http://groups.google.com/groups?as_umsgid=3C2CC94A.503FB8E2%40web.de
http://groups.google.com/groups?as_umsgid=3C596B75.7163A890%40web.de
regards,
alexander.
You really should direct that to the libc-alpha mailing list. I'd love to
work on this, but guys like Drepper may have some plans in that regard.
My feeling is that this is a sensitive thing, because it leads to fragile
integration between the GCC and Glibc projects. I don't know where the two
groups stand on the issue of settling on an exception handling interface.