Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

C++ and pthread_create()

0 views
Skip to first unread message

Hristo Grigorov

unread,
Feb 10, 2002, 3:05:03 PM2/10/02
to
Hello!

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.

Pete Becker

unread,
Feb 10, 2002, 3:17:21 PM2/10/02
to
Hristo Grigorov wrote:
>
> Hello!
>
> I know it is impossible to directly pass C++ class method
> to pthread_create() but is there any simple way to do that ?

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)

Kaz Kylheku

unread,
Feb 10, 2002, 3:24:01 PM2/10/02
to
In article <a46jnp$o71$1...@news.kolumbus.fi>, Hristo Grigorov wrote:
>Hello!
>
>I know it is impossible to directly pass C++ class method
^^^^^^^^^^^

>to pthread_create() but is there any simple way to do that ?
^^^^^^^^^^

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++.

Hristo Grigorov

unread,
Feb 10, 2002, 3:29:47 PM2/10/02
to

Thanx! Your reply really helped me a lot :P

Kaz Kylheku wrote:

--
Greetings, Hristo.

Hristo Grigorov

unread,
Feb 10, 2002, 3:39:26 PM2/10/02
to

You gave me one nice idea... Thanx a lot!

David Schwartz

unread,
Feb 10, 2002, 7:22:22 PM2/10/02
to
Hristo Grigorov wrote:
>
> Hello!
>
> I know it is impossible to directly pass C++ class method
> to pthread_create() but is there any simple way to do that ?

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

Alexander Terekhov

unread,
Feb 11, 2002, 5:03:18 AM2/11/02
to
k...@accton.shaw.ca (Kaz Kylheku) wrote in message news:<BFA98.8029$Cg5.4...@news1.calgary.shaw.ca>...
[...]

> 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++.

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.

Kaz Kylheku

unread,
Feb 11, 2002, 11:15:59 AM2/11/02
to
In article <c29b5e33.0202...@posting.google.com>, Alexander

Terekhov wrote:
>k...@accton.shaw.ca (Kaz Kylheku) wrote in message news:<BFA98.8029$Cg5.4...@news1.calgary.shaw.ca>...
>"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? ;-)"

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.

0 new messages