. NET Parallel Programming Two: parallel loops

Source: Internet
Author: User

This article mainly includes the following:

1. Conditions that can be converted into a parallel loop

2. Usage of parallel for loop: Parallel.For

3. The use of parallel ForEach Parallel.ForEach

4. Use of Parallel LINQ (PLINQ) AsParallel ()

5. Usage and differences between parallel interrupts and parallel stops

6. External control loop cancellation method (Break,stop)

7. Handling exceptions thrown in the loop body

8. Partitioned parallel method for Small loop bodies: Parallel.ForEach

9. Maximum degree of parallelism control, Parallel.for,parallel.foreach

10. Use the local task state in the loop body (the individual understanding is that the loop body contains several stages, but this phase requires thread-safe calls to these methods)

11. Try to define your own task scheduler for the parallel loop.

12. Precautions.

-------------------Gorgeous split-line-----------------------------------------

1. Conditions that can be converted into a parallel loop

. Net Framework 4.0,visualstudio2010,xp sp3,vista sp1,win7,win8,win2008 ...

The actions on the elements in the collection are the same and do not affect each other, but the order of execution does not affect the final result.

Examples of situations in life:

A: The bank's multiple service windows as a plurality of processor cores, different customer withdrawals operations are not affected by each other. When the total customer volume is certain, it is much more efficient to open multiple windows at the same time than to open only one window.

B: After the exam, the teacher sentenced the paper, the more teachers to take the test papers, the shorter the time to finish all the papers

C: Process/Initialize an array of objects of a high order of magnitude, independent of each other

2. Usage of parallel for loop: Parallel.For

 Public int 10000000 ; Double New Double [numberofsteps];              for (int0; i < numberofsteps; i++)            {                = DoWork (i);            }
The generic sequential execution of the for
   Public int 10000000  Doublenewdouble[numberofsteps];            Parallel.For (0, Numberofsteps, (i) = =                {= DoWork (i);            });
Parallel for

The method prototypes in the example are: public static parallelloopresult for (int frominclusive, int toexclusive, action<int> body);

The return value is Parallelloopresult, which indicates the status of the result, as defined below

 Public struct Parallelloopresult    {      publicboolget;}          Public Long Get ; }    }    
Parallelloopresult

There are 12 types of overloads in Parallel.For , the simplest of which is the other types of overloads that can be used to specify configurations that run in parallel, such as parallelism, unmark, and so on.

3. The use of parallel ForEach Parallel.ForEach

Not to be continued ...

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.