Naruto
Naruto

Reputation: 9634

why does thread procedure should be static or member function

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

Upvotes: 3

Views: 1652

Answers (3)

mukeshkumar
mukeshkumar

Reputation: 2778

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.

Upvotes: 2

stefaanv
stefaanv

Reputation: 14392

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.

Upvotes: 3

sharptooth
sharptooth

Reputation: 170529

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.

Upvotes: 6

Related Questions