Parallel programming technology in. NET 4 (also known as multi-core programming technology) 2

Source: Internet
Author: User

This series of articles will give a comprehensive introduction to the parallel programming technologies in. NET 4 (also known as multi-core programming technology) and applications.

The topics in this article are as follows:
1. The difference between parallel programming and multithreaded programming.
2. Advantages and disadvantages of parallel programming technology
3. When to use parallel programming

1. The difference between parallel programming and multithreaded programming.

1.1 Parallel programming.
Now with the popularity of multi-core computers, parallel programming technology, that is, multi-core programming technology is gradually called the development of the mainstream. For this reason, "parallel programming" is introduced in. NET 4. The Library and classes for some of the columns in. NET 4 provide support for parallel programming, such as Task Parallel Library,parallel LINQ.


In fact, in. NET 1.0, there are some implementations of parallel programming techniques---multi-line threading technology. The biggest problem with multithreading is that it is difficult to use and manage. In the use of multithreading, because of its complexity, it is often used to distract us with multithreading. And our original purpose was obscured.

1.2 Comparison differences
The underlying mechanism for programming in. NET 4 is actually based on multithreading. But the big difference is that it's more convenient to have parallel programming in. NET 4.


In the traditional programming model, programmers are 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 an order, and then you have to keep an eye on your soldiers and make sure that they act on your terms. (Very tired.) )


Parallel programming in. NET 4 is based on the task Parallel Library (later, TPL). In TPL, the most basic execution unit is task (Chinese can be understood as "task"), and a task represents an action you want to perform. You can define a TASK,TPL for each action you want to perform to create threads to execute the tasks you define and manage threads. TPL is task-oriented, automatic, and traditional multithreading is manual.


The task mechanism allows us to focus on the problem we are trying to solve. If the previous multithreaded technology had enabled us to give up some of the use of parallel programming, the new parallel programming technology in. NET 4 would allow us to re-establish confidence.


Although there is a new parallel technology, but the traditional multithreading technology is very useful. When we execute multiple tasks while using the parallel technology in the TPL, we do not have to care about the underlying create threads, manage threads, and so on.

2. Advantages and disadvantages of parallel programming technology
The biggest advantage of using parallel technology is that it improves the performance of the system. The parallel processing process is usually this: a task to be performed is split into a number of small parts, and these small portions are executed on different processors (which can be multi-core or many computers). Because many of these small parts are executed at the same time, they are called "parallelism".
The following questions need to be considered when using and programming:

1. Cost issues.
Concurrent execution is not free, but also expensive. Running both start and manage in parallel requires overhead, as is analogy with the creation and management of threads. The more tasks you have to perform in a program, the better it will be to use parallelism.


2. Coordination of data
If the small portions of parallel execution require shared public data, then we need to coordinate. In general, the more data that needs to be coordinated, the greater the performance loss of parallel execution. If the implementation of the various small parts are independent, then we do not have to coordinate. But a lot of times, we all need to coordinate. And the coordination of the technology is not difficult, in the following article will be described.


3. How much performance improvement
Adding a computer's CPU may increase the speed of the program, but not absolute. We know that the time that an application runs on a single core computer is not necessarily (and often is not) a dual-core computer
1/2. Therefore, the use of parallel programming may not necessarily be 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


Suggestion: If a problem can be solved by parallel programming, then use, otherwise do not. It sounds like nonsense, but it's a really good suggestion. Because parallel programming is not a panacea, it can only
Solve a class of problems, so you need to analyze the problem before use. If a problem can really be solved by a parallel solution, there are a number of factor effects, as we mentioned before. Weigh the cost after use and be good at deciding whether to use it. The following article will give a lot of examples.

Today temporarily write here, Theory said a lot of hope that the friends of the park forgive Ah. From the beginning of the next chapter in the form of Code+ explained.
Note: For a long time did not write something,: I hope you will forgive me.

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.