Worker¶
Worker thread management and job scheduling.
Overview¶
Typical use cases:
Managing worker threads for background tasks.
Scheduling jobs with different priorities.
Header¶
<RaeptorCogs/Worker.hpp>
Metadata¶
- Author
Estorc
- Version
v1.0
- Copyright
Copyright (c) 2025 Estorc MIT License.
Enums¶
Enum |
Description |
|---|---|
|
Enum class for job priority levels. |
-
enum class RaeptorCogs::JobPriority : int¶
Enum class for job priority levels.
Used to specify the priority of jobs added to the worker.
Values:
-
enumerator LOWEST¶
Lowest priority level for jobs.
-
enumerator NORMAL¶
Normal priority level for jobs.
-
enumerator HIGHEST¶
Highest priority level for jobs.
-
enumerator LOWEST¶
Classes¶
Class |
Description |
|---|---|
|
MainWorker singleton class. |
|
Worker class for managing a background thread and job scheduling. |
-
class MainWorker¶
MainWorker singleton class.
Manages jobs that need to be executed on the main thread.
Public Functions
-
~MainWorker() = default¶
Destructor for the MainWorker singleton.
-
void addJob(const std::function<void()> &job, int priority)¶
Add a job to the main worker.
Note
Jobs with higher priority values are executed first.
- Parameters:
job – The job function to add.
priority – The priority level of the job (default is NORMAL).
-
void addJob(const std::function<void()> &job, JobPriority priority = JobPriority::NORMAL)¶
Overload of addJob to accept JobPriority enum.
Note
Jobs with higher priority values are executed first.
- Parameters:
job – The job function to add.
priority – The priority level of the job as JobPriority enum (default is NORMAL).
-
void executeJobs()¶
Execute all pending jobs in the main worker.
Note
Jobs are executed in order of priority.
-
void clearJobs()¶
Clear all pending jobs in the main worker.
Note
This removes all jobs without executing them.
Private Functions
-
inline MainWorker()¶
Private constructor for the MainWorker singleton.
Ensures that the MainWorker can only be instantiated through the SingletonAccessor.
Private Members
-
std::mutex mtx¶
Mutex for synchronizing access to the job queue.
Ensures thread-safe operations when adding or executing jobs.
-
std::map<int, std::vector<std::function<void()>>, std::greater<int>> jobs¶
Map of job queues categorized by priority.
Each priority level maps to a vector of job functions.
- friend SingletonAccessor< MainWorker >
-
~MainWorker() = default¶
-
class Worker¶
Worker class for managing a background thread and job scheduling.
Provides functionality to start/stop a worker thread, add jobs with priorities, and manage job execution.
Public Functions
-
void start()¶
Start the worker thread.
Initializes and begins execution of the worker thread.
-
void stop()¶
Stop the worker thread.
Signals the worker thread to terminate and waits for it to finish.
-
bool isRunning() const¶
Check if the worker thread is running.
- Returns:
true if the worker is running, false otherwise.
-
void addJob(const std::function<void()> &job, int priority)¶
Add a job to the worker.
Note
Jobs with higher priority values are executed first.
- Parameters:
job – The job function to add.
priority – The priority level of the job (default is NORMAL).
-
void addJob(const std::function<void()> &job, JobPriority priority = JobPriority::NORMAL)¶
Overload of addJob to accept JobPriority enum.
Note
Jobs with higher priority values are executed first.
- Parameters:
job – The job function to add.
priority – The priority level of the job as JobPriority enum.
-
void clearJobs()¶
Clear all pending jobs in the worker.
Note
This removes all jobs without executing them.
Private Functions
-
void run()¶
Main loop function for the worker thread.
Continuously executes jobs while the worker is running.
Private Members
-
std::thread workerThread¶
Thread object for the worker.
Manages the execution of jobs in a separate thread.
-
std::mutex mtx¶
Mutex for synchronizing access to the job queue.
Ensures thread-safe operations when adding or executing jobs.
-
std::map<int, std::vector<std::function<void()>>, std::greater<int>> jobs¶
Map of job queues categorized by priority.
Each priority level maps to a vector of job functions.
-
bool running = false¶
Flag indicating whether the worker is running.
Used to control the main loop of the worker thread.
-
void start()¶