Introduction to Microsoft Parallel programming class library Parallel Extensions

Source: Internet
Author: User
Overview

Modern computers are increasingly moving towards multi-core and multi-processor development. Developers can now use this advantage to develop powerful applications to adapt to complex algorithms and a large amount of data operations.

Parallel expansion enables developers to easily compile programs that fully utilize the hardware parallel capabilities. program performance can increase with the increase in the number of processors or cores, there is no need to deal with the increasingly complex concurrent programming model.

Microsoft Parallel Extensions to the. NET Framework 3.5 is a managed programming model that supports Parallel data processing, Parallel task processing, and unified and Parallel Running hardware through a common job scheduler. After "Microsoft Parallel Extensions to the. NET Framework 3.5" is installed, a System. Threading. dll assembly is registered to GAC. You need to reference "using System. Threading. Tasks;" before calling ;". Parallel Extensions consists of Task Parallel Library (TPL) and Parallel LINQ (PLINQ ). Parallel expansion allows us to use Task Parallel Library (TPL) to achieve Task parallelization. Parallel LINQ allows developers to process data parallelization in a declarative manner.

Simple call

Define three tasks:

private void Task1()
{
Thread.Sleep(1000);
}
private void Task2()
{
Thread.Sleep(2000);
}
private void Task3()
{
Thread.Sleep(3000);
}
The code for calling these three tasks in parallel is as follows:
Parallel.Invoke(Task1, Task2, Task3);

In addition, you can place all the tasks in an Action data group before calling them. The following code snippets are useful in some scenarios, in the design phase, we do not need to consider how many tasks will be executed during the final run:

Action[] actions = { Task1, Task2, Task3 };
Parallel.Invoke(actions);




For statement loop call

Parallel. For (0, 10000, I =>
{
Console. WriteLine (I. ToString ());
});
Test results:
 

Foreach statement loop call

Parallel.ForEach(li, i =>
{
Console.WriteLine(i.ToString());
});
Test results:
 
 
Summary
  • The output on the screen shows that the loop is out of order when parallel computing is used.
  • Each task that calls the Parallel library must be independent and cannot call each other.

 

Download Demo:
 
Click here to download the Demo

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.