Multithreading: introducing the thread pool
Until now, we’ve been starting threads by creating instances of the System.Threading.Thread class in the samples. Thread creation isn’t cheap! If we’re creating a thread just to run a small task, then the thread’s creation might completely outweigh the advantage of running such a small task.
In most cases, instead of creating new threads, we should simply run those tasks on one of the threads maintained on the thread pool that is automatically created for all managed processes. Even though the most used operation is queuing a task in the thread pool, you can also:
- use the pool to run a task when IO completes;
- run a (possible recurrent) task at a specific time;
- execute a task when a kernel object is signaled.
The global CLR thread pool maintains two “sub-pools”. One is used for IO and the other is used for the remaining possible options mentioned above. By default, there is one pool per process and the IO “sub-pool” will contain a maximum of 1000 threads, while the other “sub-pool” (aka worker pool) contains a maximum of 250 threads.
wow! wtf? so many threads? Yes, that’s the maximum number you’ll get per process.However, when the process starts, there aren’t really any threads on the pool )ie, both “sub-pools” are empty. Threads are created as needed (ie, while new task are queued on the thread pool) until the specified minimum number of threads is reached.
Notice that threads might not be immediately created for work items that are queued on the thread pool. When the minimum number of threads is reached, the thread pool will throttle the creation of other threads (meaning, it won’t immediately create new threads per task that is queued on the thread pool). In these cases, the thread pool will create a new thread as needed from 30 to 30 seconds if all the predefined minimum number of threads are all busy doing work.
When a thread ends its work, it returns to thread pool where it waits for more work. If the current number of waiting threads is greater than the minimum specified, then those additional threads will be terminated.
And that’s it for this post. We’ll be returning to this topic in the next posts.