views:

90

answers:

3

why does thread procedure should be static or member function? any valid reason?

+4  A: 

Non-static member variables have an implicit this parameter passed by the compiler internally.

You have

ClassInQuestion {
   void threadFunc( int );
}

and the compiler internally creates a function

void ClassInQuestion_threadFunc( ClassInQuestion* thisObject, int );

So unless the thread procedure accepts a pointer t a function that has a first parameter of type ClassInQuestion* it will not match the expected function signature.

sharptooth
The question reads "static *or* member function".
Georg Fritzsche
I have also misread the question, but for mfc, following shows that a free or static member function is needed: http://www.go4expert.com/forums/showthread.php?t=3197 and for pthreads (C-API), that seems obvious, so I guess the question is a bit sloppy.
stefaanv
+2  A: 

Mainly because non static member functions have an implicit parameter, making it hard to fill in in a function pointer. I guess that when specifying a non static member function, you would also expect the object to be known, which is different from how functions otherwise work.

stefaanv
+1  A: 

Typically, the thread procedures have to be called by the predefined functions in the thread libraries with callback mechanism. To be able to call a member function (not static), you need an object of the class which would invoke the function. However, none of the available thread libraries support this i.e. they do no accept the object which would be used to call the registered function. So all such functions should be made static and type casted appropriately.

hype