The creation of a new thread is a complex operation that takes up a lot of resources. In the Call stacks section, we covered how memory is allocated for a new thread. When the lower block of a function is removed from a stack, the thread is destroyed. To avoid constantly creating new threads, we can use thread pools. There is no logic in creating a new thread for invoking each short-term operation, because this operation and switching the program flow to a created context can take more time than executing the task itself. The thread-pool pattern assumes a class that contains a set of threads that are waiting for a new task, and a queue that holds the tasks.
The following diagram shows how this works:
The preceding diagram shows that a pool contains a queue that holds tasks submitted by producers. The threads from the pool take tasks from the queue and execute them...