C++ Concurrency Sandbox
Loading...
Searching...
No Matches
IsPrimeCalculator Struct Reference

A Functor (Function Object) that calculates primality. More...

Collaboration diagram for IsPrimeCalculator:

Public Member Functions

void operator() () const
 Overloaded operator() to perform the calculation.
 

Public Attributes

int n
 The number to check for primality.
 
ResultQueue< std::pair< bool, int > > * outputQueue
 Pointer to the shared inbox.
 

Detailed Description

A Functor (Function Object) that calculates primality.

  • Instead of returning a value, it holds a pointer to a ResultQueue and "pushes" its result there once finished.

Definition at line 140 of file MessagePassing.cpp.

Member Function Documentation

◆ operator()()

void IsPrimeCalculator::operator() ( ) const
inline

Overloaded operator() to perform the calculation.

  • This allows the struct instance to be treated as a void() task by the ThreadPool.

Definition at line 148 of file MessagePassing.cpp.

148 {
149 bool prime = true;
150 if (n <= 1) prime = false;
151 else {
152 for (int i = 2; i * i <= n; i++) {
153 if (n % i == 0) {
154 prime = false;
155 break;
156 }
157 }
158 }
159 // Send the result to the orchestrator (Main Thread)
160 outputQueue->push({prime, n});
161 }
void push(T result)
Pushes a result into the queue and notifies the waiting thread.
int n
The number to check for primality.
ResultQueue< std::pair< bool, int > > * outputQueue
Pointer to the shared inbox.

References n, outputQueue, and ResultQueue< T >::push().

Here is the call graph for this function:

Member Data Documentation

◆ n

int IsPrimeCalculator::n

The number to check for primality.

Definition at line 141 of file MessagePassing.cpp.

Referenced by main(), and operator()().

◆ outputQueue

ResultQueue<std::pair<bool, int> >* IsPrimeCalculator::outputQueue

Pointer to the shared inbox.

Definition at line 142 of file MessagePassing.cpp.

Referenced by main(), and operator()().


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