Thread is also crazy ------ thread basics, ------ thread Basics

Source: Internet
Author: User
Tags windows support

Thread is also crazy ------ thread basics, ------ thread Basics
Preface

Recently, I am working on a company's internal sharing, mainly about thread processing. The course aims to train developers who lack understanding and use of threads, this article mainly introduces why thread concepts, CPU development trends, thread overhead, and thread scheduling are introduced.

 

Why does Windows support threads?

In the early days, there was no thread concept in the operating system. The whole system only ran one execution thread, including system code and program code. The problem with using only one execution thread is that long running tasks will prevent other tasks from being executed, and some application bugs will cause an endless loop, which will also cause the entire machine to stop working, to solve this problem, the user had to restart the computer. With this urgent demand, Microsoft plans to build a new OS to meet the needs of enterprises and individuals. The new OS must be robust, reliable, scalable, and secure.

 

Concepts of processes and threads

A process is actually a collection of resources to be used by an application instance. Each process is assigned a virtual address space, make sure that the code and data used in one process cannot be accessed by another process. This ensures the robustness of the program, and the process cannot access the OS system code, making the operating system more stable and secure.

But what about CPU? Will the application be executed in an endless loop? If the machine has only one cpu, it will execute an endless loop and cannot execute anything else. Although the data cannot be destroyed, and it is more secure, the system may still stop responding, microsoft solves this problem by using threads.

Thread responsibility: virtualize the CPU. Windows provides dedicated threads for each process. If the application code enters an endless loop, the processes associated with the code will be frozen, however, other processes will continue to run.

 

Thread overhead

Like all virtualization mechanisms, threads have overhead in space (memory) and time (performance:

1. Thread Kernel Object

Generally speaking, it is the data structure of the thread, mainly including: thread description, thread context (memory block of the CPU register set), and the bytes used by the thread context corresponding to different architectures are also different.

 

2. Thread environment block (TEB)

TEB is the memory block allocated and initialized in user mode. It consists of about 1 Memory Page and 4 kb, mainly including: thread exception handling chain head, local data storage by thread, and some data structures used by GDI and OpenGL graphics

 

3. User Mode Stack

Stores local variables and real parameters passed to the method.

 

4. Kernel Mode Stack

Verify the parameter values in the user mode stack.

 

5. Notification of DLL thread connection and thread Separation

Call the DLLMain method of all non-hosted DLL related operations (such as creation and termination), and pass the ATTACH flag to the method.

 

Thread Context

In Windows, only one thread is allocated to the CPU at any time. Each thread can only run the length of one time slice. When the time slice expires, Windows switches the context to another thread and switches the thread each time:

1. Save the CPU register value to a context structure inside the kernel object of the currently running thread

2. Select a thread from the existing thread set to provide Scheduling

3. Load the value in the selected context structure to the CPU register.

After the context switch is complete, the CPU executes the selected thread until its time slice expires. Context switching has a significant impact on performance, and the time required for switching depends on the CPU architecture and speed, the time required to fill the CPU cache depends on the program running in the operating system, the size of the CPU cache, and other factors. Therefore, it is impossible to give a definite value for the overhead of each context, therefore, we need to build performance-oriented applications and components to avoid context switching.

 

Thread Scheduling and priority

Windows is a preemptible multi-threaded operating system because threads can be stopped and scheduled at any time. Each thread is assigned a priority from 0 to 31, when the system determines which thread is allocated to the CPU, it first checks the threads with a priority of 31 and schedules them in turn. If a thread with a priority of 31 can be scheduled, it is allocated to the CPU. When the time slice of this thread ends, the system checks whether another thread with a priority of 31 can run.

As long as there is a thread with a scheduling priority of 31, the system will never allocate any thread of <31 to the CPU. This situation is called hunger.

Process Priority: Idle, Below Normal, Normal, abve Normal, High, Realtime. The default value is Normal.

 

Foreground thread and background thread

When all foreground threads of a process stop running, CLR forces to terminate any background threads that are still running.

 

This article mainly explains the thread's basic knowledge theory too much may be boring. In general, the thread is a very valuable resource and must be saved for use. In order to do this, the best way is to use the CLR thread pool, which automatically manages the creation and destruction of threads. Next, I will continue to explain how to calculate the limited asynchronous operations and learn about the advantages of asynchronous operations based on actual cases.

 

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.