C# thread pool limiting threads

Note: if you are limiting this to “3” just so you don’t overwhelm the machine running your app, I’d make sure this is a problem first. The threadpool is supposed to manage this for you. On the other hand, if you don’t want to overwhelm some other resource, then read on!


You can’t manage the size of the threadpool (or really much of anything about it).

In this case, I’d use a semaphore to manage access to your resource. In your case, your resource is running the web scrape, or calculating some report, etc.

To do this, in your static class, create a semaphore object:

System.Threading.Semaphore S = new System.Threading.Semaphore(3, 3);

Then, in each thread, you do this:

System.Threading.Semaphore S = new System.Threading.Semaphore(3, 3);

try
{
    // wait your turn (decrement)
    S.WaitOne();
    // do your thing
}

finally {
    // release so others can go (increment)
    S.Release();
}

Each thread will block on the S.WaitOne() until it is given the signal to proceed. Once S has been decremented 3 times, all threads will block until one of them increments the counter.

This solution isn’t perfect.


If you want something a little cleaner, and more efficient, I’d recommend going with a BlockingQueue approach wherein you enqueue the work you want performed into a global Blocking Queue object.

Meanwhile, you have three threads (which you created–not in the threadpool), popping work out of the queue to perform. This isn’t that tricky to setup and is very fast and simple.

Examples:

  • Best threading queue example / best practice
  • Best method to get objects from a BlockingQueue in a concurrent program?

Leave a Comment