Creatthread in Windows

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.