Twenty-six chapters thread basis

table of Contents:

26.1 Windows Why support thread

26.2 thread overhead

26.3 madness stop

26.4 trends CPU

26.5 CLR thread and Windows threads

26.6 calculation using a dedicated asynchronous thread execution limiting operation

26.7 reasons to use threads

26.8 thread scheduling and priority

26.9 foreground and background thread thread

26.10 to continue learning

Why Windows support thread

Windows as a concept, is the responsibility of threads on the CPU virtualization. Windows for each process provides a reform process dedicated thread (functions as a CPU). The application code into an infinite loop, the code associated with that process back to "freeze", but other processes will not freeze, will continue.

Thread overhead

Threads allow users forced to terminate the application seems to have been frozen. And all the virtualization mechanism, like a thread space (memory consumption) and time (runtime execution performance) overhead on.

Thread kernel objects: OS each thread created in the system are allocated and initialized one such data structure. The data structure comprises a set of threads is described by the attribute. Further comprising a data structure called a thread context. Context is a block of memory containing a set of CPU registers.

Thread environment block (the TEB): the TEB is allocated and initialized memory block in user mode (fast access to application code in the address space). TEB consumption of a memory page. TEB include exception handling thread chain first. Each thread into the try block in the chain inserting a first node; delete node change from the chain when the thread exits the try block. "Thread local storage" TEB data further comprises threads, and some data structures used by the GDI and OpenGL graphics.

User mode stack: the user mode stack of local variables and arguments passed to the method of storage. It contains an address; it pointed out that the current method returns, the thread should then be executed from somewhere.

Kernel mode stack : the application code is passed to the argument in the operating system kernel-mode function, the kernel-mode stack is also used.

DLL thread connection (attach) and thread separation (detach) notice: a strategy Windwos that any time a thread is created in the process, DllMain method of all unmanaged DLL will use the first oh ah loaded in the process, and the transfer method DLL_THREAD_ATTACH flag. Any thread terminates, DllMain square method for all unmanaged DLL that calls the process, and method for delivering DLL_THREAD_DETACH flag.

Windows any time only one thread is assigned to a CPU. The thread can run a "time slice (also referred to as" amount "or" span ")" in length. Time slice expires, Windows will context switch to another thread:

1. Save CPU registers to a structure within the context of the kernel objects currently running thread.

2. elect a thread for thread scheduling from the existing collection. If the thread is owned by another process, Windows Before you begin any contact with any code or data, also must be switched to "see" the virtual address space of CPU.

3. The structure selected context value loaded into the CPU registers.

The net overhead context switch; overhead produced does not return any memory or return on performance. So in exchange for a better user experience by sacrificing the performance of context switching.

Time required to perform a context switch and the speed depends on the CPU architecture. The CPU cache fill time required depends on the application running on the system, CPU cache size and various other factors.

The implementation of garbage collection, CLR must suspend (pause) all the threads, they traverse the stack to find the root in order to label objects in the heap, they traverse the stack again, to restore all threads.

Stop the madness

Trends CPU

 Multiple CPU

Hyper-Threading chip

Multi-core chips

CLR thread and Windows threads

 The CLR uses Windows thread processing functions, CLR thread is fully equivalent to Windows threads.

Calculation using a dedicated asynchronous thread execution limiting operation

The following conditions can create your own threads:

Priority thread needs to run in non-ordinary thread.

We need to give the foreground thread to thread performance, prevent the application to terminate before the end of the thread task.

Computing tasks need to limit the long run.

To start a thread and may call Thread Abort method to early termination of it.

Reasons to use threads

 Response (typically measured for a client GUI application)

Windows provides its own thread for each process to ensure that the occurrence of an infinite loop applications do not interfere with other applications.

Performance (for client and server applications)

Because Windows scheduling a thread per CPU, multiple CPU can execute concurrently and these threads, it can perform multiple operations simultaneously improve performance.

Thread scheduling and priority

Preemptive operating system must use an algorithm to determine which thread at what time schedule for how long - the thread may stop (preempted) and schedule another thread at any time. (Each object contains a kernel thread context structure, a context structure reflects the state of the CPU registers After completion of execution of the thread. After a time slice, Windows kernel threads to check all existing objects. In these subjects, who had no What thread is waiting just for scheduling .Windows select a thread kernel object can be scheduled, and context switching to it .Windows actually recorded the number of times each thread context switching to. then, the thread starts executing code, and its process after processing data address space. then, after over a time slice, Windows performs a context switch next .Windows will have to perform a context switch from the start of the system starts until the system is turned off.)

Each thread is assigned a priority from 0 to 31. Hunger: a higher priority thread taking up too much CPU time, resulting in a lower priority thread can not run.

Is created when the system starts a special zero-page thread priority of the threads is 0, and the whole system is the only priority thread 0. In the absence of other threads need "working children", the zero page thread system RAM is cleared of all free pages.

Process priority classes: Idle, Below Normal, Above Normal, High, Realtime.

The relative thread priority: Idle, Lowest, Below Normal, Normal, Above Normal, Highest, Time-Critical.

The relative thread priority Process priority class
Idle Below Normal Normal Above Nomal High Realtime
Time-Critical 15 15 15 15 15 31
Highest 6 8 10 12 15 26
Above Normal 5 7 9 11 14 25
Normal 4 6 8 10 13 24
Below Normal 3 5 7 9 12 23
Lowest 2 4 3 8 11 22
Idle 1 1 1 1 1 16

Foreground and background thread thread

CLR each thread is either treated as foreground thread, or regarded as a background thread. When all foreground threads of a process stops running, CLR forcibly terminate any background thread is still running. The background thread is terminated directly, without throwing an exception.

The main thread of the application and any thread by constructing a Thread object to explicitly create default to the foreground thread, the thread pool thread a background thread by default. Any thread created by the managed execution environment into the native code is marked as a background thread

Guess you like

Origin www.cnblogs.com/terry-1/p/10683083.html