Parallel Programming in .NET 4.0 - Part 1

This is the series of articles which focuses on new Parallel Programming concepts introduction and existing System.Threading namespace enhancements in .NET Framework 4.0. These series of article will bring in depth coverage of the Parallel Programming in .NET 4.0 and we can make use of it our day-to-day development activity. It will have working samples as attachment and code snippets as well for better understanding of Parallel Programming.

This is the series of artciles which focuses on new Parallel Programming concepts introduction and existing System.Threading namespace enhancements in .NET Framework 4.0

Why Parallel Programming?

We have multi core processors in today's world. Are we really using them? We still focused on Sequential Programming. Yes. It's true. Newer processors processes faster compared to the older processors. However, only one core will be used for the processing and rest of core sits idle when you run the sequential program. Everyone in today's world goes for Dual Core and some go for Quad Core. If we are not using those cores, then there is no point in owning these many cores. Right? If we continue to program the same sequential way, then our performance will be flatter. There won't be an increase in the performance of the applications. There is a possibility that performance will decrease a bit as many cores are introduced that each are just a little less powerful than their predecessors (for less power consumption, less heat dissapation, etc.). Cleary, new tools for us developers to write applications with are necessary. Most of the Processor manufacturing companies are inclined towards having more and more cores where developers can make use of it. Unfortunately, we are not using them to the fullest.

Parallel Programming in .NET 4.0

Microsoft realized it and it wants to make the developers to make use of multi core their programming. Yes. You are right. They had Threading before has well. What's new in .NET Framework 4.0 on this threading? Developers used to got to their backfoot when they want to work on the Multi Threaded Applications. Everyone knows there are lots of issues that needs to be handled when we go for multithreading like thread abortion, thread joining, thread to thread communication. Everyone knows it's not the easies of all tasks.
Microsoft thought about it and came wit new features which supports Parallel Programming and easier for the implementation. MS don't want the brigthest developer to spend time and scratch their heads in threading application instead of focusing on the business requirement implementation.

VS 2010 and .NET Framework 4.0

There are 3 Primary improvements in VS 2010 and .NET Framework 4.0. They are

1. System.Threading improvements

  • Excerpt from MSDN: A barrier is a user-defined synchronization primitive that enables multiple threads (known as participants) to work concurrently on an algorithm in phases. Each participant executes until it reaches the barrier point in the code. The barrier represents the end of one phase of work. When a participant reaches the barrier, it blocks until all participants have reached the same barrier. After all participants have reached the barrier, you can optionally invoke a post-phase action. This post-phase action can be used to perform actions by a single thread while all other threads are still blocked. After the action has been executed, the participants are all unblocked.

  • System.Threading.CountdownEvent is a synchronization primitive that unblocks its waiting threads after it has been signaled a certain number of times. CountdownEvent is designed for scenarios in which you would otherwise have to use a ManualResetEvent or ManualResetEventSlim and manually decrement a variable before signaling the event.

2. Introduction of Parallel Extensions

  • Parallel Extensions in .NET Framework supports declarative and imperative data parallelism, imperative task parallelism and a set of data structures that can used for achieveing this. Parallel Extensions contains 3 major compoenents, PLINQ, TPL and Collection classes.

3. New Unified Cancellation Model

  • The .NET Framework 4.0 introduces a new unified model for cancellation of asynchronous or long-running synchronous operations. This model is based on a lightweight object called a cancellation token. The object that invokes a cancelable operation passes the token to the operation. That operation can in turn pass copies of the token to other operations. At some later time, the object that created the token can use it to request that the operation stop what it is doing. Only the requesting object can issue the cancellation request, and each listener is responsible for noticing the request and responding to it in a timely manner.

For the people in .NET 3.5

If you want to explore a bit on Parallel Programming, you may need to download the Reactive Extensions for .NET Framework 3.5 SP1. Microsoft released it as a separate downloadable. You won't be able to find it when you install you VS 2008 and .NET Framework 3.5 with SP1. However, one thing to remember is, Microsoft doesn't give any support for 3.5 Reactive Extensions as it's only experimental. You can downoad it from the following location Reactive Extensions for .NET Framework

In next part of this, we will cover about Barrier and System.Threading.CountdownEvent


Guest Author: Dmitry29 Jan 2013

Parallel programming in . NET Framework 4.0

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: