Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TPL - How can I force TPL to use fixed # of threads? not less

How can I force TPL to use a fixed number of threads? I know MaxDegreeOfParallelism can be used to set the upper limit, but I want the upper limit to equal the lower limit. Is this possible? How?

Because I know someone will ask =) yes, I'm sure I want to do this and yes its optimal for my scneario. =)

EDIT
The solution requires a custom Partitioner as well as the custom TaskScheduler http://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/002ff888-6e13-4d7e-a234-1632e8a1f551

like image 834
SFun28 Avatar asked Mar 08 '11 17:03

SFun28


2 Answers

You could create your own TaskScheduler if you really needed to, but using TaskCreationOptions.LongRunning is probably the best approach.

How to: Create a Task Scheduler That Limits the Degree of Concurrency

like image 169
BrandonAGr Avatar answered Oct 19 '22 18:10

BrandonAGr


Have you tried using TaskCreationOptions.LongRunning when creating the tasks? It's only a hint, but I believe it may help:

Specifies that a task will be a long-running, coarse-grained operation. It provides a hint to the TaskScheduler that oversubscription may be warranted.

It's possible that that will oversubscribe it beyond MaxDegreeOfParallelism of course... you should probably try to find a resource which explains it in detail. How are you creating your tasks, by the way?

like image 4
Jon Skeet Avatar answered Oct 19 '22 18:10

Jon Skeet