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.
Loading...
Searching...
No Matches
AutonomyThread< T > Class Template Referenceabstract

Interface class used to easily multithread a child class. More...

#include <AutonomyThread.hpp>

Collaboration diagram for AutonomyThread< T >:

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.
 
IPSGetIPS ()
 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
 

Detailed Description

template<class T>
class AutonomyThread< T >

Interface class used to easily multithread a child class.

Template Parameters
T- Variable return type of internal pooled code.
Author
ClayJay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2023-07-27

Member Enumeration Documentation

◆ AutonomyThreadState

template<class T >
enum class AutonomyThread::AutonomyThreadState
strong
46 {
47 eStarting,
48 eRunning,
49 eStopping,
50 eStopped
51 };

Constructor & Destructor Documentation

◆ AutonomyThread()

template<class T >
AutonomyThread< T >::AutonomyThread ( )
inline

Construct a new Autonomy Thread object.

Author
clayjay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2023-12-30
64 {
65 // Initialize member variables.
66 m_bStopThreads = false;
67 m_eThreadState = AutonomyThreadState::eStopped;
68 m_nMainThreadMaxIterationPerSecond = 0;
69 }

◆ ~AutonomyThread()

template<class T >
virtual AutonomyThread< T >::~AutonomyThread ( )
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.

Author
ClayJay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2023-07-23
82 {
83 // Tell all threads to stop executing user code.
84 m_bStopThreads = true;
85 // Update thread state.
86 m_eThreadState = AutonomyThreadState::eStopping;
87
88 // Pause and clear pool queues.
89 m_thPool.pause();
90 m_thPool.purge();
91 m_thMainThread.pause();
92 m_thMainThread.purge();
93
94 // Wait for all pools to finish.
95 m_thPool.wait();
96 m_thMainThread.wait();
97 // Update thread state.
98 m_eThreadState = AutonomyThreadState::eStopped;
99 }
void wait()
Wait for tasks to be completed. Normally, this function waits for all tasks, both those that are curr...
Definition BS_thread_pool.hpp:761
void purge()
Purge all the tasks waiting in the queue. Tasks that are currently running will not be affected,...
Definition BS_thread_pool.hpp:444
Here is the call graph for this function:

Member Function Documentation

◆ Start()

template<class T >
void AutonomyThread< T >::Start ( )
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.

Note
This method will block until the thread state is eRunning.
Author
ClayJay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2023-07-22
118 {
119 // Tell any open thread to stop.
120 m_bStopThreads = true;
121 // Update thread state.
122 m_eThreadState = AutonomyThreadState::eStopping;
123
124 // Pause queuing of new tasks to the threads, then purge them.
125 m_thPool.pause();
126 m_thPool.purge();
127 m_thMainThread.pause();
128 m_thMainThread.purge();
129
130 // Wait for loop, pool and main thread to join.
131 this->Join();
132
133 // Update thread state.
134 m_eThreadState = AutonomyThreadState::eStarting;
135 // Clear results vector.
136 m_vPoolReturns.clear();
137 // Reset thread stop toggle.
138 m_bStopThreads = false;
139
140 // Submit single task to pool queue and store resulting future. Still using pool, as it's scheduling is more efficient.
141 std::future<void> fuMainReturn = m_thMainThread.submit_task([this]() { this->RunThread(m_bStopThreads); });
142
143 // Unpause pool queues.
144 m_thPool.unpause();
145 m_thMainThread.unpause();
146
147 // Block until thread is started or currently stopping if thread start failed.
148 std::unique_lock<std::mutex> lkStartLock(m_muThreadRunningConditionMutex);
149 m_cdThreadRunningCondition.wait(lkStartLock,
150 [this]
151 { return this->m_eThreadState == AutonomyThreadState::eRunning || this->m_eThreadState == AutonomyThreadState::eStopping; });
152 }
void Join()
Waits for thread to finish executing and then closes thread. This method will block the calling code ...
Definition AutonomyThread.hpp:180
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 u...
Definition AutonomyThread.hpp:571
std::future< R > submit_task(F &&task BS_THREAD_POOL_PRIORITY_INPUT)
Submit a function with no arguments into the task queue, with the specified priority....
Definition BS_thread_pool.hpp:605
Here is the call graph for this function:
Here is the caller graph for this function:

◆ RequestStop()

template<class T >
void AutonomyThread< T >::RequestStop ( )
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.

