Createthread
Microsoft provides the createthread function for creating new threads in Windows APIs,
Overview:
When CreateProcess is called, The system creates a process and a main thread. Createthread creates a new thread on the basis of the main thread. The steps are as follows:
1. Allocate a thread ID/handle to the kernel object for management. It is returned by createthread.
2. Set the thread exit code to still_active, and set the thread suspension count to 1.
3. Allocate the Context Structure
4. allocate two pages of physical storage to prepare the stack. Set the protection page to page_readwrite and page 2nd to page_guard.
5lpstartaddr and lpvthread values are placed on the top of the stack to make them the parameters passed to startofthread.
6. Point the stack pointer of the context structure to the top of the stack (step 1) and the command pointer to the startofthread function.
The createthread prototype in msdn:
Handle createthread (
Lpsecurity_attributes lpthreadattributes,
DWORD dwstacksize,
Lpthread_start_routine lpstartaddress,
Lpvoid lpparameter,
DWORD dwcreationflags,
Lpdword lpthreadid );
Parameter description:
Lpthreadattributes: pointer to the structure of the security_attributes type. Ignore this parameter in Windows 98. In Windows NT, it is set to null, indicating that the default value is used.
Dwstacksize, thread stack size, generally = 0. In any case, Windows dynamically prolongs the stack size as needed.
Lpstartaddress, a pointer to a thread function, in the form of @ function name, function name is not limited, but must be declared in the following column form:
DWORD winapi threadproc (pvoid pparam). If the format is incorrect, the call will fail.
Lpparameter: The parameter passed to the thread function. It is a pointer to the structure and nil is used when no parameter is required.
Dwcreationflags: Specifies the thread flag. Optional values:
Create_suincluded: Creates a suspended thread.
0: activated immediately after creation.
Lpthreadid: ID of the new thread to be saved.
Return Value:
If the function succeeds, the thread handle is returned. If the function fails, false is returned.
Function Description:
Create a thread.
Syntax:
Hthread = createthread (& security_attributes, dwstacksize, threadproc, pparam, dwflags, & idthread );
It is generally not recommended to use the createtheard function, but it is recommended to use the begintheard function defined in the system unit in the RTL library, because in addition to creating a thread and an entry function, several protection measures are added.
-- Refer to Baidu encyclopedia