Main example method.
Separate threads.
171{
172
178
179
182
184 ThreadedPrimeCalculator1.
Start();
185
187 ThreadedPrimeCalculator2.
Start();
188
190 ThreadedPrimeCalculator3.
Start();
191
193 ThreadedPrimeCalculator4.
Start();
194
196 ThreadedPrimeCalculator5.
Start();
197
198
199 ThreadedPrimeCalculator1.
Join();
200 ThreadedPrimeCalculator2.
Join();
201 ThreadedPrimeCalculator3.
Join();
202 ThreadedPrimeCalculator4.
Join();
203
205 ThreadedPrimeCalculator5.
Join();
206
207
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
222
223
225 ThreadedPrimeCalculator5.
Start();
226
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