CreateThread function, createthread

Source: Internet
Author: User

CreateThread function, createthread

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.
Syntax:
HThread = CreateThread (& security_attributes, dwStackSize, ThreadProc, pParam, dwFlags, & idThread );

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 first parameter is a pointer to the structure of the SECURITY_ATTRIBUTES type. Ignore this parameter in Windows 98. In Windows NT, it is set to NULL.
The second parameter is the initial stack size for the new thread. The default value is 0. In any case, Windows dynamically prolongs the size of the stack as needed.
The third parameter is the indicator pointing to the thread function. Function names are not restricted, but must be declared in the following column:
Dword winapi ThreadProc (PVOID pParam );
The fourth parameter is the parameter passed to ThreadProc. In this way, the main thread and slave thread can share data.
The fifth parameter is usually 0, but the flag create_suincluded is used when the created thread is not executed immediately. The thread is paused until calling ResumeThread to resume thread execution.
The sixth parameter is an indicator that points to the variable that accepts the execution thread ID value.
 

Note: The critical section should be initialized before the thread is executed, because the thread starts to run once it is established (unless it is suspended manually), but problems may occur in the initialization critical section after the thread is established.
For example, the phenomenon in Sun Xin MFC

Int main ()
{
InitializeCriticalSection (& g_cs); // initialize the critical section first.

HANDLE handle1 = CreateThread (NULL, 0, ThreadProc1, NULL, 0, NULL );
HANDLE handle2 = CreateThread (NULL, 0, ThreadProc2, NULL, 0, NULL );
CloseHandle (handle1 );
CloseHandle (handle2 );

Sleep (4000 );
DeleteCriticalSection (& g_cs );
Return 0;
}


Every parameter of the CreateThread () function is required. Let me explain the thread problem.

HANDLE CreateThread (
LPSECURITY_ATTRIBUTES lpsa,
DWORD cbStack,
LPTHREAD_START_ROUTINE lpStartAddr,
LPVOID lpvThreadParam,
DWORD fdwCreate,
LPDWORD lpIDThread
);
Lpsa: Security Attribute of the thread handle. For example, if a sub-process can inherit the thread handle, it is generally set to NULL.
CbStack: Specifies the thread stack size. Generally, 0 indicates the default size.
LpStartAddr: the thread entry function typedef DWORD (_ stdcall * LPTHREAD_START_ROUTINE )(
Void * lpThreadParameter); the default function call convention in win32 programs is WINAPI ,__ stdcall = WINAPI. Therefore, you can declare that your entry function is:
Dword winapi ThreadProc (void * lpParamete) {// what you want to do in the thread}
LpvThreadParam: the parameter of the thread entry function, that is, the parameter of ThreadProc (void * lpParamete ).
FdwCreate: the flag for controlling thread creation is generally 0, indicating that the thread starts immediately. If you want to hook up the thread after creation, you can pass create_suincluded. You need to call ResumeThread to start the thread where appropriate.
LpIDThread: the return value of the thread ID, which is used to receive the ID returned by the thread.
I am afraid that you do not understand it after writing so much:
Let me give you an example. I don't guarantee the operation is successful, and I didn't compile it either.
Dword winapi ThreadProc (void * lpParameter)
{
Int * x = (int *) lpParameter; // obtain the parameter address.
MessageBox (NULL, TEXT ("adf"), NULL, MB_ OK );
}
DWORD dwThreadID;
Int x = 0;
HANDLE hThread = CreateThread (0, 0, ThreadProc, (void *) & x, 0, & dwThreadID );
CloseHandle (hThread );
If you want to learn windows programming well, MSDN is required. You don't know what it is. MSDN is the help document. You can search Baidu and download it, this is very helpful for your MFC, Windows API, C/C ++, etc.

(C ++ multithreading problem) CreateThread () function problem

(1) LPTHREAD_START_ROUTINE lpStartAddress specifies the starting address of the thread function. The new thread starts to execute this address until the return statement is returned, and the control is handed over to the operating system.
(2) LPVOID lpParameter is the parameter passed to the thread function, that is, the address passed to the thread function specified in (1.
The following is an example: After a thread is created, it is transferred to the thread function for execution. When a return statement is encountered, it is returned to the main function.

# Include <iostream>
# Include <windows. h>

Using namespace std;

/* ThreadProc is LPTHREAD_START_ROUTINE lpStartAddress
* The thread function specified by the parameter. lpParam indicates LPVOID lpParameter.
* Fixed parameters passed to the thread function */

Dword winapi ThreadProc (LPVOID lpParam)
{
For (int I = 0; I <20; ++ I)
{
Cout <"I am from a thread, count =" <I <endl;
}

Return 0;
}

Int main ()
{
HANDLE hThread;
DWORD dwThreadID;

HThread =: CreateThread (
NULL,
NULL,
ThreadProc, // thread entry address
NULL,
0,
& DwThreadID );

: WaitForSingleObject (hThread, INFINITE );
: CloseHandle (hThread );

Return 0;
}

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.