When to use thread pool in C#? [closed] When to use thread pool in C#? [closed] multithreading multithreading

When to use thread pool in C#? [closed]


I would suggest you use a thread pool in C# for the same reasons as any other language.

When you want to limit the number of threads running or don't want the overhead of creating and destroying them, use a thread pool.

By small tasks, the book you read means tasks with a short lifetime. If it takes ten seconds to create a thread which only runs for one second, that's one place where you should be using pools (ignore my actual figures, it's the ratio that counts).

Otherwise you spend the bulk of your time creating and destroying threads rather than simply doing the work they're intended to do.


If you have lots of logical tasks that require constant processing and you want that to be done in parallel use the pool+scheduler.

If you need to make your IO related tasks concurrently such as downloading stuff from remote servers or disk access, but need to do this say once every few minutes, then make your own threads and kill them once you're finished.

Edit: About some considerations, I use thread pools for database access, physics/simulation, AI(games), and for scripted tasks ran on virtual machines that process lots of user defined tasks.

Normally a pool consists of 2 threads per processor (so likely 4 nowadays), however you can set up the amount of threads you want, if you know how many you need.

Edit: The reason to make your own threads is because of context changes, (thats when threads need to swap in and out of the process, along with their memory). Having useless context changes, say when you aren't using your threads, just leaving them sit around as one might say, can easily half the performance of your program (say you have 3 sleeping threads and 2 active threads). Thus if those downloading threads are just waiting they're eating up tons of CPU and cooling down the cache for your real application


Here's a nice summary of the thread pool in .Net: http://blogs.msdn.com/pedram/archive/2007/08/05/dedicated-thread-or-a-threadpool-thread.aspx

The post also has some points on when you should not use the thread pool and start your own thread instead.