![]() |
Autonomy Software C++ 24.5.1
Welcome to the Autonomy Software repository of the Mars Rover Design Team (MRDT) at Missouri University of Science and Technology (Missouri S&T)! API reference contains the source code and other resources for the development of the autonomy software for our Mars rover. The Autonomy Software project aims to compete in the University Rover Challenge (URC) by demonstrating advanced autonomous capabilities and robust navigation algorithms.
|
Interface class used to easily multithread a child class. More...
#include <AutonomyThread.hpp>
Public Types | |
enum class | AutonomyThreadState { eStarting , eRunning , eStopping , eStopped } |
Public Member Functions | |
AutonomyThread () | |
Construct a new Autonomy Thread object. | |
virtual | ~AutonomyThread () |
Destroy the Autonomy Thread object. If the parent object or main thread is destroyed or exited while this thread is still running, a race condition will occur. Stopping and joining the thread here insures that the main program can't exit if the user forgot to stop and join the thread. | |
void | Start () |
When this method is called, it starts a new thread that runs the code within the ThreadedContinuousCode method. This is the users main code that will run the important and continuous code for the class. | |
void | RequestStop () |
Signals threads to stop executing user code, terminate. DOES NOT JOIN. This method will not force the thread to exit, if the user code is not written properly and contains WHILE statement or any other long-executing or blocking code, then the thread will not exit until the next iteration. | |
void | Join () |
Waits for thread to finish executing and then closes thread. This method will block the calling code until thread is finished. | |
bool | Joinable () const |
Check if the code within the thread and all pools created by it are finished executing and the thread is ready to be closed. | |
AutonomyThreadState | GetThreadState () const |
Accessor for the Threads State private member. | |
IPS & | GetIPS () |
Accessor for the Frame I P S private member. | |
Protected Member Functions | |
void | RunPool (const unsigned int nNumTasksToQueue, const unsigned int nNumThreads=2, const bool bForceStopCurrentThreads=false) |
When this method is called, it starts/adds tasks to a thread pool that runs nNumTasksToQueue copies of the code within the PooledLinearCode() method using nNumThreads number of threads. This is meant to be used as an internal utility of the child class to further improve parallelization. Default value for nNumThreads is 2. | |
void | RunDetachedPool (const unsigned int nNumTasksToQueue, const unsigned int nNumThreads=2, const bool bForceStopCurrentThreads=false) |
When this method is called, it starts a thread pool full of threads that don't return std::futures (like a placeholder for the thread return type). This means the thread will not have a return type and there is no way to determine if the thread has finished other than calling the Join() method. Only use this if you want to 'set and forget'. It will be faster as it doesn't return futures. Runs PooledLinearCode() method code. This is meant to be used as an internal utility of the child class to further improve parallelization. | |
template<typename N , typename F > | |
void | ParallelizeLoop (const int nNumThreads, const N tTotalIterations, F &&tLoopFunction) |
Given a ref-qualified looping function and an arbitrary number of iterations, this method will divide up the loop and run each section in a thread pool. This function must not return anything. This method will block until the loop has completed. | |
void | ClearPoolQueue () |
Clears any tasks waiting to be ran in the queue, tasks currently running will remain running. | |
void | JoinPool () |
Waits for pool to finish executing tasks. This method will block the calling code until thread is finished. | |
bool | PoolJoinable () const |
Check if the internal pool threads are done executing code and the queue is empty. | |
void | SetMainThreadIPSLimit (int nMaxIterationsPerSecond=0) |
Mutator for the Main Thread Max I P S private member. | |
int | GetPoolNumOfThreads () |
Accessor for the Pool Num Of Threads private member. | |
int | GetPoolQueueLength () |
Accessor for the Pool Queue Size private member. | |
std::vector< T > | GetPoolResults () |
Accessor for the Pool Results private member. The action of getting results will destroy and remove them from this object. This method blocks if the thread is not finished, so no need to call JoinPool() before getting results. | |
int | GetMainThreadMaxIPS () const |
Accessor for the Main Thread Max I P S private member. | |
Protected Attributes | |
IPS | m_IPS = IPS() |
Private Member Functions | |
virtual void | ThreadedContinuousCode ()=0 |
virtual T | PooledLinearCode ()=0 |
void | RunThread (std::atomic_bool &bStopThread) |
This method is ran in a separate thread. It is a middleware between the class member thread and the user code that handles graceful stopping of user code. This method is intentionally designed to not return anything. | |
Private Attributes | |
BS::thread_pool | m_thMainThread = BS::thread_pool(1) |
BS::thread_pool | m_thPool = BS::thread_pool(2) |
std::vector< std::future< T > > | m_vPoolReturns |
std::atomic_bool | m_bStopThreads |
std::atomic< AutonomyThreadState > | m_eThreadState |
std::mutex | m_muThreadRunningConditionMutex |
std::condition_variable | m_cdThreadRunningCondition |
int | m_nMainThreadMaxIterationPerSecond |
Interface class used to easily multithread a child class.
T | - Variable return type of internal pooled code. |
|
strong |
|
inline |
Construct a new Autonomy Thread object.
|
inlinevirtual |
Destroy the Autonomy Thread object. If the parent object or main thread is destroyed or exited while this thread is still running, a race condition will occur. Stopping and joining the thread here insures that the main program can't exit if the user forgot to stop and join the thread.
|
inline |
When this method is called, it starts a new thread that runs the code within the ThreadedContinuousCode method. This is the users main code that will run the important and continuous code for the class.
If this method is called directly after itself, RunPool(), or RunDetachedPool(), it will signal for those threads to stop and wait until they exit on their next iteration. Any number of tasks that are still queued will be cleared. Old results will be destroyed. If you want to wait until they fully execute their code, then call the Join() method before starting a new thread.
|
inline |
Signals threads to stop executing user code, terminate. DOES NOT JOIN. This method will not force the thread to exit, if the user code is not written properly and contains WHILE statement or any other long-executing or blocking code, then the thread will not exit until the next iteration.
|
inline |
Waits for thread to finish executing and then closes thread. This method will block the calling code until thread is finished.
|
inline |
Check if the code within the thread and all pools created by it are finished executing and the thread is ready to be closed.
|
inline |
Accessor for the Threads State private member.
|
inline |
Accessor for the Frame I P S private member.
|
inlineprotected |
When this method is called, it starts/adds tasks to a thread pool that runs nNumTasksToQueue copies of the code within the PooledLinearCode() method using nNumThreads number of threads. This is meant to be used as an internal utility of the child class to further improve parallelization. Default value for nNumThreads is 2.
If this method is called directly after itself or RunDetachedPool(), it will just add more tasks to the queue. If the bForceStopCurrentThreads is enabled, it will signal for those threads to stop and wait until they exit on their next iteration. Any number of tasks that are still queued will be cleared. Old results will be destroyed. If you want to wait until they fully execute their code, then call the Join() method before this one.
Once the pool is created it stays alive for as long as the program runs or until a different threading method is called. So there's no overhead with starting and stopping threads or queueing more tasks.
YOU MUST HANDLE MUTEX LOCKS AND ATOMICS. It is impossible for this class to handle locks as all possible solutions lead to a solution that only lets one thread run at a time, essentially canceling out the parallelism.
nNumTasksToQueue | - The number of tasks running PooledLinearCode() to queue. |
nNumThreads | - The number of threads to run user code in. |
bForceStopCurrentThreads | - Clears the current tasks queue then signals and waits for existing tasks to stop before queueing more. |
|
inlineprotected |
When this method is called, it starts a thread pool full of threads that don't return std::futures (like a placeholder for the thread return type). This means the thread will not have a return type and there is no way to determine if the thread has finished other than calling the Join() method. Only use this if you want to 'set and forget'. It will be faster as it doesn't return futures. Runs PooledLinearCode() method code. This is meant to be used as an internal utility of the child class to further improve parallelization.
If this method is called directly after itself or RunPool(), it will just add more tasks to the queue. If the bForceStopCurrentThreads is enabled, it will signal for those threads to stop and wait until they exit on their next iteration. Any number of tasks that are still queued will be cleared. Old results will be destroyed. If you want to wait until they fully execute their code, then call the Join() method before this one.
Once the pool is created it stays alive for as long as the program runs or until a different threading method is called. So there's no overhead with starting and stopping threads or queueing more tasks.
YOU MUST HANDLE MUTEX LOCKS AND ATOMICS. It is impossible for this class to handle locks as all possible solutions lead to a solution that only lets one thread run at a time, essentially canceling out the parallelism.
nNumTasksToQueue | - The number of tasks running PooledLinearCode() to queue. |
nNumThreads | - The number of threads to run user code in. |
bForceStopCurrentThreads | - Clears the current tasks queue then signals and waits for existing tasks to stop before queueing more. |
|
inlineprotected |
Given a ref-qualified looping function and an arbitrary number of iterations, this method will divide up the loop and run each section in a thread pool. This function must not return anything. This method will block until the loop has completed.
To see an example of how to use this function, check out ArucoGenerateTags in the threads example folder.
YOU MUST HANDLE MUTEX LOCKS AND ATOMICS. It is impossible for this class to handle locks as all possible solutions lead to a solution that only lets one thread run at a time, essentially canceling out the parallelism.
N | - Template argument for the nTotalIterations type. |
F | - Template argument for the given function reference. |
nNumThreads | - The number of threads to use for the thread pool. |
nTotalIterations | - The total iterations to loop for. |
tLoopFunction | - Ref-qualified function to run. MUST ACCEPT TWO ARGS: const int a, const int b. a - loop start b - loop end |
|
inlineprotected |
Clears any tasks waiting to be ran in the queue, tasks currently running will remain running.
|
inlineprotected |
Waits for pool to finish executing tasks. This method will block the calling code until thread is finished.
|
inlineprotected |
Check if the internal pool threads are done executing code and the queue is empty.
|
inlineprotected |
Mutator for the Main Thread Max I P S private member.
nMaxIterationsPerSecond | - The max iteration per second limit of the main thread. |
|
inlineprotected |
Accessor for the Pool Num Of Threads private member.
|
inlineprotected |
Accessor for the Pool Queue Size private member.
|
inlineprotected |
Accessor for the Pool Results private member. The action of getting results will destroy and remove them from this object. This method blocks if the thread is not finished, so no need to call JoinPool() before getting results.
|
inlineprotected |
Accessor for the Main Thread Max I P S private member.
|
privatepure virtual |
Implemented in BasicCamera, ArucoGenerateTagsThreaded, PrimeCalculatorThread, RecordingHandler, StateMachineHandler, TagDetector, BasicCam, SIMBasicCam, SIMZEDCam, ZEDCam, and ObjectDetector.
|
privatepure virtual |
Implemented in BasicCamera, ArucoGenerateTagsThreaded, PrimeCalculatorThread, RecordingHandler, StateMachineHandler, TagDetector, BasicCam, SIMBasicCam, SIMZEDCam, ZEDCam, and ObjectDetector.
|
inlineprivate |
This method is ran in a separate thread. It is a middleware between the class member thread and the user code that handles graceful stopping of user code. This method is intentionally designed to not return anything.
bStopThread | - Atomic shared variable that signals the thread to stop iterating. |