ehsan
ehsan

Reputation: 787

patterns of parallel programming; which pattern to choose!?

referring to the e-book patterns for parallel programming. i read the e-book and learn a lot of new things. but i wonder which pattern to choose.

for example Parallel class, task class, PLINQ, ThreadPool class, ...

is there any resource that provide more information about parallel programming in .net 4?

Upvotes: 1

Views: 266

Answers (3)

Ade Miller
Ade Miller

Reputation: 13723

Task and Parallel are not patterns, they're classes or namespaces. These classes can be used to implement various common parallel patterns. In some cases only a single class is required to do this, for example data parallelism with Parallel.ForEach. In others different classes must be used in concert to develop a solution, for example implementing a pipeline with Tasks and BlockingCollection.

The ThreadPool can actually be considered an implementation detail upon which the Task Parallel Library in .NET 4 is built. Tasks represent an abstraction over the thread pool allowing developers to specify work as Tasks and have the .NET runtime handle scheduling the work as efficiently as possible in a hardware independent and scalable way.

Patterns discussed in the book aren't really applicable at the application level, they are more like the design patterns described by the Gang of Four. Typically an application consists of many lines of code that use many different patterns. For example an image processing application might use the Model-View-Controller pattern to separate concerns of the View (GUI) and the Model. The model itself might use a pipeline and data parallelism. You can see just such an example in the pipelines chapter of the book.

Upvotes: 1

ehsan
ehsan

Reputation: 787

there is an official documentation in MSDN explaning parallel patterns and section (Selecting the Right Pattern).

Upvotes: 0

P.P
P.P

Reputation: 121397

Different patterns are designed for different types of application.

Task class: when the parallel jobs you want to execute are independent of each other & the order of completion doesn't matter. The jobs may be of irregular size. E.g. Manufacturing a car. It doesn't matter which parts(tire, engine, doors etc) are produced in which order. You only care that car is manufactured in the end and you are doing it in parallel so as to complete it as quickly as possible.

Parallel class: This type is for tasks that are independent and similar but the tasks are of almost equal size. E.g.: A simple for loop where the iterations are independent.

Threadpool class: This is somewhat a combination of the above two except that there can be certain level of dependencies between threads which can be managed by the developer (E.g.: by executing threads in some order OR executing a thread based on the results/conditions obtained from other threads etc).

Note that the choice of parallel pattern is not exclusively yours! Most of the times, one is forced to pick one or another based on the kind of application that's being implemented.

Upvotes: 1

Related Questions