Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to throttle many .NET Task instances?

I have created a large amount of Task instances. I need to run them all, and wait for them all to complete. The problem is that I need to make sure that no more than X tasks are between "started" and "completed" at any given time; the tasks involve calls to other parties that have a restriction on the number of simultaneous calls. Since these restrictions are not based on my hardware, I can't rely on any built in intelligent throttling; I need to enforce the limit strictly. I've been able to do this by having the tasks increment and decrement a shared variable in a thread-safe way, but it seems unnecessarily cumbersome. Is there a way that's more built-in to the API directly, or an easy synchronization tool I'm missing?

like image 983
GWLlosa Avatar asked Feb 17 '11 21:02

GWLlosa


2 Answers

You have to create your own task scheduler, see How to: Create a Task Scheduler That Limits the Degree of Concurrency

Edit: This requires more code than with Semaphore, but I have a feeling that this might perform better, because thread pool in which tasks are executed is unaware of your semaphore, but using TaskScheduler is playing by their rules.

Edit 2: One of possible drawback of using semaphore is that thread pool might think that your task is doing IO operations and schedule them a lot at a time (so that they hang there and wait, because they don't need CPU). Using TaskScheduler they will be scheduled exactly when there is place for them. It will definitely keep pool cleaner.

like image 60
Andrey Avatar answered Sep 27 '22 21:09

Andrey


The Semaphore class might work for you: http://msdn.microsoft.com/en-us/library/system.threading.semaphore.aspx

There is a good example on this MSDN page to show you how to use it.

There may also be lighter-weight built-in thread pooling/synchronization mechanisms you can use to accomplish the same end, but Semaphore is basically designed to do what you are asking for.

like image 21
Andy White Avatar answered Sep 27 '22 19:09

Andy White