This series of articles will give a comprehensive introduction to parallel programming technology (also known as multi-core programming technology) and applications in. NET 4.
The topics in this article are as follows:
1. Differences between parallel programming and multi-threaded programming.
2. Advantages and Disadvantages of Parallel Programming Technology
3. When to adopt parallel programming
1. Differences between parallel programming and multi-threaded programming.
1.1 parallel programming.
Now with the popularization of multi-core computers, parallel programming technology, that is, multi-core programming technology, is also known as the mainstream of development. To this end, "parallel programming" is introduced in. NET 4 ". Libraries and classes of some columns in. NET 4 provide support for Parallel programming, such as Task Parallel Library and Parallel LINQ.
In fact, some implementations of parallel programming technology are available in. NET 1.0-multiline thread technology. The biggest problem with multithreading is that it is difficult to use and manage. Because of its complexity, multithreading is often used to distract us from multithreading. This masks our initial purpose.
1.2 comparison
The underlying mechanism of parallel programming proposed in. NET 4 is actually based on multithreading. But their biggest difference before is that parallel programming in. NET 4 is more convenient.
In the traditional programming model, programmers are responsible for creating threads, assigning tasks to threads, and managing threads. A metaphor: you have a group of soldiers, and then you give them commands, then you must always monitor your soldiers and ensure they do what you want. (Tired !)
In. NET 4, Parallel programming relies on Task Parallel Library (TPL. In TPL, the most basic execution unit is a task (which can be understood as a "task" in Chinese). A task represents an operation to be executed. You can define a task for each operation you want to execute. TPL is responsible for creating a thread to execute the task you define and managing the thread. TPL is task-oriented and automatic, while traditional multithreading is manual-oriented.
The Task mechanism enables us to focus on the problems we want to solve. If the previous multi-thread technology makes us abandon the use of some parallel programming, the new parallel programming technology in. NET 4 will allow us to build confidence again.
Although new parallel technologies are available, the traditional multi-thread technology is still very useful. When we use the parallel technology in TPL to execute multiple tasks, we do not have to worry about the underlying thread creation and management threads.
2. Advantages and Disadvantages of Parallel Programming Technology
The biggest advantage of using parallel technology is to improve the system performance. The parallel processing process is generally like this: a task to be executed is split into many small parts, and then these small parts are separately on different processors (can be a multi-core computer, it can also be executed on many computers. Because many small parts are executed at the same time, it is called "Parallel ".
When using parallel programming, consider the following:
1. Overhead issues.
Parallel execution is not free, but overhead. Both the start and Management of Parallel Running require overhead, just like the creation and management of threads. In a program, the more tasks you want to execute, the better the effect of using parallelism.
2. Data coordination
If a small part of the parallel execution needs to share public data, we need to coordinate. Generally, the more data to be coordinated, the larger the performance loss of parallel execution. If each part of the execution is independent, we do not need to coordinate it. But in many cases, we need to coordinate. In addition, the coordination technology is not very difficult, and will be described one by one in subsequent articles.
3. Performance Improvement
Adding a computer's CPU may increase the program's running speed, but it is not absolute. We know that an application runs on a single-core computer for a certain period of time (often not ).
1/2. Therefore, adopting parallel programming may not necessarily multiply the program performance. Because performance is related to many factors, hardware is a big factor.
3. When to adopt parallel programming
Suggestion: if a problem can be solved through parallel programming, it will be used; otherwise, it will not be used. It sounds nonsense, but it is indeed a good suggestion. Because parallel programming is not omnipotent
To solve the first-class problem, we need to analyze the problem before using it. If a problem can be solved using a parallel solution, there are many factors that affect it, as we mentioned earlier. The overhead after use is weighed and the difference between whether or not to use. Many examples will be provided in subsequent articles.