1

What is the difference between Task class and parallel class which part of TPL at implementation point of view.?

I believe task class is having more benefits than threadpool and thread but still context switch happens in task class as well.

But parallel class is basically design to run program on multicore processor?

Roman Pokrovskij
  • 9,449
  • 21
  • 87
  • 142
Sudhir.net
  • 83
  • 9

2 Answers2

1

Your question is extremely wide and can contain lots of details as an answer, but let me restrict to specific details.

Task - Wrap a method for execution down the line, it use the Lambda (Action, Func Delegate) to do the same. You can wrap now and execute anytime later.

Parallel is an API which helps achieve the Data Parallelization, where you can divide a collection (IEnumerable type) into smaller chunks and each can be executed in parallel and finally aggregated to achieve the result

There are broadly two kinds of parallelism, in one you can subdivide the bigger task into smaller ones, wrap them in a Task type and wait for all or some of them to complete in parallel. This is task parallelism

In other one you take each data unit in a collection and work on it in a mutually exclusive manner, which is data parallelism achieved by Parallel.forEach or Parallel.For APIs

These are introduced from .Net 4.0 onward to make the parallelism easy for the developer, else we had to dabble with Thread and ThreadPool class, which require much more in-depth understanding of the working of threads, here lot of complexity is taken care of internally.

However, don't be under the impression that current mechanism doesn't use threads, both the above mentioned form of parallelism rely completely on ThreadPool threads, that's why we have all the stuff like context -switching happening, multiple threads getting invoked, just that microsoft has made developer life easy by doing it

You may want to go through following links for a better understanding, let me know if there's still a specific query:

Parallel.ForEach vs Task.Factory.StartNew

Past, Present and Future of Parallelism

Parallel.ForEach vs Task.Run and Task.WhenAll

Community
  • 1
  • 1
Mrinal Kamboj
  • 11,300
  • 5
  • 40
  • 74
  • It means you are agree that, Task class provides more beneifts than threadpool and thread but it does also context switching? – Sudhir.net Aug 02 '15 at 08:00
  • Its easier to use, more benefit is a tricky part, depending on what you call as a benefit. In my view, ease of usage of parallelism by any developer is a huge benefit in itself. It takes care of lot of complexity internally. Yes Context Switching would be there, as it relies internally on thread pool, though you can restrict it using MaxDegreeOfParallelism – Mrinal Kamboj Aug 02 '15 at 08:11
  • Benefits in Task Class: Exception handling,Result,Contionation,Parent child relationship. – Sudhir.net Aug 02 '15 at 08:13
  • Right Task makes devloper life easier when they use task for multithreaded scenario. – Sudhir.net Aug 02 '15 at 08:15
  • Then tell me When i can parallel class and when i can use task class in my code or implementing some scenarion? – Sudhir.net Aug 02 '15 at 08:16
  • Also you have mentioned that Task parallelism really happens in parallel using TASK class, Here i think context switch happens if we will use only task class ? – Sudhir.net Aug 02 '15 at 08:20
  • Answering all together: 1. Getting Result and Exception can be achieved in Threads and ThreadPool too, but you need to handle the synchronization too 3. Already answered in main, mostly we do Data Parallelism on collections, for Task Parallelism you need to subdivide main task into simple exclusive units and then execute them in parallel using Task APIs like WaitAll and WhenAll 4. How are you even measuring the context switching that's happening that happens at OS and processor level, once I get idea of how you are measuring and its not just guessing, then can explain that it exist here too – Mrinal Kamboj Aug 02 '15 at 08:26
  • For Time Being we will not focus on Parallel class. Let's concentrate on Task class--Which is meant for Task parallelism whatever you have mentioned above. But Task class uses Threadpool interally to get thread. In threadpool/thread, you are agree with contextswitch happens. I think task is only another keyword in C#4.0 which provides more functionality than thread/Threadpool. If not then tell me how task parallelism you are achieving in your code when implementation realtime projects? – Sudhir.net Aug 02 '15 at 08:41
  • Check this example - http://www.codeproject.com/Articles/747594/Understanding-Task-Parallelism-Framework-TPL-using In simple words, lets assume for completing a job you need to execute different methods Method 1 to N, now they are all mutually exclusive and can go in parallel. Ideal way would be wrapping each method in a Task type and execute all of them using Task parallelism API, they are not part of a standard collection like List, where each element has to be supplied to a method for execution, this is Data parallelism. Does this clarify to an extent – Mrinal Kamboj Aug 02 '15 at 14:29
  • In Better way Task is Enrich Multithreading rather than threapool and thread. what is the use parallel again and how both are different? – Sudhir.net Aug 04 '15 at 23:56
  • You can say that way since, it provides an easy access wrapper, without exposing unnecessary complexity. Parallel on the other hand helps do data parallelization, where you can use each data point in an IEnumerable collection, pass it to a method for processing, this is processing each data point in parallel for a faster result. There's a difference in what Task paralellization and Data parallelization aim to achieve – Mrinal Kamboj Aug 05 '15 at 05:41
  • Thanks Mrinal , Now i am getting Better idea – Sudhir.net Aug 05 '15 at 06:04
0

TPL is designed to minimize pre-emptive context-switching (caused by thread oversubscription – having more threads than cores). Task abstractions, of which TPL is an implementation, are designed for cooperative parallelism, where the developer controls when a task will relinquish its execution (typically upon completion). If you schedule more tasks than you have cores, TPL will only execute concurrently approximately as many tasks as you have core; the rest will be queued. This promotes throughout since it avoids the overheads of context-switching, but reduces responsiveness, as each task may take longer to start being processed.

The Parallel class is yet a higher level of abstraction that builds on top of TPL. Implementation-wise, Parallel generates a graph of tasks, but can use heuristics for deciding the granularity of the said tasks depending on your work.

Douglas
  • 53,759
  • 13
  • 140
  • 188