Thread

class Thread

A wrapper for std::thread with a name and controlled start/stop lifecycle.

The Thread class encapsulates an std::thread and provides additional functionality:

  • Named threads for debugging purposes.

  • Explicit start and stop control for better thread lifecycle management.

Public Functions

Thread(const std::string &name, std::function<void()> func_to_execute)

Constructs a named thread with a function to execute.

Parameters:
  • name – The name of the thread.

  • func_to_execute – The function to execute when the thread starts.

void start()

Starts the thread, executing the provided function.

Throws:

std::runtime_error – if the thread has already started.

void stop()

Stops the thread and ensures proper cleanup.

Throws:

std::runtime_error – if the thread is not running.

~Thread()

Destructor to ensure the thread is joined before destruction.

Private Members

std::string thread_name_

Name of the thread for identification.

std::function<void()> func_

The function to execute in the thread.

std::thread thread_

Internal std::thread instance.

bool is_running_

Tracks the running state of the thread.

template<typename TType>
class ThreadSafeQueue

A thread-safe implementation of a templated queue.

The ThreadSafeQueue class ensures safe access and modification of a queue shared across multiple threads using mutex locks.

Template Parameters:

TType – The type of elements stored in the queue.

Public Functions

inline void push_back(const TType &new_element)

Adds an element to the end of the queue.

Parameters:

new_element – The element to be added.

inline void push_front(const TType &new_element)

Adds an element to the front of the queue.

Parameters:

new_element – The element to be added.

inline TType pop_back()

Removes and returns the last element of the queue.

Throws:

std::runtime_error – if the queue is empty.

Returns:

The last element of the queue.

inline TType pop_front()

Removes and returns the first element of the queue.

Throws:

std::runtime_error – if the queue is empty.

Returns:

The first element of the queue.

Private Members

std::deque<TType> queue_

Underlying deque to store elements.

std::mutex mutex_

Mutex for thread-safe access to the queue.

class WorkerPool

A pool of worker threads that executes jobs.

The WorkerPool class manages a collection of worker threads, which continuously pull jobs from a thread-safe queue and execute them. The jobs are submitted as IJob objects, which contain a std::function<void()> to be executed by the threads.

Public Functions

WorkerPool(size_t num_threads = std::thread::hardware_concurrency())

Constructs a WorkerPool with a specified number of threads.

Parameters:

num_threads – The number of worker threads in the pool.

~WorkerPool()

Destructor that stops all threads and joins them.

void addJob(const std::function<void()> &job_to_execute)

Adds a job to the pool for execution.

Parameters:

job_to_execute – The job (function) to execute.

Private Functions

void _workerLoop()

The worker thread loop that continuously processes jobs from the queue.

Private Members

ThreadSafeQueue<std::unique_ptr<IJob>> job_queue_

Queue of jobs to execute.

std::vector<std::thread> workers_

Worker threads.

std::atomic<bool> stop_pool_

Flag to stop the pool and workers.

interface IJob

Interface for a job that can be executed by a worker pool.

The IJob interface provides a way to define jobs that can be executed by worker threads.

Subclassed by WorkerPool::Job

Public Functions

virtual ~IJob() = default
virtual void execute() const = 0

Executes the job’s function.

class Job : public WorkerPool::IJob

Represents a job to be executed by the worker pool.

The Job class holds a std::function<void()> and encapsulates the job that will be executed by a worker thread. It implements the IJob interface.

Public Functions

explicit Job(std::function<void()> func)

Constructs a Job with a specific function.

Parameters:

func – The function to be executed.

virtual void execute() const override

Executes the job’s function.

Private Members

std::function<void()> func_

The function to execute.

class PersistentWorker

Manages task names and assigns them to respective worker pools.

The PersistentWorker class maps each task name to its own worker pool, allowing tasks to be added or removed dynamically.

Public Functions

PersistentWorker() = default

Constructor for PersistentWorker.

~PersistentWorker() = default

Destructor for PersistentWorker.

Ensures that all tasks are cleaned up (although tasks are managed by WorkerPool).

void addTask(const std::string &name, const std::function<void()> &jobToExecute)

Adds a new task to the worker’s task pool.

Parameters:
  • name – The unique name of the task.

  • jobToExecute – The function representing the task to be executed.

void removeTask(const std::string &name)

Removes a task from the worker’s task pool.

Parameters:

name – The unique name of the task to remove.

Throws:

std::invalid_argument – if the task is not founded.

Private Members

std::unordered_map<std::string, WorkerPool> task_queues_

Mapping of task names to worker pools.