Introduction to one of the. NET 4 Parallel (multicore) programming series
This series of articles will give a comprehensive introduction to parallel programming techniques in. NET 4 (also known as multicore programming technology) and applications.
The topics in this article are as follows:
1. The difference between parallel programming and multithreaded programming.
2. Pros and cons of parallel programming technology
3. When to use parallel programming
Series Articles Links:
Introduction to one of the. NET 4 Parallel (multicore) programming series
. NET 4 Parallel (multicore) programming series two starting with task
. NET 4 Parallel (multicore) programming series three from task cancellation
. NET 4 Parallel (multicore) programming series four-task hibernation
. NET Parallel (multicore) Programming series five task execution and exception handling
End of the six-task foundation part of the. NET Parallel (multicore) programming series
. NET Parallel (multicore) programming series seven shared data issues and solutions overview
1. The difference between parallel programming and multithreaded programming.
1.1 Parallel programming.
Now with the popularization of multi-core computer, parallel programming technology, also known as multi-core programming technology, is gradually called the mainstream of development. For this reason, "parallel programming" is introduced in. NET 4. Libraries and classes in. NET 4 provide support for parallel programming, such as Task Parallel Library,parallel LINQ, and so on.
In fact, there are some implementations of parallel programming techniques in. NET 1.0---Multi-thread threading technology. The biggest problem with multithreading is that it is difficult to use and manage. The use of multithreading in use, because of its complexity, is often used to distract us from the multi-threaded. And that our original purpose was concealed.
1.2 Comparison differences
The underlying mechanism proposed in. NET 4 is also based on multithreading. But their biggest difference before is that parallel programming in. NET 4 is more convenient.
In the traditional programming model, the programmer is responsible for creating threads, assigning tasks to threads, and managing threads. An image metaphor: You have a group of soldiers, and then you give them orders, and then you have to monitor your soldiers at all times to make sure they do what you intend to do. (Very tired!) )
Parallel programming in. NET 4 is implemented by relying on the task Parallel Library (hereafter referred to as TPL). In the TPL, the most basic execution unit is a task (Chinese can be understood as a "task"), and a task represents the action you want to perform. You can define a TASK,TPL for each operation you want to perform to create a thread to execute the task you define, and to manage threads. TPL is task-oriented, automatic, while traditional multithreading is manual-oriented.
The task mechanism allows us to focus our attention on the problem we are trying to solve. The new parallel programming technology in. NET 4 allows us to re-establish confidence if the previous multithreading technology has allowed us to abandon some of the use of parallel programming.
Although there are new parallel technologies, traditional multithreading techniques are useful. When we use the parallel technology in TPL to execute multiple tasks, we do not need to create threads, manage threads, etc. at the bottom of our care.
2. Pros and cons of parallel programming technology
The greatest benefit of using parallel technology is to improve the performance of the system. The parallel process is generally the case: a task to be performed is split into a lot of small pieces, and then these very small parts are executed on different processors (one computer that can be multicore or a lot of computers). Because many of these very small parts are being executed at the same time, they are called "parallelism".
The following questions need to be considered when using and programming:
1. Overhead issues.
Parallel execution is not free, it also costs money. Running in parallel starts and manages all the overhead, just like creating and managing a thread. The more tasks you perform in a program, the better it will be to use parallelism.
2. Coordination of data
If those small portions of parallel execution need to share common data, then we must coordinate. In general, the more data that needs to be reconciled, the greater the performance loss of parallel execution. If the execution of each of the small parts is independent, then we do not have to coordinate. But a lot of times, we all need to be co-ordinated. And the coordinated technology is not very difficult, in later articles will be described.
3. How much performance is improved
Adding a computer's CPU may increase the speed of the program, but it is not absolute. We know that the time an application runs on a single-core computer is not necessarily (and often is not) a dual-core computer
So, the use of parallel programming is not necessarily multiplied to improve the performance of the program. Because the performance is related to a lot of factors, the hardware is a big factor.
3. When to use parallel programming
Recommendation: If a problem can be solved by parallel programming, then use it, or else not. It sounds like crap, but it's really a good idea. Because parallel programming is not a panacea, it can only
Solve a class of problems, so before using to analyze the problem. If a problem can really be solved in a parallel scenario, there are a number of factors, such as some of the problems we mentioned earlier. Weigh the cost after use and be good at deciding whether to use it. The following article will give a lot of examples.
Write here for the time being today, the theory tells a lot of, hope the friend in the garden excuse me! From the beginning of the next chapter in the form of code+.
Note: A long time did not write things,:), I hope you forgive me!
Copyright for the small Foreign and blog Park All, reproduced please indicate the source to the author.
Http://www.cnblogs.com/yanyangtian
Introduction to one of the. NET 4 Parallel (multicore) programming series