1. How to define thread functions in C + +
There are two ways: a. Defines a thread function as a global function B. Static member functions that define a thread function as a class
2. How a thread function in C + + accesses a member variable in a class
Describes the static members of a class for thread functions. If you are a static data member, you can certainly access it directly, but if you want to access non-static data members, direct access is not possible. If you want to access and manipulate a non-static member variable for a class in a thread function, you can use the thread function as an adapter to invoke a Non-static member function of the class in the adapter. For example:
Class CMyClass
{
Public
void TestFunc ();
static int ThreadFunc (LPVOID pparam);//Adapter
Protected
int threadfunckernal ();//Kernal
}
void Cmyclass::testfunc ()
{
AfxBeginThread (Treadfunc, this);
}
Thread function Adapter
int Cmyclass::threadfunc (LPVOID pparam)
{
CMyClass *pobj = (CMyClass *) Pparam;
return pobj->threadfunckernal ();
}
Thread function Kernal
int cmyclass::threadfunckernal ()
{
while (1)
{
My thread
To control class Non-static member
}
return 0;
}