开发者

why does thread procedure should be static or member function

开发者 https://www.devze.com 2023-01-15 12:57 出处:网络
why does thread procedure should be static or member function? any val开发者_高级运维id reason?Non-static member variables have an implicit this parameter passed by the compiler internally.

why does thread procedure should be static or member function? any val开发者_高级运维id reason?


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消