The goal of this tutorial is to introduce you to the concept of thread pools and how to implement them in C++. By the end of this tutorial, you will be able to create a simple thread pool in C++.
Thread pools are a mechanism that allows you to manage multiple threads in your applications. Using thread pools, you can control the number of threads used by your program and reuse these threads, which can improve your program's performance and system resource usage.
The thread pool will be implemented as a class with two main functionalities: adding new tasks to the pool and stopping the pool.
#include <vector>
#include <queue>
#include <thread>
#include <mutex>
#include <condition_variable>
class ThreadPool {
private:
std::vector<std::thread> workers; // Vector to hold worker threads
std::queue<std::function<void()>> tasks; // Task queue
std::mutex queue_mutex; // Mutex for thread safety
std::condition_variable condition; // Condition variable for task waiting
bool stop; // Bool to indicate stop
public:
// Constructor
ThreadPool(size_t threads): stop(false) {
for(size_t i = 0; i<threads; ++i)
workers.emplace_back(
[this] {
for(;;) {
std::function<void()> task;
{
std::unique_lock<std::mutex> lock(this->queue_mutex);
this->condition.wait(lock,
[this]{ return this->stop || !this->tasks.empty(); });
if(this->stop && this->tasks.empty())
return;
task = std::move(this->tasks.front());
this->tasks.pop();
}
task();
}
}
);
}
// Add new task to the pool
template<class F>
void enqueue(F&& f) {
{
std::unique_lock<std::mutex> lock(queue_mutex);
// don't allow enqueueing after stopping the pool
if(stop)
throw std::runtime_error("enqueue on stopped ThreadPool");
tasks.emplace(std::forward<F>(f));
}
condition.notify_one();
}
// Destructor
~ThreadPool() {
{
std::unique_lock<std::mutex> lock(queue_mutex);
stop = true;
}
condition.notify_all();
for(std::thread &worker: workers)
worker.join();
}
};
In this code, we create a class ThreadPool
that manages all the threads. The constructor creates the specified number of worker threads and continuously pops tasks from the task queue to be executed by the threads. ThreadPool::enqueue()
is used to add tasks to the queue. The destructor stops all threads and waits for them to finish.
In this tutorial, we learned about thread pools, their importance, and how to implement them in C++. We also implemented a simple thread pool class in C++ that can add tasks to the pool and stop the pool.
Exercise 1: Modify the ThreadPool
class to allow for dynamic resizing of the pool.
Exercise 2: Implement a priority queue in the ThreadPool
class where tasks with higher priority will be executed first.
Exercise 3: Add error handling to the ThreadPool
class to deal with possible exceptions.
To gain a deeper understanding of thread pools, try to implement more complex scenarios such as:
- A thread pool with a dynamic number of threads.
- A thread pool that can prioritize certain tasks over others.
- Error handling in thread pools.
Remember, the key to learning is practicing. Happy coding!