You are on page 1of 12

Task Parallel Library (TPL)

Parallel vs. Concurrent

Concurrency

Concurrency is about executing multiple unrelated tasks in a concurrent mode


They share system resources, but not a common
problem (i.e., they are logically independent)
Faces deadlocks and data races

Parallel

Parallelism is taking a certain task and dividing it


into a set of related tasks to be executed
concurrently.

Setting the Context of .NET Parallel


Programming

Parallel programming is all about how to partition a single


piece of work into multiple concurrent units
Partitioning Options

Static hard coding the number of unit and thread we want to


use.

Dynamic - each unit per available code

Solves scalability, but creates load-imbalance

Smallest feasible units

This is not scalable

Solves load imbalance, but creates thread overhead and creating and
killing thread is an expensive procedure

.NET Thread Pool

Maintains a scheduler and a thread queue, instead of killing thread, it


pushes the thread into queue to be picked up by other unit of work
The namespace is System.Threading.ThreadPool and it is available
since .Net v 4.0

Introduction

The purpose of the TPL is to make developers


more productive by simplifying the process of
adding parallelism and concurrency to
applications.
Starting with the .NET Framework4,the TPL is
the preferred way to write multithreaded and
parallel code

Data Parallelism

Data parallelismrefers to scenarios in which


the same operation is performed concurrently
(that is, in parallel) on elements in a source
collection or array. In data parallel operations,
the source collection is partitioned so that
multiple threads can operate on different
segments concurrently.

parallelforandforeachloops
// Sequential version foreach (var item in sourceCollection) { Process(item); }
// Parallel equivalent Parallel.ForEach(sourceCollection, item => Process(item));

If the token that signals the cancellation is the


same token that is specified in the Parallel
Options instance, then the parallel loop will
throw a singleOperationCanceledExceptionon
cancellation. If some other token causes
cancellation, the loop will throw an
Aggregated Exceptionwith an
OpearationCanceledExceptionas
an
Parallel.ForEach(nums,
po, (num) =>
{
InnerException
double d = Math.Sqrt(num);

Console.WriteLine("{0} on {1}", d, Thread.CurrentThread.ManagedThreadId);


po.CancellationToken.ThrowIfCancellationRequested();
});

Stop or Break from a Parallel.For Loop

Break (Exit for VB)

Stop

Complete all iterations on all thread that are prior to the


current iteration on the current thread and then exit the
loop
Stop all iterations as soon as convenient

Both methods will stop future iterations from


running.
Note that we cannot control whether other threads
on a loop continue to run after either stop or break
is called. We can use ParallelLoopState.IsStopped
property to check whether the loop has stopped on
another thread

Task Parallelism

One or more independent tasks running concurrently


Benefits

More efficient and more scalable use of system resources

More programmatic control than is possible with a thread or work item

Tasks are queued on ThreadPool


Since tasks are queued using ThreadPool, which uses enhanced algorithms to
adjust the thread and this makes tasks relatively lightweight and we can create
many of them
Tasks are built around them using a rich set of APIs which support waiting,
cancellation, continuations, robust exception handling, detailed status, custom
scheduling and more

Task Creation Options

None
PerferFairness
LongRunning
AttachedtoParent
DenyChildAttach
HideScheduler

Potential Pitfalls in Data and Task


Parallelism

Do Not Assume That Parallel Is Always Faster

The basic rule of thumb is that parallel loops that


have few iterations and fast user delegates are
unlikely to speedup much

Avoid Writing to Shared Memory Locations

In sequential code, it is not uncommon to read


from or write to static variables or class fields.
However, whenever multiple threads are
accessing such variables concurrently, there is a
big potential for race conditions. Even though you
can use locks to synchronize access to the
variable, the cost of synchronization can hurt
performance. Therefore, we recommend that you
avoid, or at least limit, access to shared state in a
parallel loop as much as possible.

Potential Pitfalls in Data and Task


Parallelism

Avoid Over-Parallelization

The benefits of parallelization in limited by the


number of processors on the computer
The most common scenario in which overparallelization can occur is in nested loops. In
most cases, it is best to parallelize only the outer
loop unless one or more of the following conditions
apply

The inner loop is know to be very long


You are performing an expensive computation on each
order
The target system is known to have enough processors
to handle the number of thread that will be produced by
parallelizing the query on cust.Order

Potential Pitfalls in Data and Task


Parallelism

Avoid calls to Non-Thread-Sage Methods


FileStream fs = File.OpenWrite(path);
byte[] bytes = new Byte[10000000];
// ...
Parallel.For(0, bytes.Length, (i) => fs.WriteByte(bytes[i]));

In the above example, multiple thread would be


attempting to call the FileStream.WriteByte
method simultaneously, which is not supported by
the class

Limit Calls to Thread-Safe Methods

Most static methods in the .NET Framework are


thread-safe and can be called from multiple
threads concurrently. However, even in these
cases, the synchronization involved can lead to
significant slowdown in the query.

Potential Pitfalls in Data and Task


Parallelism

Be aware of thread affinity issues


Use caution when waiting in Delegates that
are called by Parallel.Invoke
Do Not Assume that Iterations of ForEach, For
and ForAll always execute in Parallel
Avoid executing parallel Loops on the UI
Thread

You might also like