The use of the Windows API Thread function createthread is described as follows:
TheCreatethreadFunction creates a thread to execute within the address space of the calling process.
HANDLE CreateThread( LPSECURITY_ATTRIBUTES lpThreadAttributes, // pointer to security attributes DWORD dwStackSize, // initial thread stack size LPTHREAD_START_ROUTINE lpStartAddress, // pointer to thread function LPVOID lpParameter, // argument for new thread DWORD dwCreationFlags, // creation flags LPDWORD lpThreadId // pointer to receive thread ID);
The prototype of the lpstartaddress function, msdn, has the following statement:
TheThreadprocFunction is an application-defined function that serves as the starting address for a thread. specify this address when callingCreatethread
OrCreateremotethreadFunction.Lpthread_start_routineType defines a pointer to this
Callback function.ThreadprocIs a placeholder for the application-defined function name.
DWORD WINAPI ThreadProc( LPVOID lpParameter // thread data);
The most important core meaning is that createthread is the way Windows creates threads, in addition, a global or static function is required as the thread execution function (not a non-static member function of the class). However, let's recall the use of the thread class in Java, you will find that, in Java, you only need to implement the runable interface, you can create a thread, naturally there are polymorphism, if you use a global function or static function, naturally lose the object-oriented polymorphism.
I think you will surely ask why the class member function cannot be used as a thread function in createthread? Students who have studied C ++ know that C ++'s member functions carry at least one this pointer, that is, the following code:
classCBase{public:voidHello();};void CBase::Hello(){cout<<"Hello,World!"<<endl;}int main(){CBasebase;base.Hello();}
This cbase: Hello () implementation will be compiled to compile the equivalent cbase: Hello (cbase * This). Of course, different compilers will have different processing methods, for example, GCC and vs have different processing methods for this Hello Message (however, It is equivalent in semantics ).
However, the principle is explained here. The principle is to facilitate understanding, but it is difficult to convert the code into a real code. The real code is also related to more details, so that you can understand it easily, in the next section, I will explain how the vsplatform handles this pointer.