Threading with Classes

Threading with Classes

Post by Stefan Siefe » Fri, 24 Aug 2001 22:57:14



Hi,

I have a question concerning threading an Classes.

First I will describe you the present state of my Program.
My Main Prog is a C++ Class, which controlls different subclasses. I have a
private memberfunction called handle_client. Threading in this class fails,
what I try is this:
class cMain
{
        ....
        int main()
        {
                res = pthread_create(&handle_thread, NULL, handle_client,(void * )
fd_temp);
....
        }
        void * handle_client(void * arg)
        {
                ...
        }      

Quote:}

what I get is this:

no matches converting function `handle_client' to type
`void *(*) (void *)'
candidates are: void *cMain::handle_client (void *)

If I try the same with a global function handle_client (e.g. not a class
member) this works fine.

IMHO this looks to me like I can't thread within a class with an private
class function. What I would like to know, is why is this like it is? Can
somebody explain this to me or give me a hint or link where I can get Infos
about?

Thx,

Stefan Siefert

 
 
 

Threading with Classes

Post by David Schwart » Sat, 25 Aug 2001 03:56:27



> IMHO this looks to me like I can't thread within a class with an private
> class function. What I would like to know, is why is this like it is? Can
> somebody explain this to me or give me a hint or link where I can get Infos
> about?

        You are correct, you can't. The reason is that thread start functions
are defined to have C linkage and take a single pointer. What's to
explain? Create a global "C" function that's a friend of the class.

        DS

 
 
 

Threading with Classes

Post by Wim Lauwer » Sat, 25 Aug 2001 16:10:40




> > IMHO this looks to me like I can't thread within a class with an private
> > class function. What I would like to know, is why is this like it is? Can
> > somebody explain this to me or give me a hint or link where I can get Infos
> > about?

>         You are correct, you can't. The reason is that thread start functions
> are defined to have C linkage and take a single pointer. What's to
> explain? Create a global "C" function that's a friend of the class.

>         DS

And if you really _must_ have the object executing inside the thread,
use a static class function to start the thread, pass it the pointer to
your object as argument and launch the correct function from there,
using this object pointer. A friend is probably cleaner though.

Wim

 
 
 

Threading with Classes

Post by Stefan Siefe » Sat, 25 Aug 2001 23:09:01


Thx for the suggestions. I did solve the problem this way. But what I would
like to understand is what the technical problem is why this above doesn't
work (I'm not sure but I thought I have done this long time ago in
Java...).

Thx,

Stefan Siefert

 
 
 

Threading with Classes

Post by Alan Veas » Sun, 26 Aug 2001 17:04:30



> Thx for the suggestions. I did solve the problem this way. But what I would
> like to understand is what the technical problem is why this above doesn't
> work (I'm not sure but I thought I have done this long time ago in
> Java...).

> Thx,

> Stefan Siefert

Hi. I hope this helps. I too just started using threads and classes.
My attempt is to create a thread class that hides the pthreads
functions with more generic names (start, stop, cancel, etc). I don't
fully understand the technical part but here goes: pthread_create
wants a pointer to a function that returns void * and takes void * as
a parameter. The problem with passing a member function to it is that
the function name is actually class::function instead of just
function. Making the member function static somehow gets around. From
what I have read, making a member function static removes the this
pointer from being passed to it. My accomplishments sped up after I
found a thread class called ObjectThread. It can be found at
http://sourceforge.net/projects/objectthread. Dude, this source rocks.
I learned MUCH more about C++ looking over the source. There may be
better and to the experts this may not be very good, but it could be a
lifesaver. Anyway, I hope this helps and good luck.
 
 
 

Threading with Classes

Post by ?yvind R?tvol » Sun, 26 Aug 2001 18:44:39


[snip]

Quote:> (I'm not sure but I thought I have done this long time ago in
> Java...).

But ofcourse - Java has a object based threading model.  If you can
find a object based threading library for C++ (or make it yourself)
you could do something like this.

--
?yvind

 
 
 

Threading with Classes

Post by David Butenho » Fri, 07 Sep 2001 03:28:16



> I don't
> fully understand the technical part but here goes: pthread_create
> wants a pointer to a function that returns void * and takes void * as
> a parameter. The problem with passing a member function to it is that
> the function name is actually class::function instead of just
> function. Making the member function static somehow gets around. From
> what I have read, making a member function static removes the this
> pointer from being passed to it.

That may be true on SOME implementations of C++, but it's not "correct"
C++, and is likely to get you into trouble.

The actual requirement by pthread_create() is that the function you're
asking it to call in the context of a new thread must have C calling
conventions. While most C++ implementations do NOT use calling conventions
compatible with C for normal member functions, many DO for static member
functions. Some, on the other hand, don't, and there's no reason they
should. (That is, ANSI C++ says they needn't.)

The only proper way to do this is to specify, to pthread_create(), the name
of a function declared with the 'extern "C"' C++ qualification. This will
force the C++ compiler to generate a routine that expects to be called with
the platform's C calling conventions, and it'll always work with
pthread_create(). You can pass to that routine a "(void*)this", which your
'extern "C"' "*oline" can then convert back into a C++ object pointer
before passing it on to your member function.


| Compaq Computer Corporation              POSIX Thread Architect |
|     My book: http://www.veryComputer.com/;   |
\-----[ http://www.veryComputer.com/~anneart/family/dave.html ]-----/

 
 
 

1. Threading C++ Class Instances

Followups to apps.

Only the base class ever needs to refer to the function.

C++ might pass 'this' in a register while C could pass 'This' on the
stack.  With GCC, there's no difference, though.

I believe casts from member function pointers to static function
pointers are undefined.

--

666 MHz getting closer

2. Belkin OmniView 2-Port Kills Mouse in Linix

3. threading a class member function

4. Sun Solaris Packaging

5. Threading C++ Class Instances

6. Linux & DNS

7. Thread Pool Class

8. Promise ata100 controller on x86

9. problem running Java Applets on linux - threads: cannot find class java/lang/Thread ????

10. Fax modems -- class 1, class 2, class 2.0 -- whats going on?

11. thread class library

12. C++ class member function as a thread function.

13. C++ class wrappers for Posix threads