There are three methods:
1.
In this case, the thread function is declared as static, for example:
Class crealtimetask
{
Public:
Static uint taskmain (lpvoid PARAM );
Bool starttask ();
};
For the member functions defined in the class, VC will impose a this pointer during compilation, so the above situation will occur. Declare the member function as static. You can remove this pointer, but the static member function can only access static members.
2.
To declare the thread function as a member function, so that the pointer of this class can be passed into the member of the member class;
ClassCrealtimetask
{
Public:
FriendUint taskmain (lpvoid PARAM );
Bool starttask ();
};
Uint taskmain (lpvoid PARAM)
{
Crealtimetask * ptaskmain = (crealtimetask *) Param;
// Reference using the ptaskmain pointer.
ReturnTrue;
}
Bool crealtimetask: starttask ()
{
Afxbeginthread (taskmain,This);
}
3.
Callback can be implemented for member functions and non-static members can be accessed, as shown in the following figure. This is a class implemented to implement the callback Class Members of the thread function. It seems better than the implementation method of MFC.
ClassBase;
Typedef int(Base: * fncallback )(Void* P );
StructCallback (Void* Param; fncallback * pfuc; base * pthis ;};
ClassBase {
Static intMythreadfuc (Void* P ){
StructCallback * P1 = (StructCallback *) P;
Base * pthis = p1-> base; fncallback * pfuc = p1-> pfuc;Void* Param = p1-> param;
IntI = (pthis-> * pfuc) (PARAM );DeleteP;
ReturnI;
}
Public:
VoidMycreatethread (fncallback pfuc,Void* PARAM ){
StructCallback * P =New structCallback;
P. Param = Param; p. pthis =This; P. pfuc = pfuc;
: Createthread (mythreadfuc, P );
}
Virtual intMycallback (Void* P) {printf ("It's base class." N ");Return0 ;}
};
ClassDerived:PublicBase {
IntMycallback (Void* P) {printf ("It's derived class" N ");}
};
VoidMycreatethreadimitate (fncallback FUC,Void* P)
{
(* FUC) (p );
}
VoidMain ()
{
Base P;Char* Param;
P. mycreatethread (& (base: mycallback), Param );
Derived P2; p2.mycreatethread (& (base: mycallback), Param );
}