Andrew Simpson
Andrew Simpson

Reputation: 7314

Is there any point to using Task Parallel Library

I have a quad core PC.

I had considered programmatically of uterlising multi-core processing using the Task Parallel Library. However, when I Googled for examples I was informed that the CPU will handle this automatically and is best to leave it alone.

Now, I find another article singing the praises of this library on Code Project.

Is there any advantage to using this library?

thanks

Upvotes: 2

Views: 2384

Answers (2)

tomsv
tomsv

Reputation: 7277

Unless your application is actively taking advantage of parallel processing, neither the OS nor the CPU will do this for you automatically. The OS and CPU may switch execution of your application between multiple cores, but that does not make it execute simultaneously on the different cores. For that you need to make your application capable of executing at least parts in parallel.

According to MSDN Parallel Processing and Concurrency in the .NET Framework there are basically three ways to do parallel processing in .NET:

  1. Managed threading where you handle the threads and their synchronization yourself.
  2. Various asynchronous programming patterns.
  3. Parallel Programming in the .NET Framework of which both the Task Parallel Library and PLINQ are a part.

Reasons for using the TPL include that it and the accompanying tools according to the MSDN article

simplify parallel development so that you can write efficient, fine-grained, and scalable parallel code in a natural idiom without having to work directly with threads or the thread pool.

Threads vs. Tasks has some help for deciding between threads and the TPL with the conclusion:

The bottom line is that Task is almost always the best option; it provides a much more powerful API and avoids wasting OS threads.

The only reasons to explicitly create your own Threads in modern code are setting per-thread options, or maintaining a persistent thread that needs to maintain its own identity.

Upvotes: 3

Sameer
Sameer

Reputation: 3183

Task parallel Library conducts its act through Task Schedulers .You can configure your TPL to which scheduler it uses. You can write your custom task scheduler which can create one thread for one task. This way you can have configuration advantage over managing your thread . Sth similar to advantage of using Dependency Injection framework over DIY-DI.

And there are already many SO entries for difference between task and Thread

  1. Task vs Thread
  2. Task vs Thread
  3. Multithreading or TPL

Upvotes: 2

Related Questions