A probe into C # multithreading mechanism

Source: Internet
Author: User
Tags thread

Note: The code that appears in this article runs through the. NET Framework RC3 Environment

A. The concept of multithreading

Windows is a multitasking system, and if you are using Windows 2000 and above, you can view the programs and processes that are running on the current system through Task Manager. What is a process? When a program starts running, it is a process that refers to the memory and system resources used by the running programs and programs. And a process is composed of multiple threads, thread is a flow of execution in the program, each thread has its own proprietary registers (stack pointers, program counters, etc.), but the code area is shared, that is, different threads can perform the same function. Multithreading is a program that contains multiple execution streams in which multiple different threads can be run at the same time to perform different tasks, that is, to allow a single program to create multiple threads executing in parallel to accomplish their tasks. The browser is a good example of multithreading, in the browser you can download Java small application or image while scrolling the page, when accessing new pages, play animation and sound, print files and so on.

The advantage of multithreading is that you can increase CPU utilization--any programmer doesn't want its program to have a lot of time to do, in multithreaded programs, a thread must wait, the CPU can run other threads instead of waiting, which greatly improve the efficiency of the program.

However, we must also recognize that the thread itself may affect the adverse aspects of system performance to properly use the thread:

Threads are also programs, so threads need to occupy memory, and more threads consume more memory

Multithreading needs to be coordinated and managed, so CPU time tracking threads are required

Access to shared resources between threads can affect each other, and the issue of competing shared resources must be addressed

Too many threads can cause control to be too complex, and can eventually cause many bugs

Based on the above understanding, we can a metaphor to deepen understanding. Assuming that there is a company with a lot of staff in the company, then we can assume that a functioning company is a process and that the employees in the company are threads. A company must have at least one employee, for the same reason, a process contains at least one thread. In the company, you can do all the work with one employee, but efficiency is obviously not high, a person's company can not be big; A program may also use only one thread to do things, in fact, some outdated languages such as fortune,basic, but like a person's company, is inefficient, If you do a bigger program, it's less efficient--virtually no single-threaded commercial software. The more employees the company has, the more money the boss has to pay them, also have to expend a lot of energy to manage them, reconcile their contradictions and interests; The program is the same, the more resources the more the more the resource needs CPU time to track the thread, but also to solve such as deadlock, synchronization and other issues. In short, if you don't want your company to be called a "shell company," You have to have a few more employees; If you don't want your program to look childlike, introduce multithreading into your program!

In this paper, the multithreading mechanism in C # programming is discussed, and some examples are used to solve the problem of thread control and multithreading communication. In order to eliminate the cumbersome steps of creating the GUI, and to more clearly approximate the nature of the thread, all of the following programs are console programs, and the final console.readline () of the program is to stop the program in order to see the output in the execution process.

OK, cut the crap, let's experience multithreaded C #!

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.