2

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

Andrew Simpson
  • 6,883
  • 11
  • 79
  • 179
  • 2
    The operating system does not distribute work for you. If you want to work on multiple cores, you'll have to partition and schedule each unit of work. That is what the `Task Parallel Library` does for you. The OS will schedule the work on the available CPUs. – Yuval Itzchakov Jul 13 '15 at 10:52
  • Hi, but I if use threads will this not be dome automatically for me instead? – Andrew Simpson Jul 13 '15 at 10:53
  • The TPL provides you with abstraction over the `Thread` class, which is the lowest wrapped mechanism available in .NET. It saves you the trouble of creating the parallelism yourself. Look into `Parallel.ForEach` and `Parallel.For`. Also, look into `PLINQ`. – Yuval Itzchakov Jul 13 '15 at 10:54
  • multiple threads will automatically be 'planned' on an available processor. This library just makes it easier for you to use threads. – DoXicK Jul 13 '15 at 10:54
  • @DoXicK Thanks for the clarification. I am using Threads at the moment so I might as well stick with that? – Andrew Simpson Jul 13 '15 at 10:57
  • @AndrewSimpson I strongly suggest you look further more into the TPL. Out of the box, it works with the thread-pool, which could be of use to you instead of spinning a new thread each time. – Yuval Itzchakov Jul 13 '15 at 10:58
  • Ah I see, so like a database pooling system so instead of recreating threads it uses 1 that is available? – Andrew Simpson Jul 13 '15 at 11:00
  • @Closer please explain why my question is so broad? thanks – Andrew Simpson Jul 13 '15 at 11:05
  • See [this](http://stackoverflow.com/questions/31381384/is-there-any-point-to-using-task-parallel-library#31381473) as well – Yuval Itzchakov Jul 13 '15 at 11:09
  • On top of taking care of some things for you, PLINQ has some smart heuristics built in to split up larger workloads in the most effective way for the hardware you are running on. – slugster Jul 13 '15 at 11:15
  • @slugster That is interesting though I am not using LINQ in my app. I have this app that reads 4 images streams from 4 cameras and at the moment they each have their own Thread. I was just wondering whether TPL would be better for me. I do not think for my scenario it will be – Andrew Simpson Jul 13 '15 at 11:18
  • It can be an option if you want to use the TPL along with a producer/consumer pattern (using a blocking collection) - this will help your app scale from 1 to n threads. But if all you are going to run is 4 threads then you can put those on the thread pool yourself. – slugster Jul 13 '15 at 11:21
  • 4
    If the OP is a beginner or the question isn't that smart, doesn't mean it's a bad question that deserves downvoting. IMO – Sleiman Jneidi Jul 13 '15 at 12:19
  • The CPU will not distribute the work automatically. Where did you read that? – Sleiman Jneidi Jul 13 '15 at 12:21
  • @SleimanJneidi Hi I may have mis-understood what I read. I believe what the article had said was that if I use Threads it will distibute it automatically for me. Perhaps CPU said have been OS? – Andrew Simpson Jul 13 '15 at 12:22

2 Answers2

3

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.

Community
  • 1
  • 1
tomsv
  • 7,207
  • 6
  • 55
  • 88
  • HI, yes. That was what I thought and I should have phrased my original question better. I should have added 'will not Threading' do the job for me? – Andrew Simpson Jul 13 '15 at 10:56
  • Sure. Using the Task Parallel Library will however normally be easier than using Threads. – tomsv Jul 13 '15 at 11:00
  • @AndrewSimpson, use threads as a last resort, except when you have some functionality that must occur in parallel for the entire lifetime of your application. The TPL, `async/await` and tasks are all simpler to use than threads. – David Arno Jul 13 '15 at 11:16
  • @DavidArno Thanks and understood. I will stick with Threads for my camera stream and they will only need to be created once. But, for my Task.Runs stuff I could switch to TPL.. – Andrew Simpson Jul 13 '15 at 11:22
  • I did not downvote you. I found your answer useful :) – Andrew Simpson Jul 13 '15 at 11:31
  • Whats wrong with this answer? Why its being downvoted? – Sleiman Jneidi Jul 13 '15 at 12:15
  • @SleimanJneidi I down voted because this answer doesn't do anything except summarize what's in the comments and the question itself. We **already know** about the TPL and threads and the CPU and OS won't do it automatically for you. What we **don't** know is **why** the TPL should be used instead of normal threading, which is what the question wanted answering. This answer effectively hasn't answered the question at all. – slugster Jul 13 '15 at 21:41
  • @slugster Thanks for the feedback. I tried improving the answer. Is it more helpful now? – tomsv Jul 14 '15 at 10:01
  • @tomsv Great effort on that edit, I've reversed (+1) my vote. Thanks for spending some time on it and making it worth keeping - that's what SO is all about. – slugster Jul 14 '15 at 11:41
2

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
Community
  • 1
  • 1
Sameer
  • 3,124
  • 5
  • 30
  • 57
  • Hi, thanks for those links. Whilst they explain the different ways of using Threads v TPL it does not say if it is more efficient. The only way I can see to use it is derived from the comment by @Yuval Itzchakov. – Andrew Simpson Jul 13 '15 at 11:30
  • I think it mentions effectiveness of TPL over threads. And it is up to you how do u measure efficiency -:) – Sameer Jul 13 '15 at 11:34
  • well I shall remain objective and read through all those links - thanks – Andrew Simpson Jul 13 '15 at 11:59