Improvements to Parallelism in .Net Framework 4.5

Parallelism has become a buzz word in the .NET Framework world, making it important for every developer to keep up to date on the latest and greatest. The .NET Framework, version 4.5 has shipped with quite a number of updates and improvements to the parallelism features, many of which Microsoft implemented based on user feedback and common feature requests. This article walks you through a few of these new improvements.


In .net framework 4.5 a lot of work has been done to improve the performance of parallel computing. For developers there wouldn’t be any difference between the code that is written between 4.0 and 4.5 versions but there will be an increase in the performance of the parallel computing operations. Listed below are some areas where there will be a significant amount of performance improvement.

1. Long list of dependent task execution

2. PLinq

3. Concurrent Collections

Below is a code sample where the order by and take are done using PLinq. The same code was run on both 4.0 and 4.5 versions. Fig 1.0 shows the comparison of the results in milliseconds.

static void Main(string[] args)
            Stopwatch stopWatch = new Stopwatch();
            var students = GetStudentRecords();
            var result = students.AsParallel().OrderBy(student => student.Name).Take(5000);
            int count = result.Count();
            Console.WriteLine("Time taken in .net <version>: {0}ms", stopWatch.ElapsedMilliseconds);

Comparison Results
Fig 1.0: Comparison Results

Values Property – in ThreadLocal Class

ThreadLocal is a class introduced in .net framework 4.0 in order to store per-instance and per-thread resources. The value stored in the ThreadLocal object goes off when the corresponding thread exits or when the ThreadLocal object instance is disposed. In .net framework 4.5 there is a new property added to the ThreadLocal class named Values. It will hold the values maintained or stored by different threads and it is an IEnumerable<T> type. These values will remain even if the respective thread exits.

The population of the Values property can be made optional by passing the trackAllValues parameters as false to the ThreadLocal constructor. The code given below will depict the Values property of ThreadLocal class.

static void Main(string[] args)
            var threadLocal = new ThreadLocal<string>(() => String.Empty, trackAllValues: true);
            var tasks = new Task[2];
            tasks[0] = Task.Factory.StartNew(() =>
                threadLocal.Value = "Dummy value 1";
            tasks[1] = Task.Factory.StartNew(() =>
                threadLocal.Value = "Dummy value 2";
            //Note that the local values set by different threads will be available even after the thread exit.
            foreach (var value in threadLocal.Values)

New TaskCreation and TaskContinuation Options

There are two new TaskCreation and TaskContinuation options introduced in .net framework 4.5. These options provide you more control while calling some third party codes in your task. Below are the new options.


The AttachToParent option in a child task will not allow the parent task to be completed until the child task completes. Say for example if you are calling a third party code in your task and you do not want any task inside it to get attached to your task then this option can be used. In such a scenario even if the third party code task does an AttachToParent it will be notified that there is no parent task available and hence will not attach.

var task = Task.Factory.StartNew(() =>
        //Perform the operation calling a 3rd party code
}, TaskCreationOptions.DenyChildAttach);


HideScheduler option will hide your scheduler from the third party code that you are calling inside your task. The TaskScheduler.Current will return always TaskScheduler.Default. In other words your TaskScheduler will not be applied to the child task in case the later doesn’t specify any and the default one will be used.

Another useful feature is that .net framework 4.5 will now allow you to specify the timeout for the cancellation token. The cancellation request will be issued after the specified timeout.

Happy reading!

Related Articles


  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Protecting business operations means shifting the priorities around availability from disaster recovery to business continuity. Enterprises are shifting their focus from recovery from a disaster to preventing the disaster in the first place. With this change in mindset, disaster recovery is no longer the first line of defense; the organizations with a smarter business continuity practice are less impacted when disasters strike. This SmartSelect will provide insight to help guide your enterprise toward better …

  • Live Event Date: August 14, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Data protection has long been considered "overhead" by many organizations in the past, many chalking it up to an insurance policy or an extended warranty you may never use. The realities of today make data protection a must-have, as we live in a data driven society. The digital assets we create, share, and collaborate with others on must be managed and protected for many purposes. Check out this upcoming eSeminar and join eVault Chief Technology …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds