Thread Pool
A thread pool is a software design pattern that helps programmers achieve concurrency in an optimal way. By maintaining a pool of threads ready to execute at any moment performance is increased and latency is minimized by keeping a set of threads always resident in a pool; at the ready, instead of frequently starting them up and destroying them because of frequent brief tasks. Thus we avoid short lived threads which break concurrency speedup.
All this because creating and destroying threads takes a longer time than keeping them constantly alive and waiting.
Design
- there is an array of threads -
m_pool
- there is a FIFO queue of tasks (a
Task
should be a wrapper for a Callable object) -m_tasks
. Tasks areenqueue()
ed into the task queue - all...