Principal
What is a Worker Pool? Have you used it?
sobes.tech AI
Answer from AI
Worker Pool is a design pattern used to manage a pool of threads (workers) that execute tasks from a shared queue. The idea is to limit the number of concurrently running threads and reuse them for processing multiple tasks, which increases performance and reduces overhead associated with thread creation and destruction.
In C++, this is often implemented through a class that contains a task queue and a fixed number of threads that constantly take tasks from the queue and execute them.
An example of a simplified Worker Pool implementation in C++ using std::thread and std::queue:
#include <iostream>
#include <thread>
#include <queue>
#include <mutex>
#include <condition_variable>
#include <functional>
#include <vector>
#include <atomic>
class WorkerPool {
std::vector<std::thread> workers;
std::queue<std::function<void()>> tasks;
std::mutex queue_mutex;
std::condition_variable condition;
std::atomic<bool> stop;
public:
WorkerPool(size_t threads) : stop(false) {
for (size_t i = 0; i < threads; ++i) {
workers.emplace_back([this] {
while (true) {
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();
}
});
}
}
void enqueue(std::function<void()> task) {
{
std::unique_lock<std::mutex> lock(queue_mutex);
tasks.push(std::move(task));
}
condition.notify_one();
}
~WorkerPool() {
stop = true;
condition.notify_all();
for (std::thread &worker : workers) {
if(worker.joinable())
worker.join();
}
}
};
int main() {
WorkerPool pool(4);
pool.enqueue([]{ std::cout << "Task 1" << std::endl; });
pool.enqueue([]{ std::cout << "Task 2" << std::endl; });
std::this_thread::sleep_for(std::chrono::seconds(1));
}
Yes, I have used it in projects for organizing parallel task processing with thread count control.