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 as tpl in C#. At the end of this article, you will understand What is Task Parallel Library and why we need need in C# applications.
Why we need 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 the simple word, 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 improve the performance.
Using the Task Parallel Library (TPL), we can express the parallelism in the existing sequential code, means we can express the code as the Parallel task, will be run concurrently on all the available processors.
What is Parallel Programming?
Parallel Programming is a type of programming in which many calculations or the execution of processes are carried out simultaneously.
Points to Remember while working with Parallel Programming:
- The Tasks must be independent.
- 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. 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. 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.