Worker
-
class Worker
An asynchronous worker class.
Worker lifecycle
Instantiation: create worker object with a thread name, stay_alive flag, desired execution frequency,and logging verbosity.
Start(): Called once. Calls private run() function to begin thread execution.
Stop(): Called once. Ends thread execution; blocks until the thread has stopped. If param interrupted=false AND stay_alive is true, then the worker is restarted. By default, stay_alive=true. A stopped worker cannot be restarted, a new object must be created.
Init(): Called once when the worker is told to begin execution (after Start is called)
Execute(): Called periodically at a maximum rate of _exec_freq Hz until Stop() is called.
Finish(): Called once before the Worker is destroyed (before destructor).
Join(): Blocks until the thread object has finished.
run(): Private member function that handles Execute timing and monitors for flags to stop execution.
Subclassed by CalibrationCamWorker, LocalizationWorker, NTWorker, TDCamWorker, WebServerWorker
Public Functions
-
inline Worker()
Default constructor with placeholder threadname.
-
inline explicit Worker(const std::string &thread_name)
A worker with a specified thread name.
- Parameters:
thread_name – The thread name of the worker.
-
inline explicit Worker(const std::string &thread_name, bool stay_alive)
A worker with specified thread name and stay_alive flag.
- Parameters:
thread_name – The thread name of the worker.
stay_alive – Flag to restart worker when Stop(false) is called.
-
inline explicit Worker(const std::string &thread_name, bool stay_alive, double execution_freq)
A worker with specified thread name, stay_alive flag, and execution_frequency.
- Parameters:
thread_name – The thread name of the worker.
stay_alive – Flag to restart worker when Stop(false) is called.
execution_freq – The maximum execution frequency, in hertz.
-
inline explicit Worker(const std::string &thread_name, bool stay_alive, double execution_freq, AppLogger::SEVERITY debug_verbosity)
A worker with specified thread name, stay_alive flag, execution_frequency, and debug verbosity.
- Parameters:
thread_name – The thread name of the worker.
stay_alive – Flag to restart worker when Stop(false) is called.
execution_freq – The maximum execution frequency, in hertz.
debug_verbosity – The minimum verbosity of events to log.
-
inline Worker(const Worker &o)
Copy constructor, do not copy the thread object.
- Parameters:
o – The object to copy from.
-
inline ~Worker()
Ensure the thread has stopped execution, then delete the thread.
-
inline void SetStayAlive(bool stay_alive)
Change the stay_alive flag for the worker. Can be modified before or after a Worker has been started.
- Parameters:
stay_alive – true to keep the worker alive after Stop(false) is called, false to kill the worker instead.
-
inline bool GetStayAlive()
Get the value for the stay_alive flag of the worker. true keeps the worker alive after Stop(false) is called, false kills the worker instead.
- Returns:
The value of the stay_alive flag.
-
inline bool Stopped()
Check if this worker has stopped.
- Returns:
true if the worker has stopped, false otherwise.
-
inline const std::string &GetName()
Get the name of the worker.
- Returns:
The worker name.
-
inline void Start()
Start the worker.
-
inline bool Stop(bool interrupted = true)
Stop the worker. If the worker has the stay_alive flag set to true, then Stop(false) will simply call the Init() function again, then resume execution of the Execute() function. If the worker has the stay_alive flag set to false OR Stop() is called, the worker will finish its loop in the run() function, then call Finish().
- Parameters:
interrupted – If (true) OR (false AND stay_alive==false), tell the worker to end execution, then call Finished(). Otherwise the worker will end execution, call Init(), then resume execution.
- Returns:
If the worker will completely stop, return true. Otherwise return false.
-
inline bool IsFinished()
-
inline void Join()
Block until the worker finishes execution.
-
inline double GetExecutionFreq()
Get the actual execution frequency in hertz.
- Returns:
The actual execution frequency in hertz.
-
inline void SetExecutionFreq(double freq)
Set the desired execution frequency in hertz. Blocks until the frequency is set.
- Parameters:
freq – The desired execution frequency in hertz.
Protected Functions
-
inline virtual void Execute()
The user-defined Execute function runs once per execution_freq. The execute function should never block. No timeouts are implemented to detect loop overrun.
-
inline virtual void Init()
The user-defined Init function runs once, right after Start() is called. This function should never block. No timeouts are implemented to detect loop overrun.
-
inline virtual void Finish()
The user-defined Finish function runs once, after Stop(true) is called OR (Stop(false) AND stay_alive == false). This function should never block. No timeouts are implemented to detect loop overrun.
Protected Attributes
-
std::thread *_t_worker = {}
Private Functions
-
inline void run()
The function that the worker’s thread executes. First, run Init(), then run Execute() function at a maximum rate of _exec_freq. No timeout if execution cannot match desired frequency. Periodically check flags to determine if thread should stop. If thread should stop, run the Finish() function, then exit.
Private Members
-
bool _stop = false
-
std::binary_semaphore _stop_sem = {1}
-
bool _stay_alive = true
-
std::binary_semaphore _stay_alive_sem = {1}
-
bool _interrupted = false
-
std::binary_semaphore _interrupted_sem = {1}
-
double _exec_freq = 100.0
-
bool _is_finished = false
-
std::binary_semaphore _is_finished_sem = {1}
-
double _measure_exec_freq = {}
-
std::binary_semaphore _exec_freq_sem = {1}
-
std::string _thread_name
-
int _debug_v = AppLogger::SEVERITY::INFO
-
inline Worker()