threading mechanisms, CLR thread pools, and application domains

Source: Internet
Author: User
Tags switches terminates windows support

Recently in summarizing multithreading, the CLR thread pool, and the TPL programming practice, rereading the CLR via C # is a lot more rewarding than just working. Also need to read more books, reading a good book, at the same time to more summary, more practice, the technology research thoroughly, the use of good.

Don't talk more, just go to the blog. First of all, why does Windows support threading mechanisms?

1. Why Windows supports threading

In the early days of the computer, the operating system had no thread concept, and the entire system ran only one thread of execution, which contained operating system code and application code. The problem with only one thread of execution is that long-running tasks prevent the execution of other tasks. For example, in the age of 16-bit Windows, applications that print documents can easily "freeze" the entire machine.

When Microsoft designed this version of the OS kernel for Windows NT, it decided to run each instance of the application in one process. A process is actually a collection of resources to be used by an instance of an application. Each process 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 application instance. At the same time, the process cannot access the kernel code and data of the OS, so the application code cannot destroy the code and data of the operating system.

What happens if the application has a dead loop? If the machine has only one CPU, it executes a dead loop and cannot execute any other program. Microsoft's solution is threading. As a Windows concept, the thread's responsibility is to virtualize the CPU. Windows provides the process-specific thread (functionally equivalent to a CPU) for each process. The code of the application dies, and the processes associated with the code "freeze", but other processes (which have their own threads) do not freeze, and they continue to execute.
Threads are powerful because they allow Windows to respond at any time, even when performing long-running tasks.

However, as with all virtualization mechanisms, threads have the overhead of space (memory consumption) and time (execution performance at runtime).

2. What are the threading costs?

Each thread is composed of the following elements:

Thread kernel objects (Threads kernel object): The Description property and thread context of a thread, the context is a block of memory that contains the CPU register collection. For x86, x64, ARM CPU architectures, the thread context uses approximately 700,1240 and 350 bytes of memory, respectively.

thread environment block (thread environment BLOCK,TEB): A block of memory allocated and initialized in user mode (the address space that the application code can quickly access). TEB consumes one memory page (4KB in x86, x64, ARM CPU)

user mode stack (user-mode stack): The user-mode stack stores local variables and arguments passed to the method, and it also contains an address: indicates where the thread should proceed from when the current method returns. Windows defaults to allocating 1MB of memory for each thread's user-mode stack. Windows simply retains the 1MB address space and commits (transfers) physical memory when the thread actually needs it.

kernel-mode stack (Kernel-mode stack): When application code passes parameters to kernel mode in the operating system, the kernel-mode stack is also used, and for security reasons, Windowd copies these arguments from the thread's user-mode stack to the thread's kernel-mode stack. 32windows kernel-mode stack size 12kb,64 bit is 24KB

DLL Thread connection (Attach) and thread detach (Detach) notifications : One of the policies of Windows is that whenever a thread is created in a process, the DllMain method of all unmanaged DLLs loaded in the process is called and passed to the method Dll_ Thread_attach logo. Similarly, any time a thread terminates, the DllMain method of all unmanaged DLLs in the process is called, and the Dll_thread_detach flag is passed to the method.

Context Switches

A single CPU computer can only do one thing at a time. Therefore, Windows must share the physical CPU between all the threads (logical CPUs) in the operating system.

Windows assigns only one thread to a CPU at any time. That thread can run the length of a "time slice". When the time slice expires, Windows switches the context to another thread. Each context switch requires Windows to do the following:

    • Saves the value of the CPU register in a context structure inside the kernel of the currently running thread
    • Select a thread from the existing thread collection for dispatch
    • Loads the values in the selected context structure into the CPU register

When the Windows context switches to another thread, there is some performance penalty.

At the end of a time slice, if Windows decides to dispatch the same thread again (instead of switching to another thread), Windows does not perform a context switch.

3. Reasons to use Threads: what scenarios use threads

responsiveness (typically for client-side GUI applications): in a client GUI application, some work can be given to a thread, allowing the GUI thread to respond sensitively to the user's input. The number of threads created during this process may exceed the core number of CPUs, wasting system resources and reducing performance. But the user experience has been improved and enhanced.

performance (for clients and server-side applications): because Windows schedules one thread per CPU and multiple CPUs can execute those threads concurrently, performing multiple operations concurrently can improve performance.

4. CLR thread pool mechanism

Creating and destroying threads is an expensive operation that takes a lot of time, and too many threads waste memory resources. Because the operating system must dispatch a running thread and perform context switching , too many threads can affect performance.

To improve this situation, the CLR provides a thread pool mechanism, one thread pool per CLR .

The CLR thread pool does not create a thread immediately when the CLR is initialized, but rather when the application is creating a thread to perform the task, the thread pool initializes one of the threads. Thread initialization is the same as other threads. After the task is completed, the thread does not destroy itself, but instead returns to the thread pool in a suspended state. Until the application sends a call to the thread pool again

Threads in the thread pool will activate the task again.

This saves the performance loss caused by creating threads, and allows multiple tasks to reuse the same thread over and over again, saving significant overhead during the lifetime of the application.

5. Processes, threads, and application domains

process: A basic concept in Windows systems that contains the resources required to run a program. Processes are relatively independent, and one process cannot access the data of another process (unless distributed computing is used), and the failure of one process to run does not affect the operation of other processes, and Windows systems use processes to divide the work into separate areas. A process can be understood as a basic boundary of a program.

application domain (AppDomain): A logical container for a set of assemblies. The CLR creates the first AppDomain (the default AppDomain) at initialization time, and the AppDomain is destroyed when the process terminates. NET is the assembly that runs in the application domain.

A process can contain multiple application domains, and an application domain can contain multiple assemblies.

Contains one or more context contexts in an application domain, using the context CLR to place the state of certain special objects in different containers

thread: The basic unit of execution in a process, the first thread that executes at the process entrance is considered the thread of the process. In. NET applications, the main () method is used as the portal, and when this method is called, the system automatically creates a main thread.

Threads are primarily composed of CPU registers, call stacks, and thread local memory (thread locally storage,tls). The CPU registers primarily record the state of the currently executing thread, and the call stack is primarily used to maintain the memory and data that the thread invokes, and TLS is primarily used to hold the thread's state information.

6. Relationship of processes, threads, and application domains

Relationships for processes, application domains, and threads, for example,

A process can include multiple application domains, including multiple threads, and threads can also travel through multiple application domains. At the same time, however, the thread is only in one application domain.

Zhou

2017/5/26

threading mechanisms, CLR thread pools, and application domains

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.