Task Parallel Library in C# with Examples (TPL in C#)
In this article, I am going to give you an overview of the Task Parallel Library in C# with Examples. Please read our previous section articles where we discussed Task-Based Asynchronous Programming in C#. The Task Parallel Library is also referred to as TPL in C#. At the end of this article, you will understand What is Task Parallel Library is and why do we need it in C# applications.
Why do we need Task Parallel Library in C#?
We can’t expect our sequential program to run faster on the new processors as we know the processor technology advances means the focus is on Multicore-processors. Today’s desktop typically has 4 cores but the latest experimental multi-core chips have up to 1000 cores.
So in simple words, we can say that the multicore processor machines are now becoming standard and the aim is to improve the performance by running a program on multiple processors in parallel. So by considering the above scenario, the .NET Framework 4 introduces Task Parallel Library (TPL) that makes it easier for developers to write parallel programs that target multi-core machines (automatically use multiple processors) which improves the performance.
Using the Task Parallel Library (TPL), we can express the parallelism in the existing sequential code, which means we can express the code as the Parallel task, which will be run concurrently on all the available processors.
What is Parallel Programming in C#?
Parallel Programming is a type of programming in which many calculations or the execution of processes are carried out simultaneously. The Points to Remember while working with Parallel Programming:
- The Tasks must be independent.
- The order of the execution does not matter
C# Supports Two Types of Parallelism:
Data parallelism: In the case of Data Parallelism, the operation is applied to each element of a collection. This means each process does the same work on unique and independent pieces of data.
Task parallelism: In the case of Task Parallelism independent computations are executed in parallel. This means each process performs a different function or executes different code sections that are independent.
In the next article, I am going to discuss the Parallel For Method in C# with some examples. Here, in this article, I try to give you an overview of the Task Parallel Library (TPL) in C#. I hope you enjoy this article.