Windows Thread Basics

Source: Internet
Author: User

A thread consists of a thread kernel object and a thread stack. For more information about kernel objects, seeIntroduction to Windows kernel objects.

The thread kernel object. The operating system uses the thread kernel object to manage the thread. The operating system also uses it to store statistics.

The thread stack is used to maintain all function parameters and local variables required for thread execution, that is, C #ProgramIt is often said that the thread stack and the thread stack in the managed heap.

We know that a process is inert, and all its work is done by a thread. The process only provides a site for the thread. The thread function executes the task that we want it to execute, the final thread function will terminate and return, the thread will terminate, the thread memory will be released, and the usage count of the thread kernel object will be reduced by one, if the usage count of the thread kernel object is reduced to 0, the thread kernel object will be destroyed.

After talking about this, it feels a bit Virtual. Let's take a look at the functions used to create threads.

Createthread (

Lpsecurity_attributes lpthreadattributes,

Size_t dwstacksize,

Lpthread_start_routine lpstartaddress,

Lpvoid lpparameter,

DWORD dwcreationflags,

Pdword lpthreadid

);

Lpthreadattributes: used to set the thread security, whether the thread kernel object can be inherited by the process's sub-process, learn more about my articleArticleIntroduction to Windows kernel objects.

Dwstacksize: used to set the thread stack size. The default value is 1 MB.

Lpstartaddress: Address of the function executed by the thread

Lpparameter: parameter of the thread function

Dwcreationflags: used to identify whether the thread is executed immediately

Lpthreadid: thread ID

Create thread. When createthread is called, The system creates a thread kernel object, which is managed by the operating system. When its usage count is 0, it is automatically destroyed. The handle table of the current process will also contain a reference to the thread kernel object, and the system will allocate a common thread stack in the address space of the process. The new thread can access all kernel objects in the Process Handle table. The kernel objects are managed by the system kernel, and the process handle table maintains the reference of the kernel objects used by the process, the thread references the kernel object through the address of the kernel object in the handle table. The new thread can also access all the memory of the process and the stacks of all threads in the process. For example, the sub-thread can easily access the stack of the main thread. Another thing to note is that after the thread kernel object is created, the thread will not be executed immediately because the thread stack memory allocation and initialization take time, the process starts to run only after everything is ready.

Terminate thread. It is best to make the thread function automatically return, rather than force terminate the thread function, because the thread function also needs to do some cleanup before returning, such as destructor object and memory reclaim, let the operating system correctly release the memory used by the thread stack. If the thread is forcibly terminated, these operations may fail to be correctly executed, and memory leakage may occur.

Thread insider. The createthread call generates a thread kernel object. The Kernel Object count is 2. I personally think that the reason for the count is 2 is that the current process references it, in addition, ensure that the kernel objects returned by the thread function do not need to be destroyed automatically immediately, because the use count of the kernel objects must be reduced by one when the current function returns, when the value is reduced to 0, the kernel object is automatically destroyed. You also need to initialize other attributes of the kernel object and set the kernel object to the untriggered State so that it can be executed at any time. When the kernel object and thread stack memory are all ready, the system will write the two values to the top of the new thread stack. One is the thread function parameter and the other is the thread function address.

Each thread has its own set of CPU registers, that is, the context of the thread. Context reflects the status of the CPU register when the current thread executes once. This is because a thread can only be executed after multiple CPU polling. The status of the CPU register is saved before each CPU time is used up, so that the execution can continue when the next Round Robin arrives. The thread CPU registers are all stored in a context structure, and the context itself is stored in the thread kernel object. The instruction pointer register and the stack pointer register are two important registers in the thread context. The thread is always running in the thread context. When the thread's kernel object is initialized, the stack pointer register of the context structure is set as the address of the function in the thread stack, and the Instruction Pointer register is set as the address of the rtluserthreadstart function, while the rtluserthreadstart function is the place where the thread starts.

We recommend that you use _ beginthreadex instead of createthread to create a function.

Author:Chen taihan

Blog:Http://www.cnblogs.com/hlxs/

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.