C # thread-Let you know what a thread is (1)

Source: Internet
Author: User

Thread thread, process, what is thread, what is skilled multi-thread programming?

We will discuss thread basics with you today to let you know the basic construction of threads.

Before talking about a thread, you must first understand the process.

What is a process?

When Microsoft designed the operating system kernel, they decided to run the application in a processProgramProcesses are just a collection of resources that an application instance needs to use. (Have you ever seen corn or sugar cane? For example :)

A process is like a baking tube. A process does not interfere with other processes. The operating system sets up a virtual address space for them to ensure that a process usesCodeAnd data cannot be accessed by another process. This ensures the robustness of an application.

Now again, what happens when a process enters an infinite loop?

That is to say, the program does not respond and consumes cup without limit, so that the CPU cannot execute anything else. Therefore, the process is robust (because it will not be damaged and secure ), but it will still cause the system to stop responding, which is what we often call "dead ".

At this time, Microsoft's solution is thread. That is to say, the thread runs in the process.

Speaking of threads, Let's first look at what is a thread:

    1. The thread kernel object. Each thread created by windows for an application has a thread kernel object, which contains a set of attributes that describe the thread. In addition, it also contains a thread context.ContextIt is a memory block, which is used to contain a set of CPU registers (what? Do you know the cup register? Quickly go to Baidu ).
    2. The thread environment block contains a memory block and an exception handling link. Some local data is stored and some data structures used by the GDI graph are also used. I personally don't feel useful. Just know what to do.
    3. The user mode stack is used to bring local variables and real parameters to user operations. For example, if the program executes add (I) in a function, it stores the address of the function before executing the function, then, after the function call is complete, return the address of the previous record, and then execute the next step. This is also used in the user mode stack. You must know that windows must allocate at least 1 MB of memory to it.
    4. The kernel mode stack uses the kernel mode stack to pass real parameters in the user mode stack. Has it been used for debugging? Can I modify the code when I debug the program? The key is that, after you run the program, the user mode stack cannot access the kernel stack, and all the code you write is sent to the kernel.
    5. DLL thread connection and thread separation notifications. Have you used a reference? This is why you need to quote it in advance when calling an external program. To load this program, you need to use references. Just as there is no reference, your program will go wrong.

Now let's take a look at the familiar task manager to understand the image of the thread:

See the process? See the thread?

1 thread occupies 1 MB, and 1264 threads occupy 1264 MB. I started two major programs: VS and SQL. There are not many programs.

Let's look at the number of threads for each process:

The number of threads is also seen, SQL-46 threads, maximum memory usage.

Let's talk about the common situation. Suppose you are playing a large game, and then the teacher is coming. You want to pretend that you are working, Tab + ALT, and then the screen is black, so you are in a hurry, why haven't I jumped over yet !! Hurry up !!

O (partition _ partition) O

Now let's talk about what Windows does with ALT + TAB.

 

The reason why Windows can quickly receive our operations is that we always receive our input. In fact, when you do not input, this thread has terminated this thread in advance, however, it takes only 5 milliseconds for your input to wake up the text input thread.

There is also a context switch in the middle. The switch speed is 30 milliseconds. When Windows does not respond to an operation, it does more work than you think, this feature ensures the robustness and quick response of windows.

Context switch, not invisible. Return to the scene, ALT + TAB. Now we all know that it is executing a different thread. When you switch to the desktop,

The game's thread code and data are still in the high-speed cache of the Cup (the High-speed cache can make the CPU do not need to frequently access the memory, it accesses the cache much faster than the memory ), your operation requires the CPU to access new data code, which is in the memory,

Therefore, the CPU needs to re-read the memory and fill it again to tell the cache to restore the high-speed execution status.

Now we know what happened to Alt + TAB. The time required for context switching depends on the CPU architecture and speed. The filling cache depends on the application size and CPU cache size.

 

If any of the above information is incorrect, please correct me. Thank you.

 

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.