1.0 Concept
Fig 1. ThreadPool Diagram
This ThreadPool consists of a thread-safe blocking queue that accepts workitems (callable objects) and a
small set of threads that dequeue and execute workitems as they become available.
The Task class wraps a static ThreadPool instance and sends workitems to the thread-pool for execution.
The ThreadPool instance is static so that only a specified number of threads process the workitems
no matter how many Tasks are declared.
2.0 Operation
Each thread runs a processing function that defines a loop dequeing and executing workitems. The
loop only terminates if the workitem execution returns false, e.g., doesn't continue.
3.0 Design
The workitem type and number of threads are specified with template parameters. Often the ThreadPool
doesn't need to return results, e.g., a socket listener dispatches a client handler to the ThreadPool
for each connection and has no other interaction with it. If a design does need a result, that can be
effected by supplying each workitem with a reference to a future.
4.0 Implementation
The ThreadPool and Task classes are relatively small, require only C++11, and have been used for
a while with no problems reported.
5.0 Build
The code was built with Visual Studio Community Edition - 2019, and tested on Windows 10.
6.0 Status
I plan to make a wrapper for a workitem and future, that will make it easier to use instances of the
Task class for work that returns a result.