RoveComm C++ 3.0.0
Welcome to the RoveComm C++ 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 RoveComm C++. The RoveComm C++ project is the RoveComm interface for high level systems and should not be used for microcontrollers.
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 }
enum class  AutonomyThreadPriority {
  eLowest = BS::pr::lowest , eLow = BS::pr::low , eNormal = BS::pr::normal , eHigh = BS::pr::high ,
  eHighest = BS::pr::highest
}

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.
void SetMainThreadPriority (AutonomyThreadPriority ePriority)
 Set the OS priority for the main continuous thread.
void SetPoolThreadPriority (AutonomyThreadPriority ePriority)
 Set the OS priority for the highly parallelized pool threads.

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::pr m_eMainThreadPriority = BS::pr::normal
BS::pr m_ePoolThreadPriority = BS::pr::normal
BS::thread_pool< BS::tp::pause > m_thMainThread = BS::thread_pool<BS::tp::pause>(1)
BS::thread_pool< BS::tp::pause > m_thPool = BS::thread_pool<BS::tp::pause>(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

◆ AutonomyThreadPriority

template<class T>
enum class AutonomyThread::AutonomyThreadPriority
strong
Enumerator
eLowest 
eLow 
eNormal 
eHigh 
eHighest 
56 {
57 eLowest = BS::pr::lowest, // Thread will be scheduled less often and may not be able to reach the max IPS if the system is under heavy load.
58 eLow = BS::pr::low, // Thread will be scheduled less often and may not be able to reach the max IPS if the system is under heavy load.
59 eNormal = BS::pr::normal, // Default. Thread will be scheduled normally and should be able to reach the max IPS if the system is not under heavy load.
60 eHigh = BS::pr::high, // Thread will be scheduled more often and should be able to reach the max IPS even if the system is under
61 // heavy load, but it may starve other threads of CPU time.
62 eHighest = BS::pr::highest // Thread will be scheduled as often as possible and should be able to reach the max IPS even if the system is under heavy load.
63 };
@ lowest
Definition BS_thread_pool.hpp:399
@ high
Definition BS_thread_pool.hpp:402
@ highest
Definition BS_thread_pool.hpp:403
@ low
Definition BS_thread_pool.hpp:400
@ normal
Definition BS_thread_pool.hpp:401

◆ AutonomyThreadState

template<class T>
enum class AutonomyThread::AutonomyThreadState
strong
Enumerator
eStarting 
eRunning 
eStopping 
eStopped 
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
76 {
77 // Initialize member variables.
78 m_bStopThreads = false;
81 }
@ eStopped
Definition AutonomyThread.hpp:50
std::atomic_bool m_bStopThreads
Definition AutonomyThread.hpp:598
std::atomic< AutonomyThreadState > m_eThreadState
Definition AutonomyThread.hpp:599
int m_nMainThreadMaxIterationPerSecond
Definition AutonomyThread.hpp:602

◆ ~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
94 {
95 // Tell all threads to stop executing user code.
96 m_bStopThreads = true;
97 // Update thread state.
99
100 // Pause and clear pool queues.
101 m_thPool.pause();
102 m_thPool.purge();
103 m_thMainThread.pause();
104 m_thMainThread.purge();
105
106 // Wait for all pools to finish.
107 m_thPool.wait();
108 m_thMainThread.wait();
109 // Update thread state.
111 }
BS::thread_pool< BS::tp::pause > m_thPool
Definition AutonomyThread.hpp:595
@ eStopping
Definition AutonomyThread.hpp:49
BS::thread_pool< BS::tp::pause > m_thMainThread
Definition AutonomyThread.hpp:594

Member Function Documentation

◆ 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
477{ m_thPool.purge(); }

◆ 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
242{ return m_IPS; }
IPS m_IPS
Definition AutonomyThread.hpp:278

◆ 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
580 {
581 // Return member variable value.
583 }

◆ 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
529{ return m_thPool.get_thread_count(); }

◆ 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
539{ return m_thPool.get_tasks_queued(); }

◆ 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
554 {
555 // Create instance variable.
557
558 // Loop the pool futures and get result.
560 {
561 // Store returned value.
562 vResults.emplace_back(fResult.get());
563 }
564
565 // Clear pool returns member variable.
566 m_vPoolReturns.clear();
567
568 return vResults;
569 }
Interface class used to easily multithread a child class.
Definition AutonomyThread.hpp:38
std::vector< std::future< T > > m_vPoolReturns
Definition AutonomyThread.hpp:597

◆ 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
232{ return m_eThreadState; }

◆ 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
199 {
200 // Wait for pool to finish all tasks.
201 m_thPool.wait();
202 // Wait for main thread to finish.
203 m_thMainThread.wait();
204
205 // Update thread state.
207 }
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
220 { // Check current number of running and queued tasks.
221 return (m_thMainThread.get_tasks_total() <= 0 && m_thPool.get_tasks_total() <= 0);
222 }

◆ 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
487{ m_thPool.wait(); }

◆ 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
453 {
454 // Create new thread pool (no specific template flags needed for basic detaching).
456
457 m_thLoopPool.detach_blocks(0,
459 [&tLoopFunction](const int nStart, const int nEnd)
460 {
461 // Call loop function without lock.
463 });
464
465 // Wait for loop to finish.
466 m_thLoopPool.wait();
467 }
Here is the call graph for this function:

◆ PooledLinearCode()

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

Implemented in rovecomm::RoveCommTCP, and rovecomm::RoveCommUDP.

Here is the caller 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
500 {
501 // Check current number of running and queued tasks.
502 return (m_thPool.get_tasks_total() <= 0);
503 }

◆ 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
183 {
184 // Signal for any open threads to stop executing,
185 m_bStopThreads = true;
186 // Update thread state.
188 }

◆ 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
385 {
386 // Check if the pools need to be resized.
387 if (m_thPool.get_thread_count() != nNumThreads)
388 {
389 // Pause queuing of new tasks to the threads, then purge them.
390 m_thPool.pause();
391 m_thPool.purge();
392 // Wait for open threads to terminate, then resize the pool.
393 m_thPool.reset(nNumThreads);
394 // Unpause queue.
395 m_thPool.unpause();
396
397 // Clear results vector.
398 m_vPoolReturns.clear();
399 }
400 // Check if the current pool tasks should be stopped before queueing more tasks.
402 {
403 // Pause queuing of new tasks to the threads, then purge them.
404 m_thPool.pause();
405 m_thPool.purge();
406 // Wait for threadpool to join.
407 m_thPool.wait();
408 // Unpause queue.
409 m_thPool.unpause();
410 }
411
412 // Loop nNumThreads times and queue tasks.
413 for (unsigned int i = 0; i < nNumTasksToQueue; ++i)
414 {
415 // Push single task to pool queue. No return value no control.
416 m_thPool.detach_task(
417 [this]()
418 {
419 // Run user code without lock.
420 this->PooledLinearCode();
421 });
422 }
423 }
virtual T PooledLinearCode()=0
Here is the call 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
313 {
314 // Check if the pools need to be resized.
315 if (m_thPool.get_thread_count() != nNumThreads)
316 {
317 // Pause queuing of new tasks to the threads, then purge them.
318 m_thPool.pause();
319 m_thPool.purge();
320 // Wait for open threads to terminate, then resize the pool.
321 m_thPool.reset(nNumThreads);
322 // Unpause queue.
323 m_thPool.unpause();
324
325 // Clear results vector.
326 m_vPoolReturns.clear();
327 }
328 // Check if the current pool tasks should be stopped before queueing more tasks.
330 {
331 // Pause queuing of new tasks to the threads, then purge them.
332 m_thPool.pause();
333 m_thPool.purge();
334 // Wait for threadpool to join.
335 m_thPool.wait();
336 // Unpause queue.
337 m_thPool.unpause();
338 }
339
340 // Loop nNumThreads times and queue tasks.
341 for (unsigned int i = 0; i < nNumTasksToQueue; ++i)
342 {
343 // Submit single task to pool queue.
344 m_vPoolReturns.emplace_back(m_thPool.submit_task(
345 [this]()
346 {
347 // Run user pool code without lock.
348 this->PooledLinearCode();
349 }));
350 }
351 }

◆ 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
625 {
626 // Declare instance variables.
628
629 // Loop until stop flag is set.
630 while (!bStopThread)
631 {
632 // Check if max IPS limit has been set.
634 {
635 // Get start execution time.
637 }
638
639 // Call method containing user code.
641
642 // Check if max IPS limit has been set.
644 {
645 // Get end execution time.
647 // Get execution time of user code.
649 // Check if the elapsed time is slower than the max iterations per seconds.
650 if (tmElapsedTime.count() < (1.0 / m_nMainThreadMaxIterationPerSecond) * 1000000)
651 {
652 // Calculate the time to wait to stay under IPS cap.
653 int nSleepTime = ((1.0 / m_nMainThreadMaxIterationPerSecond) * 1000000) - tmElapsedTime.count();
654 // Make this thread sleep for the remaining time.
656 }
657 }
658
659 // Check if thread state needs to be updated.
661 {
662 // Update thread state to running.
664 // Notify waiting start method that thread is now running.
665 m_cdThreadRunningCondition.notify_all();
666 }
667
668 // Call iteration per second tracking tick.
669 m_IPS.Tick();
670 }
671
672 // Notify waiting start method that thread is now stopping.
673 m_cdThreadRunningCondition.notify_all();
674 }
@ eRunning
Definition AutonomyThread.hpp:48
virtual void ThreadedContinuousCode()=0
std::condition_variable m_cdThreadRunningCondition
Definition AutonomyThread.hpp:601
Here is the call graph for this function:
Here is the caller 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
516 {
517 // Assign member variable.
519 }

◆ SetMainThreadPriority()

template<class T>
void AutonomyThread< T >::SetMainThreadPriority ( AutonomyThreadPriority ePriority)
inline

Set the OS priority for the main continuous thread.

Parameters
thPriority- The OS thread priority to set.
Note
This will take effect the next time Start() is called.
Author
clayjay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2026-03-21
254{ m_eMainThreadPriority = static_cast<BS::pr>(ePriority); }
BS::pr m_eMainThreadPriority
Definition AutonomyThread.hpp:590

◆ SetPoolThreadPriority()

template<class T>
void AutonomyThread< T >::SetPoolThreadPriority ( AutonomyThreadPriority ePriority)
inline

Set the OS priority for the highly parallelized pool threads.

Parameters
thPriority- The OS thread priority to set.
Note
This immediately resets the pool and applies the priority.
Author
clayjay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2026-03-21
267 {
268 // Update member variable.
269 m_ePoolThreadPriority = static_cast<BS::pr>(ePriority);
270 // Native OS priority extensions were removed in BS v5.1.0, so we just reset thread count.
271 m_thPool.reset(m_thPool.get_thread_count());
272 }
BS::pr m_ePoolThreadPriority
Definition AutonomyThread.hpp:591

◆ 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
130 {
131 // Tell any open thread to stop.
132 m_bStopThreads = true;
133 // Update thread state.
135
136 // Pause queuing of new tasks to the threads, then purge them.
137 m_thPool.pause();
138 m_thPool.purge();
139 m_thMainThread.pause();
140 m_thMainThread.purge();
141
142 // Wait for loop, pool and main thread to join.
143 this->Join();
144
145 // Update thread state.
147 // Clear results vector.
148 m_vPoolReturns.clear();
149 // Reset thread stop toggle.
150 m_bStopThreads = false;
151
152 // Submit single task to pool queue and store resulting future. Still using pool, as it's scheduling is more efficient.
154 [this]()
155 {
156 // Note: BS::this_thread::set_os_thread_priority was completely removed in BS v5.1.0.
157 // Priority is kept in memory but not applied to OS threads natively by the library anymore.
159 });
160
161 // Unpause pool queues.
162 m_thPool.unpause();
163 m_thMainThread.unpause();
164
165 // Block until thread is started or currently stopping if thread start failed.
168 [this]
170 }
void Join()
Waits for thread to finish executing and then closes thread. This method will block the calling code ...
Definition AutonomyThread.hpp:198
@ eStarting
Definition AutonomyThread.hpp:47
std::mutex m_muThreadRunningConditionMutex
Definition AutonomyThread.hpp:600
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:624
Here is the call graph for this function:

