threadPool
Header file (declarations and definitions because of template functions) for the implementation of a generic thread pool
-
template<class jobtype>
class default_comp - #include <threadPool.h>
Default comparator for priority_queue in threadPool — no comparison.
First in first out, not sorting
-
template<class jobtype = int, class comparator = default_comp<jobtype>>
class threadPool - #include <threadPool.h>
Class for creating a pool of threads to asynchronously distribute work.
Template parameters:
jobtype defines a structure or class that represents a job or task
comparator defines how to compare jobs for sorting the list
Default options correspond to jobs being defined by an integer job_id, and no sorting of the list (first in first out)
Public Functions
-
inline explicit threadPool(std::size_t numThreads, std::function<void(int, jobtype)> work_fn)
Constructor — starts thread pool running.
-
inline ~threadPool()
Destructor — stops threads.
-
inline void enqueue(jobtype job_id)
Places jobs in queue to wait for scheduling.
job_id is sorted if a comparator is provided
-
inline int get_num_threads()
Get the number of threads being used by the thread pool.
-
inline int get_queue_length()
Get the current length of the job queue.
Private Functions
-
inline void start(std::size_t numThreads)
Starts thread pool — launches each thread, which continually check for work.
-
inline void stop() noexcept
Stops thread pool.
Waits for all threads to end and joins them
Finishes the thread pool before ending
-
inline explicit threadPool(std::size_t numThreads, std::function<void(int, jobtype)> work_fn)