[409] in Pthreads mailing list archive

home help back first fref pref prev next nref lref last post

Re: proven's pthread with C++?

daemon@ATHENA.MIT.EDU (Mason J. Katz)
Wed Sep 11 13:16:09 1996

Date: Wed, 11 Sep 1996 09:44:26 -0700
From: "Mason J. Katz" <mjk@cs.arizona.edu>
To: wong@CS.UCLA.EDU
Cc: pthreads@MIT.EDU
In-Reply-To: <Pine.SOL.3.91.960910210005.375A-100000@flamingo.cs.ucla.edu>
	(wong@CS.UCLA.EDU)


If you want to use a class method as the entry point into the thread
make it a static method.  Manually passing "this" and casting the
method to a function is extremely non-portable and very hard to
convince the compiler to let you do it.

Do something like this instead.

class Foo {
public:
	bar();
private:
	static void run(void *arg); /* thread start function */
	^^^^^^
}


void Foo::run(void *arg) {
	Foo	*o = (Foo *)arg; /* get the object */

	o->bar();
}


The problems of passing class methods as functions occurs far too
often.  Remember this next time you deal with signals or callbacks
(even qsort) in C++.

-- 
    /    /       /  /    /  | Mason J. Katz <mjk@cs.arizona.edu>
   //  //       /  /   /    | <http://www.primenet.com/~mjk>
  / // /       /  ////      |
 /    /  /    /  /   /      |
/    /   /////  /    /      | (PGP key on webpage and keyservers)

home help back first fref pref prev next nref lref last post