RWTThreadIOUFunction<Return> RWThread
#include <rw/thread/RWTThreadIOUFunction.h>
The RWTThreadIOUFunction class is a handle class for a 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-base 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, and at that time if the result has not yet been calculated, then the calling thread will block until it has.
Although functors are central to the inner workings of Threading package classes, you may not need to deal with functors directly. Instead, the rwtMakeThreadIOUFunction() global template functions and macros can build the appropriate functor instance and use it to initialize an RWTThreadIOUFunction object directly from a function pointer.
#include <iostream.h> #include <math.h> #include <rw/thread/RWTThreadIOUFunction.h> #include <rw/itc/RWTIOUResult.h> #include <rw/thread/rwtMakeThreadIOUFunction.h> int greatestPrime(int limit) { // The sieve of Eratosthenes: int i, m; char* sieve = new char[limit + 1]; if (sieve == 0) return -1; // not enough memory for (i=0; i<=limit; ++i) sieve[i] = 1; for (i=2; i<=sqrt(limit); ++i) if (sieve[i] != 0) for (m=i+i; m<=limit; m+=i) sieve[m] = 0; // Return the greatest prime less than or equal to limit: for (i=limit; i>=2; --i) if (sieve[i] != 0) return i; } int main() { RWTThreadIOUFunction<int> lastprime = rwtMakeThreadIOUFunction(greatestPrime, 6000000); lastprime.start(); // spawn thread to calculate big prime RWTIOUResult<int> answer = lastprime.result(); // get an IOU // While the thread is busy calculating, print this // suspenseful message to keep the user entertained: cout << "The greatest prime less than six million is... " << flush; // Block until the answer has been calculated and // then print it: cout << answer.redeem() << endl; return 0; } /* * OUTPUT: The greatest prime less than six million is... 5999993 * */
RWTThreadIOUFunction(void);
Constructs an empty RWTThreadIOUFunction handle instance.
RWTThreadIOUFunction(const RWTThreadIOUFunction<Return>& second);
Binds a new handle to the runnable instance, if any, pointed to by the handle second.
RWTThreadIOUFunction<Return>& operator=(const RWTThreadIOUFunction<Return>& second);
Binds this to the runnable instance, if any, pointed to by the handle second.
RWTIOUResult<Return>& operator()(void) const;
Returns the IOU that will receive the result of the function.
RWTFunctorR0<Return> getFunctor(void) const;
Gets the current functor instance, if any, associated with the runnable. Possible exceptions include RWTHRInvalidPointer and RWTHRInternalError.
RWTIOUResult<Return> result(void) const;
Returns the IOU that will receive the result of the function.
void setFunctor(const RWTFunctorR0<Return>& functor);
Sets the functor to be executed by this runnable. Possible exceptions include RWTHRInvalidPointer and RWTHRInternalError.
void setIOUEscrow(const RWTIOUEscrow<Return>& escrow);
Specifies an IOU escrow that is to receive the result of the function. The new IOU will not be used until the next time start() is called. Each time an RWTThreadIOUFunction object is restarted, it checks its current IOU escrow handle to see if it is valid, and if so, checks to see whether the escrow has already been used to capture a result or exception, or has been aborted. If the escrow object is found to be in any of these "redeemable" states, then a new escrow instance is automatically created to capture the next result. Possible exceptions include RWTHRInvalidPointer and RWTHRInternalError.
static RWTThreadIOUFunction<Return> make(void);
Constructs and returns an RWTThreadIOUFunction object with an undefined functor. The setFunctor() member must be used to define a functor prior to starting.
static RWTThreadIOUFunction<Return> make(const RWTFunctorR0<Return>& functor);
Constructs and returns an RWTThreadIOUFunction that will execute the specified functor when started.
static RWTThreadIOUFunction<Return> make(const RWTIOUEscrow<Return>& escrow, const RWTFunctorR0<Return>& functor);
Constructs and returns an RWTThreadIOUFunction that will execute the specified functor when started, and the functor result will be placed in escrow.
static RWTThreadIOUFunction<Return> make(const RWTFunctorR0<Return>& functor, const RWThreadAttribute& attr);
Constructs and returns an RWTThreadIOUFunction that will execute the specified functor when started. A new thread will be created using the attributes given by attr.
static RWTThreadIOUFunction<Return> make(const RWTIOUEscrow<Return>& escrow, const RWTFunctorR0<Return>& functor, const RWThreadAttribute& attr);
Constructs and returns an RWTThreadIOUFunction that will execute the specified functor when started. A new thread will be created using the attributes given by attr and the functor result will be stored in escrow.
rwtMakeThreadIOUFunction, RWTFunctorR0, RWThread, RWRunnable, RWRunnableHandle, RWThreadAttribute
©Copyright 2000, Rogue Wave Software, Inc.
Contact Rogue Wave about documentation or support issues.