C# – When to use thread pool in C#?


I have been trying to learn multi-threaded programming in C# and I am confused about when it is best to use a thread pool vs. create my own threads. One book recommends using a thread pool for small tasks only (whatever that means), but I can't seem to find any real guidelines.

What are some pros and cons of thread pools vs creating my own threads? And what are some example use cases for each?

Best Answer

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.