Threads are also crazy------threading Basics

Source: Internet
Author: User
Tags switches

Reference page:

Http://www.yuanjiaocheng.net/webapi/web-api-gaisu.html

Http://www.yuanjiaocheng.net/webapi/create-web-api-proj.html

Http://www.yuanjiaocheng.net/webapi/test-webapi.html

Http://www.yuanjiaocheng.net/webapi/web-api-controller.html

Http://www.yuanjiaocheng.net/webapi/config-webapi.html

Objective

Recently in a company to share, mainly about threading, the goal of the course is to train those who have a lack of thread understanding and use of developers, this article mainly introduces the concept of the introduction of threading, CPU trends, thread overhead, thread scheduling.

Why Windows wants to support threads

Early operating systems were not thread-concept, and the entire system ran only one thread of execution, which contained system code and program code. The problem with only one thread of execution is that long running tasks prevent other tasks from being executed, and some application bugs can cause a dead loop, which can also cause the entire machine to stop working, and the user has to restart the computer for this issue. With this urgent need, Microsoft plans to build a new OS to meet the needs of businesses and individuals, and the new OS must be robust, reliable, easy to scale and secure.

Concepts of processes and threads

A process is actually a collection of resources to be used by an instance of an application, each of which is given a virtual address space to ensure 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 is not able to access the OS's system code, making the operating system more stable and secure.

But what about CPUs? Will the application Die loop execution? If the installation above said that the machine has only one CPU, it will do a dead loop, can not do anything else, although the data can not be destroyed, and more secure but the system may still stop responding, Microsoft for this problem, the solution is the thread.

Thread Responsibility: Virtualization of the CPU, Windows provides the process-specific thread for each process, and if the application's code goes into a dead loop, the process associated with that code freezes, but the other process continues to execute.

Thread Overhead

As with all virtualization mechanisms, threads have the overhead of space (memory) and Time (performance):

1. Thread Kernel objects

Popular speaking is the data structure of the thread, mainly including: Thread description, Thread context (memory block of CPU register collection), different schemas corresponding thread contexts use different bytes

2. Thread environment block (TEB)

TEB is a block of memory allocated and initialized in user mode, approximately 1 memory pages 4kb, mainly including: Thread exception handling chain head, thread-local storage data, and some data structures used by GDI and OpenGL graphics

3. User mode stack

Stores the local variables and arguments passed to the method.

4. Kernel mode stack

Validates the value of a parameter in a user-mode stack.

5. dll thread connection and thread detach notification

Call all the DllMain methods of the unmanaged DLLs for the thread-related operations (create, terminate, and so on), and pass the attach flag to the method.

Thread context

Windows assigns only one thread to the CPU at any moment, and each thread can only run a single time slice, the time slice expires, and Windows switches the context to a different thread each time it switches the operation of the thread:

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

2. Select a thread from the existing thread collection to provide scheduling

3. Load the values in the selected context structure into the registers of the CPU

After the context switch is complete, the CPU executes the selected thread until its time slice expires. Context switching has a very large impact on performance, and the time it takes to switch depends on the CPU architecture and speed, the time it takes to populate the CPU cache depends on the programs running in the operating system, the size of the CPU cache, and various other factors, so you cannot give a definite value for the cost of each context. So to build the performance of the application and components, as far as possible to avoid context switching.

Thread Scheduling and prioritization

Windows is a preemptive multithreaded operating system because threads can stop and dispatch another thread at any time, each of which is assigned a priority of 0 to 31, and the system decides which thread to assign to the CPU, first to check the priority 31 threads, and to schedule them in a rotational manner. If a thread of priority 31 can be dispatched, it is assigned to the CPU. At the end of the time slice of this thread, the system checks if there is another priority 31 thread that can run.

As long as there is a scheduled priority 31 thread, the system will never allocate any thread of 31 to the CPU, which is called starvation.

Process Priority class: Idle,below Normal,normal,above normal,high,realtime. Default to Normal

foreground thread and background thread

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

This article is mainly to explain the basic knowledge of threading theory too much may be boring, in general, threads are very valuable resources, must be used, in order to do this, the best way is to use the CLR thread pool, the thread pool for you to automatically manage the creation and destruction of threads. The next article goes on to explain the limitations of asynchronous operations, and to understand the advantages of asynchronous operations in the actual case.

Threads are also crazy------threading Basics

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.