Author
ClayJay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2023-07-22
165 {
166 // Signal for any open threads to stop executing,
167 m_bStopThreads = true;
168 // Update thread state.
169 m_eThreadState = AutonomyThreadState::eStopping;
170 }
Here is the caller graph for this function:

◆ Join()

template<class T >
void AutonomyThread< T >::Join ( )
inline

Waits for thread to finish executing and then closes thread. This method will block the calling code until thread is finished.

Author
ClayJay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2023-07-22
181 {
182 // Wait for pool to finish all tasks.
183 m_thPool.wait();
184 // Wait for main thread to finish.
185 m_thMainThread.wait();
186
187 // Update thread state.
188 m_eThreadState = AutonomyThreadState::eStopped;
189 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Joinable()

template<class T >
bool AutonomyThread< T >::Joinable ( ) const
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.

Returns
true - The thread is finished and joinable.
false - The thread is still running code.
Author
ClayJay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2023-07-22
202 { // Check current number of running and queued tasks.
203 return (m_thMainThread.get_tasks_total() <= 0 && m_thPool.get_tasks_total() <= 0);
204 }
size_t get_tasks_total() const
Get the total number of unfinished tasks: either still waiting in the queue, or running in a thread....
Definition BS_thread_pool.hpp:388
Here is the call graph for this function:

◆ GetThreadState()

template<class T >
AutonomyThreadState AutonomyThread< T >::GetThreadState ( ) const
inline

Accessor for the Threads State private member.

Returns
AutonomyThreadState - The current state of the main thread.
Author
clayjay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2024-01-08
214{ return m_eThreadState; }

◆ GetIPS()

template<class T >
IPS & AutonomyThread< T >::GetIPS ( )
inline

Accessor for the Frame I P S private member.

Returns
IPS& - The iteration per second counter for the ThreadedContinuousCode()
Author
clayjay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2023-08-20
224{ return m_IPS; }
Here is the caller graph for this function:

◆ RunPool()

template<class T >
void AutonomyThread< T >::RunPool ( const unsigned int  nNumTasksToQueue,
const unsigned int  nNumThreads = 2,
const bool  bForceStopCurrentThreads = false 
)
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.

Parameters
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.
Author
ClayJay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2023-07-23
265 {
266 // Check if the pools need to be resized.
267 if (m_thPool.get_thread_count() != nNumThreads)
268 {
269 // Pause queuing of new tasks to the threads, then purge them.
270 m_thPool.pause();
271 m_thPool.purge();
272 // Wait for open threads to terminate, then resize the pool.
273 m_thPool.reset(nNumThreads);
274 // Unpause queue.
275 m_thPool.unpause();
276
277 // Clear results vector.
278 m_vPoolReturns.clear();
279 }
280 // Check if the current pool tasks should be stopped before queueing more tasks.
281 else if (bForceStopCurrentThreads)
282 {
283 // Pause queuing of new tasks to the threads, then purge them.
284 m_thPool.pause();
285 m_thPool.purge();
286 // Wait for threadpool to join.
287 m_thPool.wait();
288 // Unpause queue.
289 m_thPool.unpause();
290 }
291
292 // Loop nNumThreads times and queue tasks.
293 for (unsigned int i = 0; i < nNumTasksToQueue; ++i)
294 {
295 // Submit single task to pool queue.
296 m_vPoolReturns.emplace_back(m_thPool.submit_task(
297 [this]()
298 {
299 // Run user pool code without lock.
300 this->PooledLinearCode();
301 }));
302 }
303 }
void reset()
Reset the pool with the total number of hardware threads available, as reported by the implementation...
Definition BS_thread_pool.hpp:545
concurrency_t get_thread_count() const
Get the number of threads in the pool.
Definition BS_thread_pool.hpp:399
Here is the call graph for this function:

◆ RunDetachedPool()

template<class T >
void AutonomyThread< T >::RunDetachedPool ( const unsigned int  nNumTasksToQueue,
const unsigned int  nNumThreads = 2,
const bool  bForceStopCurrentThreads = false 
)
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.

Parameters
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.
Author
ClayJay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2023-07-23
337 {
338 // Check if the pools need to be resized.
339 if (m_thPool.get_thread_count() != nNumThreads)
340 {
341 // Pause queuing of new tasks to the threads, then purge them.
342 m_thPool.pause();
343 m_thPool.purge();
344 // Wait for open threads to terminate, then resize the pool.
345 m_thPool.reset(nNumThreads);
346 // Unpause queue.
347 m_thPool.unpause();
348
349 // Clear results vector.
350 m_vPoolReturns.clear();
351 }
352 // Check if the current pool tasks should be stopped before queueing more tasks.
353 else if (bForceStopCurrentThreads)
354 {
355 // Pause queuing of new tasks to the threads, then purge them.
356 m_thPool.pause();
357 m_thPool.purge();
358 // Wait for threadpool to join.
359 m_thPool.wait();
360 // Unpause queue.
361 m_thPool.unpause();
362 }
363
364 // Loop nNumThreads times and queue tasks.
365 for (unsigned int i = 0; i < nNumTasksToQueue; ++i)
366 {
367 // Push single task to pool queue. No return value no control.
368 m_thPool.detach_task(
369 [this]()
370 {
371 // Run user code without lock.
372 this->PooledLinearCode();
373 });
374 }
375 }
void detach_task(F &&task BS_THREAD_POOL_PRIORITY_INPUT)
Submit a function with no arguments and no return value into the task queue, with the specified prior...
Definition BS_thread_pool.hpp:459
Here is the call graph for this function:

◆ ParallelizeLoop()

template<class T >
template<typename N , typename F >
void AutonomyThread< T >::ParallelizeLoop ( const int  nNumThreads,
const N  tTotalIterations,
F &&  tLoopFunction 
)
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.

Template Parameters
N- Template argument for the nTotalIterations type.
F- Template argument for the given function reference.
Parameters
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
Author
ClayJay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2023-07-26
405 {
406 // Create new thread pool.
407 BS::thread_pool m_thLoopPool = BS::thread_pool(nNumThreads);
408
409 m_thLoopPool.detach_blocks(0,
410 tTotalIterations,
411 [&tLoopFunction](const int nStart, const int nEnd)
412 {
413 // Call loop function without lock.
414 tLoopFunction(nStart, nEnd);
415 });
416
417 // Wait for loop to finish.
418 m_thLoopPool.wait();
419 }
A fast, lightweight, and easy-to-use C++17 thread pool class.
Definition BS_thread_pool.hpp:289
void detach_blocks(const T first_index, const T index_after_last, F &&block, const size_t num_blocks=0 BS_THREAD_POOL_PRIORITY_INPUT)
Parallelize a loop by automatically splitting it into blocks and submitting each block separately to ...
Definition BS_thread_pool.hpp:480
Here is the call graph for this function:

◆ ClearPoolQueue()

template<class T >
void AutonomyThread< T >::ClearPoolQueue ( )
inlineprotected

Clears any tasks waiting to be ran in the queue, tasks currently running will remain running.

Author
ClayJay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2023-09-09
429{ m_thPool.purge(); }
Here is the call graph for this function:

◆ JoinPool()

template<class T >
void AutonomyThread< T >::JoinPool ( )
inlineprotected

Waits for pool to finish executing tasks. This method will block the calling code until thread is finished.

Author
ClayJay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2023-07-22
439{ m_thPool.wait(); }
Here is the call graph for this function:

◆ PoolJoinable()

template<class T >
bool AutonomyThread< T >::PoolJoinable ( ) const
inlineprotected

Check if the internal pool threads are done executing code and the queue is empty.

Returns
true - The thread is finished and joinable.
false - The thread is still running code.
Author
ClayJay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2023-07-22
452 {
453 // Check current number of running and queued tasks.
454 return (m_thPool.get_tasks_total() <= 0);
455 }
Here is the call graph for this function:

◆ SetMainThreadIPSLimit()

template<class T >
void AutonomyThread< T >::SetMainThreadIPSLimit ( int  nMaxIterationsPerSecond = 0)
inlineprotected

Mutator for the Main Thread Max I P S private member.

Parameters
nMaxIterationsPerSecond- The max iteration per second limit of the main thread.
Note
- Set to zero to disable the max iteration per second limit.
Author
clayjay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2023-12-30
468 {
469 // Assign member variable.
470 m_nMainThreadMaxIterationPerSecond = nMaxIterationsPerSecond;
471 }

◆ GetPoolNumOfThreads()

template<class T >
int AutonomyThread< T >::GetPoolNumOfThreads ( )
inlineprotected

Accessor for the Pool Num Of Threads private member.

Returns
int - The number of threads available to the pool.
Author
ClayJay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2023-09-09
481{ return m_thPool.get_thread_count(); }
Here is the call graph for this function:

◆ GetPoolQueueLength()

template<class T >
int AutonomyThread< T >::GetPoolQueueLength ( )
inlineprotected

Accessor for the Pool Queue Size private member.

Returns
int - The number of tasks queued for the pool.
Author
clayjay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2024-03-14
491{ return m_thPool.get_tasks_queued(); }
size_t get_tasks_queued() const
Get the number of tasks currently waiting in the queue to be executed by the threads.
Definition BS_thread_pool.hpp:366
Here is the call graph for this function:

◆ GetPoolResults()

template<class T >
std::vector< T > AutonomyThread< T >::GetPoolResults ( )
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.

Returns
std::vector<T> - A vector containing the returns from each thread that ran the PooledLinearCode.
Author
ClayJay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2023-07-26
506 {
507 // Create instance variable.
508 std::vector<T> vResults;
509
510 // Loop the pool futures and get result.
511 for (std::future<T> fResult : m_vPoolReturns)
512 {
513 // Store returned value.
514 vResults.emplace_back(fResult.get());
515 }
516
517 // Clear pool returns member variable.
518 m_vPoolReturns.clear();
519
520 return vResults;
521 }

◆ GetMainThreadMaxIPS()

template<class T >
int AutonomyThread< T >::GetMainThreadMaxIPS ( ) const
inlineprotected

Accessor for the Main Thread Max I P S private member.

Returns
int - The max iterations per second the main thread can reach.
Author
clayjay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2023-12-31
532 {
533 // Return member variable value.
534 return m_nMainThreadMaxIterationPerSecond;
535 }

◆ ThreadedContinuousCode()

template<class T >
virtual void AutonomyThread< T >::ThreadedContinuousCode ( )
privatepure virtual

◆ PooledLinearCode()

template<class T >
virtual T AutonomyThread< T >::PooledLinearCode ( )
privatepure virtual

◆ RunThread()

template<class T >
void AutonomyThread< T >::RunThread ( std::atomic_bool &  bStopThread)
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.

Parameters
bStopThread- Atomic shared variable that signals the thread to stop iterating.
Author
ClayJay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2023-07-24
572 {
573 // Declare instance variables.
574 std::chrono::_V2::system_clock::time_point tmStartTime;
575
576 // Loop until stop flag is set.
577 while (!bStopThread)
578 {
579 // Check if max IPS limit has been set.
580 if (m_nMainThreadMaxIterationPerSecond > 0)
581 {
582 // Get start execution time.
583 tmStartTime = std::chrono::high_resolution_clock::now();
584 }
585
586 // Call method containing user code.
587 this->ThreadedContinuousCode();
588
589 // Check if max IPS limit has been set.
590 if (m_nMainThreadMaxIterationPerSecond > 0)
591 {
592 // Get end execution time.
593 std::chrono::_V2::system_clock::time_point tmEndTime = std::chrono::high_resolution_clock::now();
594 // Get execution time of user code.
595 std::chrono::microseconds tmElapsedTime = std::chrono::duration_cast<std::chrono::microseconds>(tmEndTime - tmStartTime);
596 // Check if the elapsed time is slower than the max iterations per seconds.
597 if (tmElapsedTime.count() < (1.0 / m_nMainThreadMaxIterationPerSecond) * 1000000)
598 {
599 // Calculate the time to wait to stay under IPS cap.
600 int nSleepTime = ((1.0 / m_nMainThreadMaxIterationPerSecond) * 1000000) - tmElapsedTime.count();
601 // Make this thread sleep for the remaining time.
602 std::this_thread::sleep_for(std::chrono::microseconds(nSleepTime));
603 }
604 }
605
606 // Check if thread state needs to be updated.
607 if (m_eThreadState != AutonomyThreadState::eRunning && m_eThreadState != AutonomyThreadState::eStopping)
608 {
609 // Update thread state to running.
610 m_eThreadState = AutonomyThreadState::eRunning;
611 // Notify waiting start method that thread is now running.
612 m_cdThreadRunningCondition.notify_all();
613 }
614
615 // Call iteration per second tracking tick.
616 m_IPS.Tick();
617 }
618
619 // Notify waiting start method that thread is now stopping.
620 m_cdThreadRunningCondition.notify_all();
621 }
void Tick()
This method is used to update the iterations per second counter and recalculate all of the IPS metric...
Definition IPS.hpp:138
Here is the call graph for this function:
Here is the caller graph for this function:

The documentation for this class was generated from the following file: