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.
Performance
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(); stopWatch.Start(); var students = GetStudentRecords(); var result = students.AsParallel().OrderBy(student => student.Name).Take(5000); int count = result.Count(); stopWatch.Stop(); Console.WriteLine("Time taken in .net <version>: {0}ms", stopWatch.ElapsedMilliseconds); Console.ReadLine(); }
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"; }); Task.WaitAll(tasks);
//Note that the local values set by different threads will be available even after the thread exit. foreach (var value in threadLocal.Values) { Console.WriteLine(value); } }
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.
DenyChildAttach
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
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!