◆ ThreadedContinuousCode()

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

Implemented in rovecomm::RoveCommTCP, and rovecomm::RoveCommUDP.

Here is the caller graph for this function:

Member Data Documentation

◆ m_bStopThreads

template<class T>
std::atomic_bool AutonomyThread< T >::m_bStopThreads
private

◆ m_cdThreadRunningCondition

template<class T>
std::condition_variable AutonomyThread< T >::m_cdThreadRunningCondition
private

◆ m_eMainThreadPriority

template<class T>
BS::pr AutonomyThread< T >::m_eMainThreadPriority = BS::pr::normal
private

◆ m_ePoolThreadPriority

template<class T>
BS::pr AutonomyThread< T >::m_ePoolThreadPriority = BS::pr::normal
private

◆ m_eThreadState

template<class T>
std::atomic<AutonomyThreadState> AutonomyThread< T >::m_eThreadState
private

◆ m_IPS

template<class T>
IPS AutonomyThread< T >::m_IPS = IPS()
protected

◆ m_muThreadRunningConditionMutex

template<class T>
std::mutex AutonomyThread< T >::m_muThreadRunningConditionMutex
private

◆ m_nMainThreadMaxIterationPerSecond

template<class T>
int AutonomyThread< T >::m_nMainThreadMaxIterationPerSecond
private

◆ m_thMainThread

template<class T>
BS::thread_pool<BS::tp::pause> AutonomyThread< T >::m_thMainThread = BS::thread_pool<BS::tp::pause>(1)
private

◆ m_thPool

template<class T>
BS::thread_pool<BS::tp::pause> AutonomyThread< T >::m_thPool = BS::thread_pool<BS::tp::pause>(2)
private

◆ m_vPoolReturns

template<class T>
std::vector<std::future<T> > AutonomyThread< T >::m_vPoolReturns
private

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