Parallel Programming Using TPL in .NET

Introduction

 
With today's computers, we have multiple cores that must be equipped to design and develop applications that can utilize these resources. We must develop programs that can run our functions in parallel to best utilize these hardware features. With Microsoft .NET, we have many different APIs that can be used to do parallel programming to speed up the execution of our programs. Today, we will look at one of the most common and powerful libraries used for this purpose, the Task Parallel Library or TPL.

What is the TPL?

 
We must have all heard about threading, in which we can create multiple threads to run multiple tasks. We can consider the TPL a wrapper or higher-level abstraction over threading. It provides us with many useful and easy to use features to run functions in parallel and improve the performance of our applications. We will learn by example in this article. We will be building a console application in .NET Core 3.1 and will cover the following topics,
  1. Creating and Running Tasks
  2. Waiting on Tasks to complete
  3. Getting results from a Task
  4. Canceling a Task
  5. Handling exceptions in a Task
  6. Access control to common code areas
So, let's begin.
 

Creating and Running Tasks

 
Let's look at the below code,
  1. using System;  
  2. using System.Threading.Tasks;  
  3.   
  4. namespace ParallelProgrammingProject  
  5. {  
  6.     class Program  
  7.     {  
  8.         static void Main(string[] args)  
  9.         {  
  10.             //First method to create and start a Task using TPL  
  11.             Task.Factory.StartNew(() =>  
  12.             {  
  13.                 Console.WriteLine("This is the first method to create and start a task using TPL");  
  14.             });  
  15.   
  16.             //Second method to create a Task using TPL  
  17.             var task = new Task(() =>   
  18.             {  
  19.                 Console.WriteLine("This is the second method to create a task using TPL");  
  20.             });  
  21.             // Next, we have to start the Task  
  22.             task.Start();  
  23.   
  24.             Console.Write("Program Complete...");  
  25.             Console.ReadKey();  
  26.         }  
  27.   
  28.     }  
  29. }  
Here, you see there are two methods to create a new task. In the first, we both create and start the task whereas in the second we first create the task. Then we run it. Once this application is run, we can see the following
 
Parallel Programming Using TPL In .NET
 
If we run the application again, we get the following:
 
Parallel Programming Using TPL In .NET
 
You can see that the order in which we see the output changes. It is not in the order it is written in the code, as the three writes to console are being executed on separate threads in parallel.
 

Waiting on Tasks to Complete

 
Sometimes, we would like to wait for the tasks to complete before we proceed to the next step. This will be a blocking operation and the next line of code will only execute after the task has been completed, as shown below:
  1. using System;  
  2. using System.Threading.Tasks;  
  3.   
  4. namespace ParallelProgrammingProject  
  5. {  
  6.     class Program  
  7.     {  
  8.         static void Main(string[] args)  
  9.         {  
  10.             //First method to create and start a Task using TPL  
  11.             var task1 = Task.Factory.StartNew(() =>  
  12.             {  
  13.                 Console.WriteLine("This is the first method to create and start a task using TPL");  
  14.             });  
  15.   
  16.             task1.Wait();  
  17.   
  18.             //Second method to create a Task using TPL  
  19.             var task2 = new Task(() =>   
  20.             {  
  21.                 Console.WriteLine("This is the second method to create a task using TPL");  
  22.             });  
  23.             // Next, we have to start the Task  
  24.             task2.Start();  
  25.   
  26.             task2.Wait();  
  27.   
  28.             Console.Write("Program Complete...");  
  29.             Console.ReadKey();  
  30.         }  
  31.   
  32.     }  
  33. }  
When we run this application, we get the below screenshot:
 
Parallel Programming Using TPL In .NET
 
We can run this program multiple times and always get the same output. The reason for this is that after we start task one, we wait for it to complete and then we start task 2 and then we wait for it to finish. This sort of makes the application sequential and defeats the purpose of using tasks. However, I have just used this for demo purposes.
 
Also, we might need to use this when we want the result from one task and need to use this in the second task. Next, let us look at returning values from tasks.
 

