Libftpp
A modern C++ library
WorkerPool Class Reference

Worker Pool for Concurrent Job Execution. More...

#include <worker_pool.hpp>

Classes

class  IJobs
 Interface for defining jobs to be executed by the worker pool. Classes implementing this interface must define the start() method. More...
 

Public Member Functions

 WorkerPool (const int &nbWorkers)
 
 ~WorkerPool ()
 
void loop ()
 
void addJob (const std::function< void()> &jobToExecute)
 
void addJob (IJobs &jobToExecute)
 

Detailed Description

Worker Pool for Concurrent Job Execution.

This class provides a pool of worker threads that can execute jobs concurrently. Jobs can be added to the pool, and worker threads will pick them up and execute them. The pool manages thread synchronization and job distribution automatically.

Note
Uses condition variables for efficient thread synchronization
Supports adding jobs as std::function<void()> or via IJobs interface
// Create a worker pool with 4 threads
WorkerPool pool(4);
// Add a simple job
pool.addJob([]() {
std::cout << "Hello from a worker thread!" << std::endl;
});
// Define a job via IJobs interface
class MyJob : public WorkerPool::IJobs {
public:
void start() override {
std::cout << "MyJob is running!" << std::endl;
}
};
MyJob job;
pool.addJob(job);
// The worker pool will automatically execute the jobs
Interface for defining jobs to be executed by the worker pool. Classes implementing this interface mu...
Definition: worker_pool.hpp:63
Worker Pool for Concurrent Job Execution.
Definition: worker_pool.hpp:44

Definition at line 43 of file worker_pool.hpp.

Constructor & Destructor Documentation

◆ WorkerPool()

WorkerPool::WorkerPool ( const int &  nbWorkers)

Definition at line 3 of file worker_pool.cpp.

References loop().

◆ ~WorkerPool()

WorkerPool::~WorkerPool ( )

Definition at line 32 of file worker_pool.cpp.

Member Function Documentation

◆ addJob() [1/2]

void WorkerPool::addJob ( const std::function< void()> &  jobToExecute)

Definition at line 54 of file worker_pool.cpp.

◆ addJob() [2/2]

void WorkerPool::addJob ( IJobs jobToExecute)

Definition at line 63 of file worker_pool.cpp.

References WorkerPool::IJobs::start().

◆ loop()

void WorkerPool::loop ( )

Definition at line 13 of file worker_pool.cpp.

Referenced by WorkerPool().