[ale] OT: dummy C++/threads question
John Mills
johnmills at speakeasy.net
Thu Jul 24 09:47:02 EDT 2003
Alexander -
Thanks for the note, clear explanations, and example.
On Wed, 23 Jul 2003, Alexander Barton wrote:
> o Functions are _not_ member functions. From your code snippet it looks
> like you don't get the distinction.
I certainly didn't see its importance here. I am patching an example from
MsVC++ and learning to use threads at the same time. Obviously VC++ is
much more tolerant of loose typing (and I don't mean keystrokes!) and
other glosses I find hazardous to robustness. I'ld rather know about the
distinction and handle it explicitly.
> Since pthread's C function pointer can't be a C++ function or method,
> you need the help of an intermediate launcher function.
I'll do it this way.
> 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);
> 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.
I'll have a look.
Best Regards, and thanks again for the tutorial comments.
John Mills
john.m.mills at alum.mit.edu
_______________________________________________
Ale mailing list
Ale at ale.org
http://www.ale.org/mailman/listinfo/ale
More information about the Ale
mailing list