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
PrimeCalculatorThread Class Reference

This class creates a thread for calculating N prime numbers. This is an example class demonstrating the use of the threading interface. More...

#include <PrimeNumbers.hpp>

Inheritance diagram for PrimeCalculatorThread:
Collaboration diagram for PrimeCalculatorThread:

Public Member Functions

void SetPrimeCount (int nNum)
 Mutator for the Prime Count private member.
 
int GetDesiredPrimeAmount ()
 Accessor for the Prime Counter private member.
 
std::vector< int > GetPrimes ()
 Accessor for the Primes private member.
 
void ClearPrimes ()
 Clears the prime results vector.
 
- Public Member Functions inherited from AutonomyThread< void >
 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.
 

Private Member Functions

bool IsPrime (int &nNum)
 Check if a number if prime.
 
void CalculatePrimes (int &nCount)
 Calculate 'count' number of primes.
 
void ThreadedContinuousCode () override
 This code will run in a separate thread. Main code goes here.
 
void PooledLinearCode () override
 Any highly parallelizable code that can be used in the main thread goes here.
 

Private Attributes

std::vector< int > m_vThreadPrimes
 
int m_nCount = 10
 
int m_nCurrentCount = 2
 

Additional Inherited Members

- Public Types inherited from AutonomyThread< void >
enum  AutonomyThreadState
 
- Protected Member Functions inherited from AutonomyThread< void >
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.
 
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< void > 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 inherited from AutonomyThread< void >
IPS m_IPS
 

Detailed Description

This class creates a thread for calculating N prime numbers. This is an example class demonstrating the use of the threading interface.

Author
ClayJay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2023-07-28

Member Function Documentation

◆ IsPrime()

bool PrimeCalculatorThread::IsPrime ( int &  nNum)
inlineprivate

Check if a number if prime.

Parameters
num- The number to check.
Returns
true - The number is prime.
false - The number is not prime.
Author
ClayJay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2023-07-22
48 {
49 if (nNum <= 1)
50 {
51 return false;
52 }
53 for (int i = 2; i * i <= nNum; ++i)
54 {
55 if (nNum % i == 0)
56 {
57 return false;
58 }
59 }
60
61 return true;
62 }
Here is the caller graph for this function:

◆ CalculatePrimes()

void PrimeCalculatorThread::CalculatePrimes ( int &  nCount)
inlineprivate

Calculate 'count' number of primes.

Parameters
nCount- The number of primes to calculate.
Author
ClayJay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2023-07-22
73 {
74 // Loop until we have the required amount of primes.
75 if (int(m_vThreadPrimes.size()) < nCount)
76 {
77 // Check if our current number is a prime.
78 if (IsPrime(m_nCurrentCount))
79 {
80 m_vThreadPrimes.push_back(m_nCurrentCount);
81 }
82
83 // Increment counter.
84 ++m_nCurrentCount;
85 }
86 }
bool IsPrime(int &nNum)
Check if a number if prime.
Definition PrimeNumbers.hpp:47
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ThreadedContinuousCode()

void PrimeCalculatorThread::ThreadedContinuousCode ( )
inlineoverrideprivatevirtual

This code will run in a separate thread. Main code goes here.

Author
ClayJay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2023-07-22

Implements AutonomyThread< void >.

96 {
97 // Change this to calculate a different number of prime numbers.
98 CalculatePrimes(m_nCount);
99
100 // Check if we have reached the desired number of primes.
101 if (int(this->GetPrimes().size()) >= this->GetDesiredPrimeAmount())
102 {
103 // Call thread stop.
104 this->RequestStop();
105 }
106 }
void RequestStop()
Signals threads to stop executing user code, terminate. DOES NOT JOIN. This method will not force the...
Definition AutonomyThread.hpp:164
void CalculatePrimes(int &nCount)
Calculate 'count' number of primes.
Definition PrimeNumbers.hpp:72
int GetDesiredPrimeAmount()
Accessor for the Prime Counter private member.
Definition PrimeNumbers.hpp:140
std::vector< int > GetPrimes()
Accessor for the Primes private member.
Definition PrimeNumbers.hpp:150
GOpaque< Size > size(const GMat &src)
Here is the call graph for this function:

◆ PooledLinearCode()

void PrimeCalculatorThread::PooledLinearCode ( )
inlineoverrideprivatevirtual

Any highly parallelizable code that can be used in the main thread goes here.

Author
ClayJay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2023-07-25

Implements AutonomyThread< void >.

116{}

◆ SetPrimeCount()

void PrimeCalculatorThread::SetPrimeCount ( int  nNum)
inline

Mutator for the Prime Count private member.

Parameters
nNum- The amount of primes to calculate.
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{ m_nCount = nNum; }
Here is the caller graph for this function:

◆ GetDesiredPrimeAmount()

int PrimeCalculatorThread::GetDesiredPrimeAmount ( )
inline

Accessor for the Prime Counter private member.

Returns
int - The amount of primes the calculator will produce.
Author
ClayJay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2023-07-22
140{ return m_nCount; }
Here is the caller graph for this function:

◆ GetPrimes()

std::vector< int > PrimeCalculatorThread::GetPrimes ( )
inline

Accessor for the Primes private member.

Returns
std::vector<int> - A vector of the resultant primes.
Author
ClayJay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2023-07-22
150{ return m_vThreadPrimes; }
Here is the caller graph for this function:

◆ ClearPrimes()

void PrimeCalculatorThread::ClearPrimes ( )
inline

Clears the prime results vector.

Author
ClayJay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2023-07-25
159{ m_vThreadPrimes.clear(); }
Here is the caller graph for this function:

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