Getting Results from a Task

 
Many times, we would need to get some results from a task. Let's look at how this is done:
  1. using System;  
  2. using System.Threading.Tasks;  
  3.   
  4. namespace ParallelProgrammingProject  
  5. {  
  6.     class Program  
  7.     {  
  8.         static void Main(string[] args)  
  9.         {  
  10.             //First method to create and start a Task using TPL  
  11.             var task1 = Task.Factory.StartNew<string>(() =>  
  12.             {  
  13.                 Console.WriteLine("This is the first method to create and start a task using TPL");  
  14.                 return "Hello";  
  15.             });  
  16.   
  17.             var value1 = task1.Result;  
  18.   
  19.             //Second method to create a Task using TPL  
  20.             var task2 = new Task<string>(new Func<objectstring>((str) =>   
  21.             {  
  22.                 Console.WriteLine("This is the second method to create a task using TPL");  
  23.                 return ($"{str} World!");  
  24.             }), value1);  
  25.   
  26.             // Next, we have to start the Task  
  27.             task2.Start();  
  28.   
  29.             var value2 = task2.Result;  
  30.   
  31.             Console.WriteLine($"{value2}");  
  32.   
  33.             Console.Write("Program Complete...");  
  34.             Console.ReadKey();  
  35.         }  
  36.   
  37.     }  
  38. }  
In the above example, we first create a task which returns a string type. We then get the result of the task and store it in a variable. Please note that getting the result is a blocking operation and will pause the application until the result is not received. Next, we pass this value as a parameter to the next task and wait for the result of the second task. Finally, we print the result of the second task on the screen.
 

Canceling a Task

 
Next, we will see how we can cancel a running task. For this, we will use a cancellation token source and a cancellation token, as shown below:
  1. using System;  
  2. using System.Threading;  
  3. using System.Threading.Tasks;  
  4.   
  5. namespace ParallelProgrammingProject  
  6. {  
  7.     class Program  
  8.     {  
  9.         static void Main(string[] args)  
  10.         {  
  11.             var cts = new CancellationTokenSource();  
  12.   
  13.             //First method to create and start a Task using TPL  
  14.             var task1 = Task.Factory.StartNew(() =>  
  15.             {  
  16.                 for(var i=0;i<10;i++)  
  17.                 {  
  18.                     if (cts.Token.IsCancellationRequested)  
  19.                         break;  
  20.                     Console.WriteLine($"The Number is {i.ToString()}");  
  21.                     Thread.Sleep(2000);   
  22.                 }  
  23.             }, cts.Token);  
  24.   
  25.             Console.ReadKey();  
  26.   
  27.             cts.Cancel();  
  28.   
  29.             Console.WriteLine("Program Complete...");  
  30.   
  31.         }  
  32.   
  33.     }  
  34. }  
Here, we see that we create a cancellation token source. Then we create a cancellation token from it and pass that to the task. Now, as the task is running and printing numbers on the screen, if we click any key, the cancel process is initiated and the loop breaks on the line where we are checking to see if the cancellation has been requested. Please note that we can add the cancellation at multiple points in the task. When the control gets to that point and sees that a cancellation has been requested, it will execute the action specified.
 

Handling Exceptions in a Task

 
In the previous example, we saw that when we cancel a task, we simply called the break statement to end the loop. However, a much better way to cancel a task is to throw an exception. Below is an example to throw an exception from a task and how to handle it:
  1. using System;  
  2. using System.Threading;  
  3. using System.Threading.Tasks;  
  4.   
  5. namespace ParallelProgrammingProject  
  6. {  
  7.     class Program  
  8.     {  
  9.         static void Main(string[] args)  
  10.         {  
  11.             var task1 = new Task(() =>  
  12.             {  
  13.                 for(var i=0; i<10; i++)  
  14.                 {  
  15.                     if (i > 5)  
  16.                         throw new Exception();  
  17.   
  18.                     Console.WriteLine($"The Number is {i.ToString()}");  
  19.                     Thread.Sleep(2000);   
  20.                 }  
  21.             });  
  22.   
  23.             try  
  24.             {  
  25.                 task1.Start();  
  26.                 task1.Wait();  
  27.             }  
  28.             catch (AggregateException ae)  
  29.             {  
  30.                 ae.Handle(e =>  
  31.                 {  
  32.                     Console.WriteLine("An exception has been thrown by the task");  
  33.                     return true;  
  34.                 }  
  35.                 );  
  36.             }  
  37.   
  38.             Console.WriteLine("Program Complete...");  
  39.         }  
  40.     }  
  41. }  
