Thread
-
class Thread
A wrapper for
std::threadwith a name and controlled start/stop lifecycle.The
Threadclass encapsulates anstd::threadand 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.
-
template<typename TType>
class ThreadSafeQueue A thread-safe implementation of a templated queue.
The
ThreadSafeQueueclass 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.
-
class WorkerPool
A pool of worker threads that executes jobs.
The
WorkerPoolclass manages a collection of worker threads, which continuously pull jobs from a thread-safe queue and execute them. The jobs are submitted asIJobobjects, which contain astd::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
IJobinterface provides a way to define jobs that can be executed by worker threads.Subclassed by WorkerPool::Job
-
class Job : public WorkerPool::IJob
Represents a job to be executed by the worker pool.
The
Jobclass holds astd::function<void()>and encapsulates the job that will be executed by a worker thread. It implements theIJobinterface.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.
-
explicit Job(std::function<void()> func)
-
WorkerPool(size_t num_threads = std::thread::hardware_concurrency())
-
class PersistentWorker
Manages task names and assigns them to respective worker pools.
The
PersistentWorkerclass 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.
-
PersistentWorker() = default