Why a thread procedure should be a static or member function

why should the flow procedure be static or member? any valid reason?

+2
source share
3 answers

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 ); 

Therefore, if the thread procedure does not accept the pointer t, a function that has a first parameter of type ClassInQuestion* will not match the expected signature of the function.

+5
source

Mostly because non-stationary member functions have an implicit parameter, which makes it difficult to fill in a function pointer. I assume that when specifying a non-stationary member function, you also expect the object to be known, which is different from how other functions work.

+2
source

Typically, thread procedures should be called with predefined functions in thread libraries with a callback mechanism. To be able to call a member function (not static), you need a class object that will call the function. However, none of the available thread libraries supports this, that is, they do not accept the object that will be used to call the registered function. Thus, all such functions must be made static and cast accordingly.

+1
source

All Articles