Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

Parallel Programing in C# at a glance

, 4 Mar 2014
Rate this:
Please Sign up or sign in to vote.
Objective : In this article,  I will provide the summary about Parallel Programming in C# 4.0 with comparative study of the PFX(Parallel Programming Framework) technologies with each other. My intention of writing this article is to assist you decide which PFX technology you should use in which scen

Objective : In this article,  I will provide the summary about Parallel Programming in C# 4.0 with comparative study of the PFX(Parallel Programming Framework) technologies with each other. My intention of writing this article is to assist you decide which PFX technology you should use in which scenario.

I am assuming that you are aware of the PFX technologies. So here I will not explain them in detail. Rather I would provide the summary of them to revise the concepts. Before that, I would explain some important concepts of the Parallel Programming.

Parallel ProgrammingProgramming to leverage multicores or multiple processors.

Parallel Programming uses one of the two strategies for partitioning work among threads: data parallelism and task parallelism.

Data Parallelism

  • Divide data to be processed into chunks and allocate those chunks to separate threads for processing.
  • Structured parallelism, which means that parallel work units start and finish in the same place in your program.
  • Simpler and less error prone and allows your to farm the difficult job of partitioning and thread coordination and result coalition out to libraries.

Task Parallelism

  • Divide tasks and allocate those tasks to separate threads for processing.
  • Unstructured parallelism, meaning that parallel work units may start and finish in places scattered across your program.

Below is the table which summarizes PLINQ, Parallel class and Task Parallel Library.

PFX (Parallel Framework) = PLINQ + TPL + Concurrent Collection + SpinLock and SpinWait
Task Parallel Library (TPL)
Structured Data Parallelism
PLINQ Parallel Class Task Parallelism
Partition Y Y N
Collation Y N N
Declarative — You simply declare that you want to parallelize your work and framework does paritioning and collation for you. Imperative- You need to explicitly write code to partition or collate.
About * Implement Parallel Programming with simple LINQ without worrying about complexities involved.
* PLINQ requires exactly N threads, which is specified by using the WithDegreeOfParallelism()
* PLINQ store your results in a thread-safe manner automatically.
* By default, ordering is not preserved. You need to use AsOrdered() after AsParallel().
* Parallel requires N or less threads, which is specified by using ParallelOptions.MaxDegreeOfParallelism.
* Parallel.ForEach expects you to store your results in a thread-safe manner.
* Ordering can not be preserved.
* Cooperative cancellation.
* Wait on a set of tasks — without a signaling construct.
* Attach “continuation” task(s).
* Schedule a continuation based on multiple antecedent tasks.
* Propagate exceptions to parents, continuations, and task consumers.
Use * When you want to execute operations in parallel and then wait for them to complete. This includes non-CPU-intensive tasks such as calling a web service.
* When problem is embarrassingly parallel.
you want to run some operation on a pooled thread, and also to manage a task’s workflow through continuations and parent/child tasks.
* For order preservation using AsOrdered().
* For stream processing.
* For Operating over Two Collections.
* For performing Independent Actions.
* For Thread-Local state access.
How to use simply call AsParallel() on the input sequence and then continue the LINQ query as usual. Parallel.Invoke – executes an array of delegates in parallel.
Parallel.For - parallel implementaion of a C# for loop.
Parallel.ForEach - parallel implementation of a C# foreach loop.
Task for unit for work.
Task<TResult> for a unit for work with a return value.
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 workflow.
Exception If a PLINQ query throws an exception, it’s rethrown as an AggregateException whose InnerExceptions property contains the real exception(s).
Concurrent Collection- you want a thread-safe queue, stack, or dictionary.
SpinLock and SpinWait - you want to implement producer/consumer structures.

In my next articles, I would explain these PFX technologies in detail. Hope it helps you decide  which technology to use in which scenario with reasoning.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Adarsh Chaurasia (Consultant|Mentor|Tech Savvy)
Software Developer (Senior)
India India
I have 4.6+ years of experience in SaaS, SOA based Enterprise Web Application design and development using Microsoft technology stack. I have mostly worked on Business layer, Data access layer, WCF, Entity Framework, Microsoft Application Blocks, Search engines, APIs integration, Third party APIs/Product Research & Development.
I am a huge fan of Design Patterns. I also work as Software Consultant. I read/write blogs, help and learn from other developers.
Follow on   Twitter   LinkedIn

Comments and Discussions

GeneralMy vote of 3 Pinmemberkartelo4-Mar-14 21:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140921.1 | Last Updated 5 Mar 2014
Article Copyright 2014 by Adarsh Chaurasia (Consultant|Mentor|Tech Savvy)
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid