Thread of the Windows kernel simple introduction

Source: Internet
Author: User
Tags terminates

1 Thread Definitions

<1> kernel objects, which the operating system uses to manage threads.

Kernel objects are also places that the system uses to hold thread statistics

<2> There is also a thread stack. It is used to maintain all the function parameters and local variables that the thread needs to run the code.

2 Thread and Process differences

The <1> process is inactive. The process never runs, no matter what, it is just a container for threads.

The <2> thread runs the code in its process address space. and manipulate the data in the address space of the process.

<3> address control for thread sharing processes

<4> threads have only one kernel object and one stack, leaving very few records. The overhead is much less than the process.

3 Thread functions

Threads are divided into the main thread as well as generic threads. The entry point function of the main thread must be main,wmain. Winmain,wwinmain, the general thread function can be arbitrarily named. And you must have a function return value.

4 Creating Threads

HANDLE CreateThread (   lpsecurity_attributes   lpthreadattributes,       //SD size_t         dwstacksize,                       // Initial stack size lpthread_start_routine      lpstartaddress,    //thread function LPVoid         lpparameter,            // Thread argument DWORD      dwcreationflags,         //creation option  Lpdword   lpthreadid              //thread identifier);

<1>lpthreadattributes

A pointer to a security_attributes that specifies a security attribute that, when NULL, takes advantage of the system's default security attributes

<2> dwstacksize

Sets the initial size of the line stacks, in bytes. The system rounds it to the near-term value of the page size and, when set to NULL, takes the system's default value.

<3> Lpparameter

A pointer to a function of the application-defined lpthread_start_routine type, which will have a new thread running, representing the new starting address of the thread. Specifies a function to be the entry address of a new thread, the name of the function is arbitrary, but the type must follow the following format:

Dwrod WINAPI ThreadProc (LPVOID lpparameter);

<4> Lpparameter

The number of parameters passed to the thread

<5> dwCreationFlags

Sets additional markup for thread creation. is one of the create_suspended or 2. Assume that create_suspended indicates that the thread is not running immediately, knowing that the ResumeThread function starts running.

<6> Lpthreadid

This is a return value. The thread ID used to represent the function.

The stack allocated by the system to threads is allocated from the address space of the process, and all threads share the process address space, and communication between them is very convenient.

Note: CreateThread is a Windows function, assuming you want to write multithreaded functions for C/s + +, then you need to use the _BEGINTREADX function

5 Termination of thread execution

<1> Return of thread functions

<2> revoke threads through the ExitThread function. Just better not.

<3> the same process or a thread in a process calls the TerminateThread function, you should avoid using the

<4> includes the terminating execution of the thread's process. Should avoid using

5.1 Things to do when a thread function returns

<1> all C + + objects created in the thread function are correctly undone by their undo function

<2> the operating system will correctly release the memory used by the thread stack

The <3> system sets the exit code of the thread (maintained in the kernel object of threads) to the return value of the thread function

<4> system will decrement the usage count of thread kernel objects

5.2 ExitThread function

It allows the operating system to purge all operating system resources, including the thread's stack, but C + + resources will not be revoked

5.3 TerminateThread function

This function can stop no matter what thread. However, it does not send any notification messages to the thread that will be revoked, and it is asynchronous. There is no guarantee that the function thread has been terminated when the function returns. Before the process terminates, the thread's stack is not cleared, which makes it easier for other threads to use.

5.4 Termination of the thread at the end of the process

The same problem is that the process is forced to terminate, which can cause

5.5 What happens when a thread terminates

<1> the exit code of the thread is transferred from S t i l _ A C t i V e to the code of E x i t t h R e a D or te R m i n a t e t h r e a D

<2> thread kernel object status changed to notified

<3> Assuming that the thread is the last active thread in the process, the system also treats the process as having terminated execution

<4> thread Kernel Object usage count decremented by 1

Note: By checking the rollout of the thread handle, the code can check whether the thread has terminated. The function of the response is:

BOOL GetExitCodeThread (HANDLE hthread,lpdword Lpdword)

Thread of the Windows kernel simple introduction

Related Article

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.