0

I have read in MSDN that It is not guaranteed that TPL(Task parallel library) will run the logic/code in parallel. So my question is that under what situation the code will run sequentially.?

When the code is deployed into single core processor ? Or When the .Net framework thread pool starvation happens? Or When the 'hardware threads'/'logical cores' are too busy to allocate only one 'hardware thread'/ 'logical core' at that time?

svick
  • 236,525
  • 50
  • 385
  • 514
Al.
  • 855
  • 1
  • 11
  • 24

1 Answers1

2

It is decided by the TaskScheduler set in the ParallelOptions of the TPL methods. This lets you easily replace the TaskScheduler with a custom one that could do whatever plan for paralization you want.

The default scheduler the TPL and PLINQ uses the is the ThreadPool. It will start by using one thread then add more threads as its algorithm detects that more threads would be useful (however if your task is not CPU bound the algorithm can make some incorrect assumptions and cause you problems).

I highly recommend you read the free book Patterns for Parallel Programming, it goes in to some detail about this. However the best book that I have read that goes in to a lot of detail about how the task scheduler works is Professional Parallel Programming with C# (Chapter 8 is all about Thread Pools).

I also recommend you download the package Samples for Parallel Programing with the .NET framework, it has a whole bunch of well commented projects inside it that helps explain a lot of concepts of parallel programming.

Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431