Basics: One: Why does Windows support threads?
As a Windows concept, the thread's responsibility is to virtualize the CPU. Windows provides the process-specific thread for each process (functionally equivalent to a CPU, which can be interpreted as a logical CPU). If the application's code goes into an infinite loop, the processes associated with that code "freeze", but the other processes do not freeze and they continue to execute.
Two: Thread overhead
- The thread kernel object (thread kernel object) OS allocates and initializes one of these data structures for each thread created in the system. In this data structure, there is a set of properties that describe the thread, and also contains the so-called data context (thread context). The context is a block of memory (containing a collection of CPU registers), the X86CPU context size is approximately 700 bytes, and for X64, the context is approximately 1240 bytes.
- The thread environment block (thread environment block, TEB) TEB is a block of memory that is allocated and initialized in user mode (the address space that the application code can access quickly). TEB consumes a memory page (4KB), TEB contains the thread's exception handling chain head. Each try block entered by the process inserts a node at the top of the chain. When the thread exits the try, the node is removed from the chain. In addition to this, TEB also contains thread-local storage data for threads, as well as some data structures used by GDI (graphics device Interface, graphic devices Interface), and OpenGL graphics.
- The user mode stack (user-mode stack) user mode is used to store local variables and arguments that are passed to the method. It also contains an address that indicates where the thread should start executing when the current method returns. By default, Windows allocates 1MB of memory for each thread's user-mode stack.
- DLL thread connection (attach) and thread detach (detach) notifications, one strategy for Windows is that whenever a thread is created in the process, the DllMain method of all DLLs loaded in that thread is called, and a dll_thread_ is passed to the method Attach logo. Similarly, any time a thread terminates, all Dll_thread_detach flags in the thread are called. Some DLLs need to take advantage of these notifications to perform special initialization or (resource) cleanup operations for each thread created/destroyed in the process. For example, the C-runtime library DLL allocates some thread-local storage state. These states are needed when a thread seems to use the functions contained in the C-runtime library.
At any given moment, Windows simply assigns a county to a CPU. That thread allows a "time slice" to run. Once the time slice expires, Windows switches the context to another thread. Each context switch requires WINDWOS to perform the following actions.
- Saves the value in the CPU register in a context structure inside the kernel object of the currently running thread.
- Selects a thread from the existing collection of threads for dispatch. If the thread is owned by another process, Windows must also switch the virtual address space of the CPU "see" before it starts executing any code or touches any data.
- Loads the value in the selected context structure into the CPU register.
Three: CLR threads and Windows threads
Threads in the CLR today correspond directly to Windows threads. The Microsoft CLR team retains the right to remove him from the Windows county in the future.
Four: Thread scheduling priority
The preemptive operating system must use an algorithm to determine when to dispatch those threads for how long. The context structure reflects the state of the thread's CPU registers when the thread was last executed. After a time slice (Time-slice), Windows examines all existing thread kernel objects. Of these objects, only those threads that are not waiting for anything are suitable for scheduling. Windows chooses a thread kernel object that can be dispatched, and the pawn context switches it. Windows actually records the number of times each thread has been toggled by context and can be viewed using tools from Microsoft Spy + +.
When designing an application, you should decide whether the application needs to be larger or less influential than other applications running concurrently on the machine. Then, select a process priority class to reflect your decision. Windows currently supports the 6-brother Process Priority class: Idle, Below Normal, Normal, Above Normal, hight, and realtime.
V: foreground thread and background thread
The CLR considers each thread either as a foreground thread or as a background thread. When all foreground threads in a process stop running, the CLR forces the finalization of any background threads that are still running. These back seats are directly terminated; no exception is thrown.
Therefore, foreground threads should be used to perform tasks that you do want to accomplish, such as flush data from a memory buffer to disk. In addition, a background thread should be used for tasks that are not critical code.
C # Thread Learning notes (1)