Visual Studio 2010 and the .NET Framework 4 enhance support for parallel programming by providing a new runtime, new class library types, and new diagnostic tools. This presentation is all about parallel programming and its features.
Programming to leverage multicores or multiple processors is
called parallel programming. This is a subset of the broader
concept of multithreading. - http://www.albahari.com/
ANUJ KUMAR SAHU
• Introduction and Concept
• TPL - Task Parallelism
• Parallel Class
• Many personal computers and workstations have two or
four cores (that is, CPUs) that enable multiple threads to
be executed simultaneously.
• To take advantage of the hardware of today and
tomorrow, you can parallelize your code to distribute
work across multiple processors.
• In the past, parallelization required low-level
manipulation of threads and locks.
• Visual Studio 2010 and the .NET Framework 4 enhance
support for parallel programming by providing a new
runtime, new class library types, and new diagnostic
• These APIs are collectively known (loosely) as PFX
Two strategies for partitioning work among
threads: data parallelism and task parallelism
Data parallelism - When a set of tasks must be performed on many data
values, we can parallelize by having each thread perform the (same) set
of tasks on a subset of values.
• Parallel Class
Task parallelism - partition the tasks i.e. each thread perform a different
• is the lowest-level approach to parallelization with PFX.
• tuned for leveraging multicores.
• Classes - Task ; Task<TResult> ; TaskFactory etc…
• PLINQ automatically parallelizes local LINQ queries.
• To use PLINQ, simply call AsParallel() on the input
sequence and then continue the LINQ query as usual.
• PLINQ is only for local collections: it doesn’t work with
LINQ to SQL or Entity Framework because in those cases
the LINQ translates into SQL which then executes on a
• If you need order preservation, you can force it by
calling AsOrdered() after AsParallel(); however incurs a
performance hit with large numbers of elements.
• Does not works - Take, TakeWhile, Skip,
and SkipWhile; indexed versions of Select, SelectMany,
• Three static methods in the Parallel class
o Parallel.Invoke - can execute several delegates in parallel. It takes array
of tasks (Actions) as parameters.
o Parallel.For - fromInclusive, toExclusive and step.
o Parallel.ForEach - is a multi-threaded implementation of a common loop
construct in C#, the foreach loop. It has numerous overloads; the most
commonly used has the following signature:
public static ParallelLoopResult ForEach<TSource>(
TPL – TASK PARALLEL LIBRARY
Task For managing a unit for work
Task<TResult> For managing a unit for work with a
TaskFactory For creating tasks
TaskFactory<TResult> For creating tasks and continuations
with the same return type
TaskScheduler For managing the scheduling of tasks
TaskCompletionSource For manually controlling a task’s
WHY TASK AND NOT THREADS?
• The creation of a thread comes with a huge cost.
Creating a huge number of Threads within your
application also comes with an overhead of
Context Switching. In a single core environment, it
might lead to a bad performance as well, since we
have a single core which serves various threads.The
task on the other hand, dynamically calculates if it
needs to create different threads of execution or
not. It uses the ThreadPool under the hood, in order
to distribute the work, without going through the
overhead of Thread creation/or un-necessary
context switching if not required.
• Creating a task
o Task.Factory.StartNew (() => Console.WriteLine ("Hello from a task!"));
o var task = new Task (() => Console.Write ("Hello"));
• Waiting on Tasks
• And many mores….
• Date Race condition
• Debugging //example was built by Microsoft.
• Error handling
• New .NET 4.5 async and await Features