Until recently, I was not aware that there was more than one core available in the latest computers. I came to know that applications can be developed in such a way that they can use all available cores to increase the performance. So I thought to share this because it might help at least a few of us.
- Hardware is getting more and more powerful day by day. These days, with the advancement in hardware, we are having more than a single core in Computer/ Laptop/ Workstations.
- With this in mind, even the .net applications we develop have to be designed so that they can use all the cores available to increase the performance of the application.
- One of the ways to achieve this is to create a multi-threaded application where multiple threads can be created to complete the multiple tasks at the same time so that the response time can be reduced to the maximum extent possible.
- Also with multi-threaded applications, we can ensure that the user interface stays active all the time without getting frozen at any point of time as other tasks are executed by other threads.
- By default, there is one thread always in C# programs. We can call this thread UI or the primary thread which has interaction with UI elements. This thread is responsible for UI remaining active all time. And on top to that, the additional threads we create will be called worker threads.
- In the classic threading model, as a developer, we need to take care of the creation of additional threads required and assigning tasks to these threads etc.
- Now we have Task Parallel Library in .NET Framework 4.5 which supports multi-core programming, through which we can achieve parallelism/concurrency with much greater ease when compared to classic threading.
- TPL library is just an abstraction of traditional threading model. It is built on classic threading model. When we use this library, creation of threads is required, maintaining thread pool, assigning tasks to threads that are in ready state etc., all are taken care of internally.
- Task is the central thing in Task Parallel Library. It can be referred as an independent asynchronous operation in our application. TPL library takes care of threads and thread pool during runtime based on tasks created and the number of cores available.
- TPL consists of public APIs which supports New Thread Pool, New Task Abstraction, Parallel Loops, PLINQ (Language Integrated Query Parallelized).
- Parallelism can be divided into two categories mainly
- Data Parallelism
- Task Parallelism
Data Parallelism
In Data Parallelism, the same operation is performed on different partitions of a collection concurrently. This can be achieved through parallel for, foreach loops and Invoke () which are part of System.Threading.Tasks.Parallel class.
Task Parallelism
Refers to one or more independent asynchronous tasks running concurrently.
- Using multiple cores isn't always a good idea. The decision must be made by analyzing the tasks involved in the application. If there are tasks that don’t take much time, then parallelizing would not enhance the performance but rather degrade the performance by creating an overhead of creating more threads to make use of multiple cores to process small tasks.
- If there is some task which takes more time to execute it can be divided into small tasks which can run in parallel independently, then we can go for parallelizing these tasks.