- Only use thread-safe methods. Calling to non thread-safe methods in parallel programming can cause exceptions and undetected data loss.
- Most of the time, any regular loop that fulfills both of the above requirements can be converted into a parallel loop.
- Keep it simple and avoid over-parallelization (i.e.; unnecessary nested parallelization). When using parallelization there is an overhead cost to partition the work and merge the result back.
- If the parallel work is used to populate data into a collection, use one of the thread-safe collection types.
http://msdn.microsoft.com/en-us/library/dd997305.aspx
- Avoid any ordering operation if possible. By default PLINQ does not preserve the ordering sequence in the source.
http://msdn.microsoft.com/en-us/library/dd460677.aspx
- When needed to do further operation of a PLINQ result, prefer to use ForAll() method instead of Parallel.ForEach().
For example; use this
source.AsParallel()
.Where( i => i.SomePredicate() )
.ForAll( i => i.DoSomething() );
instead ofvar filteredItems = source.AsParallel().Where( i => i.SomePredicate() );
Parallel.ForEach(filteredItems, item =>
{
item.DoSomething();
});
References:
Potential Pitfalls with PLINQ
Parallelism in .NET – Part 2, Simple Imperative Data Parallelism
Parallelism in .NET – Part 6, Declarative Data Parallelism
Parallelism in .NET – Part 8, PLINQ’s ForAll Method

No comments:
Post a Comment