I. Introduction
An Application Software generally opens up separate threads for time-consuming or asynchronous operations. The use of multithreading helps improve the robustness and responsiveness of the software system and greatly improve the user experience. This article focuses on the basic CLR Thread Technology on the. NET platform and how to optimize the use of the thread technology in the CLR.
Ii. CLR thread Basics
In the early Windows operating system, the entire system only runs one execution thread (at that time, the operating system did not provide the thread concept). If an application runs for a long time or is in an endless loop, other programs can only wait for a long time. It is easy to cause the entire system to stop working and to restart the computer in despair. what's even more crazy is that this situation will lead to unreasonable loss of processed data. Later, Microsoft made a lot of improvements to the operating system kernel and proposed the concepts of Process and Thread.
A process is an execution process of an executable program. It is an independent unit for the system to allocate and schedule resources. Intuitively, when you start an application, you start a process, which is allocated with various resources to ensure the smooth execution of the application, such as memory, CPU, and file handle, GDI resources. The system kernel allocates independent virtual space for each process. These address spaces are only available to one application instance, all resources required for this application instance are obtained in this independent virtual space. This ensures the robustness of the application instance and improves the system security, because the virtual space is independent, it also means that the program instance is independent. You do not have to worry about cracking the code and data of another instance, or worry about the password of one program instance to be read from another instance. However, at this time, a program has an endless loop, and other programs can only die, because if there is only one CPU, the CPU will be occupied by the endless loop program. To solve this problem, Microsoft proposed the thread concept. If the process is virtual independent of the memory address space, the thread is virtual independent of the CPU. The Windows Kernel provides a dedicated thread for each process (the function is equivalent to a CPU, it is called logical CPU. In fact, the CPU of a single CPU is only used by one process at a time. Therefore, Windows must implement logical CPU (thread) Sharing of physical CPU, the mechanism used is the "time slice" Management of the CPU. Simply put, the CPU usage per unit of time is divided into many smaller time slice, the CPU is only used by one thread in each time slice, And the CPU time slice in Windows is about 30 ms). If the code of a program instance enters an endless loop, the associated process is frozen, however, other processes can continue to run. At this time, we can forcibly terminate the frozen application on the "Task Manager.
Creating a thread so that the thread enters the system and finally destroys the thread will generate space memory and execution time overhead. For example, when a process creates a new thread, it will initialize the thread kernel object, it will call the DllMain method of all DLL loaded by this process, and terminate this thread will call the DllMain method of all DLL loaded by this process. There are hundreds of DLL files for some large software. creating and destroying threads in such software processes will greatly affect the performance. On the one hand, we must create as few threads as possible for performance considerations, and on the other hand, to make the application stronger and more responsive, we must use multithreading. CLR provides some mechanisms that allow you to create as few threads as possible while maintaining the code response capability, such as the thread pool technology.
It is very easy to create a dedicated thread in CLR. Sometimes we need to load the basic data used by some software into the memory. We use a new thread to simulate this process:
NamespaceCLRThread
{
ClassProgram
{
Static VoidMain (String[] Args)
{
Thread thread= NewThread (n=>LoadBaseData ((Int) N ));
Thread. Start (4);
Console. ReadLine ();
}
/// <Summary>
///Load basic data
/// </Summary>
/// <Param name = "count"> </param>
Private Static VoidLoadBaseData (IntCount)
{
For(; Count> 0; Count--)
{
Thread. Sleep (1000);//Simulate loading of basic data
Console. WriteLine ("Load basic data {0}", Count );
}
}
}
}
System is used here. threading. the common constructor of the Thread class public Thread (ParamterizedThreadStart start) to create the Thread instance. The parameter start is the method to be executed by the Thread, this method must match the signature of the delegate void ParamerizedThreadStart (Object obj.
The instance has a Thread object and does not create a Thread in the system. A system Thread is created only after the Start method is called. The following shows some common members of the Thread.
| Attribute |
Function |
| CurrentThread |
Static attribute to get the currently running thread |
| IsBackground |
Gets or sets a value indicating whether it is a background thread. |
| Name |
Obtain or set a line |