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
PrimeNumbers.hpp File Reference

Example file that creates multiple threads that calculate a random amount of prime numbers. More...

#include "../../src/interfaces/AutonomyThread.hpp"
#include "../../src/util/ExampleChecker.h"
Include dependency graph for PrimeNumbers.hpp:

Go to the source code of this file.

Classes

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

Functions

void RunExample ()
 Main example method.
 

Detailed Description

Example file that creates multiple threads that calculate a random amount of prime numbers.

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

Function Documentation

◆ RunExample()

void RunExample ( )

Main example method.

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

Separate threads.

171{
172 // Declare prime calculator threads.
173 PrimeCalculatorThread ThreadedPrimeCalculator1;
174 PrimeCalculatorThread ThreadedPrimeCalculator2;
175 PrimeCalculatorThread ThreadedPrimeCalculator3;
176 PrimeCalculatorThread ThreadedPrimeCalculator4;
177 PrimeCalculatorThread ThreadedPrimeCalculator5;
178
179
182 // Calc 1.
183 ThreadedPrimeCalculator1.SetPrimeCount(5);
184 ThreadedPrimeCalculator1.Start();
185 // Calc 2.
186 ThreadedPrimeCalculator2.SetPrimeCount(50);
187 ThreadedPrimeCalculator2.Start();
188 // Calc 3.
189 ThreadedPrimeCalculator3.SetPrimeCount(10000);
190 ThreadedPrimeCalculator3.Start();
191 // Calc 4.
192 ThreadedPrimeCalculator4.SetPrimeCount(500);
193 ThreadedPrimeCalculator4.Start();
194 // Calc 5. Example of a thread that takes to long.
195 ThreadedPrimeCalculator5.SetPrimeCount(9999999);
196 ThreadedPrimeCalculator5.Start();
197
198 // Wait for threads to finish.
199 ThreadedPrimeCalculator1.Join();
200 ThreadedPrimeCalculator2.Join();
201 ThreadedPrimeCalculator3.Join();
202 ThreadedPrimeCalculator4.Join();
203 // This thread will take took long, stop it prematurely.
204 ThreadedPrimeCalculator5.RequestStop();
205 ThreadedPrimeCalculator5.Join();
206
207 // Print length of calculated primes vectors.
208 std::cout << "Creating separate threads:" << std::endl;
209 std::vector<int> vPrimes = ThreadedPrimeCalculator1.GetPrimes();
210 std::cout << "Calculator1 Primes Length: " << vPrimes.size() << std::endl;
211 vPrimes = ThreadedPrimeCalculator2.GetPrimes();
212 std::cout << "Calculator2 Primes Length: " << vPrimes.size() << std::endl;
213 vPrimes = ThreadedPrimeCalculator3.GetPrimes();
214 std::cout << "Calculator3 Primes Length: " << vPrimes.size() << std::endl;
215 vPrimes = ThreadedPrimeCalculator4.GetPrimes();
216 std::cout << "Calculator4 Primes Length: " << vPrimes.size() << std::endl;
217 std::cout << "\n\nThis thread was stopped prematurely before reaching 9999999." << std::endl;
218 vPrimes = ThreadedPrimeCalculator5.GetPrimes();
219 std::cout << "Calculator5 Primes Length: " << vPrimes.size() << std::endl;
220
221 // Clear Calculator5 and restart the thread.
222 // Don't join before program exits to demonstrate
223 // graceful shutdowns when user doesn't do what they're supposed to.
224 ThreadedPrimeCalculator5.ClearPrimes();
225 ThreadedPrimeCalculator5.Start();
226 // Print info to user.
227 std::cout << "Calculator5 was restarted and then main program exited without joining. (graceful shutdown demo)" << std::endl;
228 vPrimes = ThreadedPrimeCalculator5.GetPrimes();
229 std::cout << "Calculator5 Primes Length: " << vPrimes.size() << std::endl;
230}
void Join()
Waits for thread to finish executing and then closes thread. This method will block the calling code ...
Definition AutonomyThread.hpp:180
void RequestStop()
Signals threads to stop executing user code, terminate. DOES NOT JOIN. This method will not force the...
Definition AutonomyThread.hpp:164
void Start()
When this method is called, it starts a new thread that runs the code within the ThreadedContinuousCo...
Definition AutonomyThread.hpp:117
This class creates a thread for calculating N prime numbers. This is an example class demonstrating t...
Definition PrimeNumbers.hpp:30
void ClearPrimes()
Clears the prime results vector.
Definition PrimeNumbers.hpp:159
void SetPrimeCount(int nNum)
Mutator for the Prime Count private member.
Definition PrimeNumbers.hpp:130
std::vector< int > GetPrimes()
Accessor for the Primes private member.
Definition PrimeNumbers.hpp:150
Here is the call graph for this function: