Used to Pre-Allocate Worker Capacity. As part of the ThreadPool instance creation, User needs to specify 2 parameters:
- Desired Capacity: Number of threads to be created as part of the Pool.
- Max Pool Capacity: The size upto which the Thread Pool can scale, to accomodate growing demand.
- When a task is submitted (via the enqueueTask API), first we check if there any spare or free threads in the Pool, if there are we assign the task to one of those threads.
- However if no threads are currently free, we check if the pool can be expanded (by adding additional threads to accomodate the request).
- If even that is not possible, the request is dropped.
Any new threads create to scale up to increased demand shall be destroyed after some predefined interval of inactivity (i.e. scale down in response to decreased demand).
Internally the ThreadPool implementation makes use of Condition Variables. Initially when the pool is created, the threads put themselves to sleep by calling wait on this Condition Variable. Whenever a task comes in, one of these threads (considering there are threads available in the pool), will be woken up and it will pick up the new task.