template<class Return>
class RWTThreadIOUFunction< Return >
The RWTThreadIOUFunction class is a handle class for functor-based threaded runnable objects.
A runnable object provides the basic mechanisms used to create, control, and monitor the threads of execution within your application. Runnables are used to define the task or activity to be performed by a thread.
Each runnable object is reference-counted. A threaded runnable body instance keeps a count of the number of handles that currently reference it. A runnable object is deleted when the last handle that references the body is deleted.
A functor-based runnable accepts a functor object for execution. A functor is an object used to encapsulate a function call. Each functor keeps a pointer to the function and copies of the argument values that are to be passed to the function. Invoking a functor produces a call to the function, and in this case, a return value.
A functor-based runnable simply redefines the basic run() member to invoke a functor instance stored within the runnable. With this capability, you do not have to resort to sub-classing or other intrusive techniques to customize the execution behavior of a runnable. The functor-based runnables allow you to dynamically specify the functions you want to execute when a runnable is started.
RWTThreadIOUFunction is used to access a threaded runnable, which creates a new thread to execute the specified functor. The result of the functor is returned in the form of an IOU. An IOU may be obtained as soon as the runnable is created. To get the actual result from the IOU you must redeem it. If the result has not yet been calculated, the calling thread blocks until it has.
- Example
#include <rw/thread/RWTThreadIOUFunction.h>
#include <rw/itc/RWTIOUResult.h>
#include <rw/functor/rwBind.h>
#include <iostream>
#include <math.h>
int greatestPrime(int limit) {
int i, m;
char* sieve = new char[limit + 1];
if (sieve == 0) {
return -1;
}
for (i = 0; i <= limit; ++i) {
sieve[i] = 1;
}
for (i = 2; i <= sqrt((double)limit); ++i) {
if (sieve[i] != 0) {
for (m = i + i; m <= limit; m += i) {
sieve[m] = 0;
}
}
}
for (i = limit; i >= 2; --i) {
if (sieve[i] != 0) {
return i;
}
}
return 0;
}
int main() {
std::cout << "The greatest prime less than six million is... "
<< std::flush;
std::cout << answer.
redeem() << std::endl;
return 0;
}
RWCompletionState start()
A readable IOU handle.
Definition RWTIOUResult.h:122
Redeemable redeem(void) const
Definition RWTIOUResult.h:312
Handle class for functor-based threaded runnable objects.
Definition RWTThreadIOUFunction.h:125
RWTIOUResult< Return > result() const
static RWTThreadIOUFunction< Return > make()
unspecified_type rwBind(C &&c, As &&... args)
Binds a callable and arguments into a callable object.
OUTPUT:
The greatest prime less than six million is... 5999993
- See also
- RWTFunctor, RWThread, RWRunnable, RWRunnableHandle, RWThreadAttribute