Multithreading (1) Understanding multithreading and Multithreading

Source: Internet
Author: User
Tags windows support

Multithreading (1) Understanding multithreading and Multithreading

Multithreading is very important in the project development process. This series will be summarized in detail. First of all, let's take a look at multithreading.

Why does windows support multithreading?

In the early age of computers, operating systems do not have the concept of threads.The entire system runs only one execution thread, including the operating system code and application code. The problem with using only one execution thread is that long-running tasks will prevent other tasks from being executed. For example, in the era of 16-bit Windows, document printing applications can easily "freeze" the entire machine.

When Microsoft designs the OS kernel of Windows NT, it decides to run each instance of the application in a process.A process is actually a collection of resources to be used by an application instance. Each process is assigned 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. Therefore, the application code cannot destroy the code and data of the operating system.

What happens if an endless loop occurs in the application? If the machine has only one CPU, it will execute an endless loop and cannot execute any other program. Microsoft's solution is thread.As a Windows concept, threads are responsible for virtualizing the CPU. Windows provides a dedicated thread (equivalent to a CPU) for each process ). If the application code is in an endless loop, the processes associated with the code will be frozen, but other processes (they have their own threads) will not be frozen and they will continue to be executed.
Threads are very powerful, because they allow Windows to respond at any time even when running tasks that run for a long time.

The development history of Multithreading can be summarized as follows: no thread (only one execution thread) ---> introducing process ---> introducing Multithreading

Thread overhead

The thread brings us both benefits and performance loss, including space and time.

1. Space

To create a thread, you must load the following resources:

  • The thread kernel object. The operating system allocates and initializes this data structure for each thread created in the system. It is mainly used to describe the attributes and context of the thread, the context is a memory block that contains a set of CPU registers. For X86, X64, and IA64 CPUs, 2500, bytes of memory are used respectively.
  • Thread environment block (TEB) is a memory block allocated and initialized in user mode (memory address that applications can quickly access, TEB consumes 1 Memory Page (4 kb for X86 and X64 CPUs and 8 kb for IA64 CPUs ).
  • User-mode stack is used to store local variables and real parameters passed to the method. It also contains an address that indicates that when the current method returns, where should the thread proceed? By default, windows allocates 1 MB of memory to the user mode stack of each thread.
  • The kernel-mode stack (kernel-mode stack) is used when application code transmits real parameters to a function in kernel mode in the operating system. For security reasons, mongowd copies these arguments from the user mode stack of the thread to the kernel mode stack of the thread. The size of the 32 windows Kernel Mode stack is 12 kb, And the 64-Bit mode is 24 KB.
  • Notification of DLL thread connection (attach) and thread separation (detach). A Windows policy is to create a thread at any time in the process, all the DllMain methods of the non-hosted DLL loaded in the process are called, and the DLL_THREAD_ATTACH flag is passed to the method. Similarly, when the thread is terminated, all the DllMain methods of the unmanaged DLL in the process will be called and the DLL_THREAD_DETACH flag will be passed to the method.

2. Time

Because windows must share the physical CPU among all threads (logical CPUs) in the system. At any given time point, windows only assigns one thread to a CPU, which can run the length of a "time slice. When the time slice expires, Windows switches the context to another thread.

Switching between time slices takes about 30 ms for windows.

Why multithreading?

1. responsive, or user experience. Generally, for winform programs, some time-consuming tasks can be handed over to another thread for processing, so that the GUI thread can be responsive to user input and operations. Otherwise, the interface will be compared.

2. improve performance. Because each CPU in windows schedules one thread and multiple CPUs can schedule threads in parallel, You can execute multiple tasks at the same time to improve performance.

Relationship between processes, threads, and application domains

Before learning multithreading, it is necessary to understand these three concepts and their relationships.

1. Glossary

Process

Or processes, which can be simply a. EXE instance.A process is a basic concept in windows. It contains the resources required to run a program. Processes are relatively independent. A process cannot access the data of another process (unless in distributed computing mode). Failure of one process does not affect the running of other processes, in Windows, work is divided into multiple independent regions by processes. A process can be understood as the basic boundary of a program.

Thread

Or Thread, which can be simply understood as a virtual CPU.A thread is the basic execution unit of a process. The first thread executed at the process entry is regarded as the main thread of the process. In. NET applications, the Main () method is used as the entry. When this method is called, the system automatically creates a Main thread. Threads are mainly composed of CPU registers, call stacks, and Thread Local Storage (TLS. The CPU register records the status of the currently executed thread. The call stack is mainly used to maintain the memory and data called by the thread. TLS is mainly used to store the state information of the thread.

Application domain

Or AppDomain, which can be simply understood as a logical container of a group of assemblies.The CLR creates the first AppDomain (default AppDomain) during initialization. This AppDomain is destroyed when the process is terminated .. . NET Assembly runs in the application domain. A process can contain multiple application domains and one application domain can contain multiple assemblies.

 

2. Relationship between processes, threads, and application domains

You can use the following two pictures and two sentences to summarize them.

1) A process can contain multiple threads and application domains.

2) A thread can be moved across multiple application domains, but at a certain time point, the thread will only be in one application domain.

Difference between foreground thread and background thread

1. The difference between the foreground thread and the background thread is that the application must run all foreground threads before exiting. For background threads, you can exit directly without considering whether or not it is running and will not throw an exception. All background threads will automatically end when the application exits.

2. By default, both the main Thread and the Thread created using the Thread are foreground threads (the threads created using the Thread pool and Task are background threads by default), unless IsBackground = true is set manually.

Differences between multithreading and Asynchronization

In many cases, multithreading and Asynchronization are considered the same thing to allow the main thread to continue execution without waiting.

However, from the perspective of the dialectical relationship, there are still differences between the two, which can be summarized in one sentence.

Asynchronization is the purpose, and multithreading is one of the ways to implement Asynchronization (for example, you can create another process to implement Asynchronization ).

 

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.