SimpleThreadPool is a C++ header-only simple library that provides a basic implementation of a thread pool. It allows you to easily distribute tasks across multiple threads for concurrent execution.
To use SimpleThreadPool in your project, follow these steps:
-
Copy the
SimpleThreadPool.h
file into your project directory. -
Include the
SimpleThreadPool.h
header in your source file where you want to use the thread pool:#include "SimpleThreadPool.h"
-
Create an instance of the
SimpleThreadPool
class by specifying the desired number of threads in the thread pool:SimpleThreadPool threadPool(threadCount);
-
Define a task that you want to execute concurrently. The task can be any callable object (e.g., function, lambda, functor). Make sure the task returns a value.
-
Submit the task to the thread pool using the
Post
member function:auto future = threadPool.Post(task);
The
Post
function returns astd::future
object that can be used to retrieve the result of the task. -
Wait for the tasks to complete and retrieve the results using the
get
function of thestd::future
objects. -
After using the thread pool, call the
Destroy
member function to stop all threads and wait for them to finish their current tasks:threadPool.Destroy();