Here, we see that if we added the try catch inside the task, it would not be visible to the main program. Therefore, the solution is to add it around the Wait call. This will then propagate the exception thrown by the task to the catch statement. We can then use the aggregate exception type to check the exception and handle it.
 

Access Control to Common Code Areas

 
Finally, I would like to show how to access areas of code that could be called from multiple tasks. We need to ensure that if a piece of code is being called from multiple tasks then the results remain correct and the results are not messed up. Let's look at the below example,
  1. using System;  
  2. using System.Threading.Tasks;  
  3.   
  4. namespace ParallelProgrammingProject  
  5. {  
  6.     class Program  
  7.     {  
  8.   
  9.         public static int total = 0;  
  10.   
  11.         static void AddValues()  
  12.         {  
  13.             for(var i=0; i < 1000000; i++)  
  14.             {  
  15.                 total += i;  
  16.             }  
  17.         }  
  18.   
  19.         static void RemoveValues()  
  20.         {  
  21.             for (var i = 0; i < 1000000; i++)  
  22.             {  
  23.                 total -= i;  
  24.             }  
  25.         }  
  26.   
  27.         static void Main(string[] args)  
  28.         {  
  29.             var task1 = new Task(AddValues);  
  30.             var task2 = new Task(RemoveValues);  
  31.   
  32.             task1.Start();  
  33.             task2.Start();  
  34.   
  35.             Task.WaitAll(new Task[] { task1, task2 });  
  36.   
  37.             Console.WriteLine($"The value of total is {total}");  
  38.   
  39.             Console.WriteLine("Program Complete...");  
  40.         }  
  41.     }  
  42. }  
If we run the above code, we would expect to see the final total as zero. However, we see the below screen:
 
Parallel Programming Using TPL In .NET 
 
The reason for this is that:
 
Total += i
And
Total -= i
 
These are not atomic. They consist of two statements where the value is first stored in a temporary variable and then assigned back to the variable total. Hence, when such a statement is called from multiple threads/tasks, we see that the results are messed up. To fix this, we need to add a locking mechanism to ensure that these lines are only called by one thread at a time. For this, we use the lock mechanism below:
  1. using System;  
  2. using System.Threading.Tasks;  
  3.   
  4. namespace ParallelProgrammingProject  
  5. {  
  6.     class Program  
  7.     {  
  8.   
  9.         public static readonly Object codelock = new Object();  
  10.   
  11.         public static int total = 0;  
  12.   
  13.         static void AddValues()  
  14.         {  
  15.             for(var i=0; i < 1000000; i++)  
  16.             {  
  17.                 lock(codelock)  
  18.                     total += i;  
  19.             }  
  20.         }  
  21.   
  22.         static void RemoveValues()  
  23.         {  
  24.             for (var i = 0; i < 1000000; i++)  
  25.             {  
  26.                 lock (codelock)  
  27.                     total -= i;  
  28.             }  
  29.         }  
  30.   
  31.         static void Main(string[] args)  
  32.         {  
  33.             var task1 = new Task(AddValues);  
  34.             var task2 = new Task(RemoveValues);  
  35.   
  36.             task1.Start();  
  37.             task2.Start();  
  38.   
  39.             Task.WaitAll(new Task[] { task1, task2 });  
  40.   
  41.             Console.WriteLine($"The value of total is {total}");  
  42.   
  43.             Console.WriteLine("Program Complete...");  
  44.         }  
  45.     }  
  46. }  
Now, when we run the application, we always will receive the below output:
 
Parallel Programming Using TPL In .NET 
 

Summary

 
In this article, I tried to discuss the Task Parallel Library (TPL) and how we can use it to do parallel programming. Running tasks in parallel is especially important these days when we have hardware that has multiple cores and we need to utilize these to make our application perform better. However, parallel programming comes with several tricky situations, like the one we saw where we need to apply a locking mechanism to ensure accurate results.