[ale] OT: dummy C++/threads question

Alexander Barton abarton at mindspring.com
Wed Jul 23 23:18:34 EDT 2003


First, make sure you're compiling with full warnings turned on.  Like 
"-Wall", but there are also some other useful warnings that aren't 
included in the "all" part.  Read over the g++ man page.  Compiler 
warnings are your friends.  :-)

Second, the pthread_create() function wants a pointer to a _function_ 
with _C_linkage_.  Not a method or a class or a function with C++ 
linkage.  The distinction between C and C++ linkage is that the C++ 
stack frame may be different from a C stack frame.  For example, it may 
have extra stuff in there to handle exceptions that a C stack frame 
doesn't.  For perfect portability you can't assume that the two are 
compatible.  (Now, having said that, it might not matter under g++.)

o Functions are _not_ member functions.  From your code snippet it looks 
like you don't get the distinction.
o For full portability, C++ functions are not the same as C functions.
o Don't let C++ exceptions unwind through C stack frame.

Since pthread's C function pointer can't be a C++ function or method, 
you need the help of an intermediate launcher function.

extern "C" {
//  This is a first-stage function with C linkage that calls
//  an object's member function.
//  ptr is a pointer to an object of class CMyClass that we're
//  gonna invoke MyFunction() on.
//
static void launcher(void *ptr)
{
     try {
         CMyClass *objectptr = dynamic_cast<CMyClass *>(ptr);
         if (NULL == objectptr)
             /* ...handle error.... */ ;
         objectpre->MyFunction();
     }
     catch (...)
     {
         /* ...handle exception.... */ ;
         //  Don't let exceptions unwind through launcher()
     }
}
}
 
 

//  Invoke with:
CMyClass foo;
foo.setArgs(....);
int result = pthread_create(&thread, &attrs, launcher, &foo);


Isn't C fun?  Makes me wanna go learn Java.  C is hard, but simple.  C++ 
is hard and much more complex.  Threads in C++ can be extremely 
difficult to get perfectly right.  I can show you scars....

Good luck.

-Alexander

PS: Don't futz around with pthread_mutex.  Go get BOOST's object 
oriented wrappers for the pthreads library and save yourself much much 
grief.



Christopher Fowler wrote:
> Remove the '&' and that should pass the function through.  What
> are the errors you are getting?
> 
> 
> On Wed, Jul 23, 2003 at 12:21:02PM -0400, John Mills wrote:
> 
>>ALErs (C++ gurus??) -
>>
>>I have a C++ class with a member function, say:
>>
>>void * CMyClass::MyFunction(void * myArgs) {blah}
>>
>>I want to this function to be executed in a thread started by another 
>>member of the same class:
>>
>>fnType CMyClass::MyManager()
>>{
>>  ...
>>  pthread_create(&someInt, NULL, &CMyclass::MyFunction, &someStruct);
>>  // can't get this right-->>    ^^^^^^^^^^^^^^^^^^^^^
>>}
>>
>>How can I pass the pointer to my member function?
>>
>>TIA.
>>
>> John Mills
>> john.m.mills at alum.mit.edu
>>
>>_______________________________________________
>>Ale mailing list
>>Ale at ale.org
>>http://www.ale.org/mailman/listinfo/ale
> 
> _______________________________________________
> Ale mailing list
> Ale at ale.org
> http://www.ale.org/mailman/listinfo/ale
> 

_______________________________________________
Ale mailing list
Ale at ale.org
http://www.ale.org/mailman/listinfo/ale





More information about the Ale mailing list