Thread pool

In computer programming, a thread pool pattern (also replicated workers or worker-crew model) consists of a number m of threads, created to perform a number n of tasks concurrently. Typically m is not equal to n; instead, the number of threads is tuned to the computing resources available to handle tasks in parallel (processors, cores, memory) while the number of tasks depends on the problem and may not be known upfront.

Reasons for using a thread pool, rather than the obvious alternative of spawning one thread per task, are to prevent the time and memory overhead inherent in thread creation, and to avoid running out of resources such as open files or network connections (of which operating systems allocate a limited number to running programs). A common way of distributing the tasks to threads (scheduling the tasks for execution) is by means of a synchronized queue known as a task queue. The threads in the pool take tasks off the queue, perform them, then return to the queue for their next task.