SourcePro® API Reference Guide

 
List of all members | Public Member Functions | Static Public Member Functions
RWTThreadIOUFunction< Return > Class Template Reference

Handle class for functor-based threaded runnable objects. More...

#include <rw/thread/RWTThreadIOUFunction.h>

Inheritance diagram for RWTThreadIOUFunction< Return >:
RWThread RWRunnable RWRunnableHandle RWHandleBase

Public Member Functions

 RWTThreadIOUFunction ()
 
 RWTThreadIOUFunction (const RWTThreadIOUFunction< Return > &second)
 
 ~RWTThreadIOUFunction ()
 
RWTFunctor< Return()> getFunctor () const
 
RWTIOUResult< Return > operator() () const
 
RWTThreadIOUFunction< Return > & operator= (const RWTThreadIOUFunction< Return > &second)
 
RWTIOUResult< Return > result () const
 
void setFunctor (const RWTFunctor< Return()> &functor)
 
void setIOUEscrow (const RWTIOUEscrow< Return > &escrow)
 
- Public Member Functions inherited from RWThread
 RWThread ()
 
 RWThread (RWStaticCtor)
 
 RWThread (const RWThread &second)
 
 ~RWThread ()
 
bool canGetPriority () const
 
bool canGetProcessScopePriority () const
 
bool canGetSchedulingPolicy () const
 
bool canGetSystemScopePriority () const
 
bool canGetTimeSliceQuantum () const
 
bool canSetPriority () const
 
bool canSetProcessScopePriority () const
 
bool canSetSchedulingPolicy (RWSchedulingPolicy policy) const
 
bool canSetSystemScopePriority () const
 
bool canSetTimeSliceQuantum () const
 
RWThreadAttribute getActiveAttribute () const
 
RWThreadAttribute getAttribute () const
 
RWPriority getMaxPriority () const
 
RWPriority getMaxProcessScopePriority () const
 
RWPriority getMaxSystemScopePriority () const
 
unsigned long getMaxTimeSliceQuantum () const
 
RWPriority getMinPriority () const
 
RWPriority getMinProcessScopePriority () const
 
RWPriority getMinSystemScopePriority () const
 
unsigned long getMinTimeSliceQuantum () const
 
RWPriority getPriority () const
 
RWPriority getProcessScopePriority () const
 
RWThreadSelf getRWThreadSelf () const
 
RWSchedulingPolicy getSchedulingPolicy () const
 
unsigned getSuspendCount () const
 
RWPriority getSystemScopePriority () const
 
unsigned long getTimeSliceQuantum () const
 
RWThreadoperator= (const RWThread &second)
 
unsigned resume ()
 
void setAttribute (const RWThreadAttribute &second)
 
void setPriority (RWPriority priority)
 
void setProcessScopePriority (RWPriority priority)
 
void setSchedulingPolicy (RWSchedulingPolicy policy)
 
void setSystemScopePriority (RWPriority priority)
 
void setTimeSliceQuantum (unsigned long milliseconds)
 
unsigned suspend ()
 
void terminate ()
 
- Public Member Functions inherited from RWRunnable
 RWRunnable ()
 
 RWRunnable (RWStaticCtor)
 
 RWRunnable (const RWRunnable &second)
 
 ~RWRunnable ()
 
RWRunnable getNestedRunnable () const
 
RWRunnableSelf getRWRunnableSelf () const
 
void join (void)
 
RWWaitStatus join (unsigned long milliseconds)
 
RWRunnableoperator= (const RWRunnable &second)
 
void raise () const
 
void releaseInterrupt ()
 
RWWaitStatus requestCancellation ()
 
RWWaitStatus requestCancellation (unsigned long milliseconds)
 
RWWaitStatus requestInterrupt ()
 
RWWaitStatus requestInterrupt (unsigned long milliseconds)
 
RWCompletionState start ()
 
RWExecutionState wait (unsigned long stateMask)
 
RWWaitStatus wait (unsigned long stateMask, RWExecutionState *state, unsigned long milliseconds)
 
- Public Member Functions inherited from RWRunnableHandle
void addCallback (const RWTFunctor< void(const RWRunnable &, RWExecutionState)> &functor, unsigned long stateMask, RWCallbackScope scope=RW_CALL_REPEATEDLY)
 
RWCompletionState getCompletionState () const
 
RWExecutionState getExecutionState () const
 
bool isInterruptRequested () const
 
bool isSelf () const
 
bool isSelf (const RWThreadId &id) const
 
void removeCallback (const RWTFunctor< void(const RWRunnable &, RWExecutionState)> &functor)
 
RWThreadId threadId () const
 
- Public Member Functions inherited from RWHandleBase
bool isValid (void) const
 
bool operator!= (const RWHandleBase &second) const
 
bool operator< (const RWHandleBase &second) const
 
bool operator== (const RWHandleBase &second) const
 

Static Public Member Functions

static RWTThreadIOUFunction< Return > make ()
 
static RWTThreadIOUFunction< Return > make (const RWTFunctor< Return()> &functor)
 
static RWTThreadIOUFunction< Return > make (const RWTIOUEscrow< Return > &escrow, const RWTFunctor< Return()> &functor)
 
static RWTThreadIOUFunction< Return > make (const RWTFunctor< Return()> &functor, const RWThreadAttribute &attr)
 
static RWTThreadIOUFunction< Return > make (const RWTIOUEscrow< Return > &escrow, const RWTFunctor< Return()> &functor, const RWThreadAttribute &attr)
 
- Static Public Member Functions inherited from RWThread
static bool canGetMaxThreads ()
 
static bool canSuspendResume ()
 
static size_t getMaxThreads ()
 

Additional Inherited Members

- Protected Member Functions inherited from RWThread
 RWThread (RWThreadImp *threadImpP)
 
- Protected Member Functions inherited from RWRunnable
 RWRunnable (const RWRunnableSelf &second)
 
- Protected Member Functions inherited from RWRunnableHandle
 RWRunnableHandle ()
 
 RWRunnableHandle (RWStaticCtor)
 
 RWRunnableHandle (RWRunnableImp *runnableImpP)
 
 RWRunnableHandle (const RWRunnableHandle &second)
 
 ~RWRunnableHandle ()
 
RWRunnableHandleoperator= (const RWRunnableHandle &second)
 
- Protected Member Functions inherited from RWHandleBase
 RWHandleBase (void)
 
 RWHandleBase (RWStaticCtor)
 
 RWHandleBase (RWBodyBase *body)
 
 RWHandleBase (const RWHandleBase &second)
 
 ~RWHandleBase (void)
 
RWBodyBasebody (void) const
 
RWHandleBaseoperator= (const RWHandleBase &second)
 

Detailed Description

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 <iostream>
#include <math.h>
#include <rw/functor/rwBind.h>
#include <rw/itc/RWTIOUResult.h>
#include <rw/thread/RWTThreadIOUFunction.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((double)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;
}
}
return 0;
}
int main()
{
= RWTThreadIOUFunction<int>::make(rwBind(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:
std::cout << "The greatest prime less than six million is... "
<< std::flush;
// Block until the answer has been calculated and
// then print it:
std::cout << answer.redeem() << std::endl;
return 0;
}

OUTPUT:

The greatest prime less than six million is... 5999993
See also
RWTFunctor, RWThread, RWRunnable, RWRunnableHandle, RWThreadAttribute

Constructor & Destructor Documentation

template<class Return >
RWTThreadIOUFunction< Return >::RWTThreadIOUFunction ( )
inline

Constructs an empty RWTThreadIOUFunction handle instance.

template<class Return >
RWTThreadIOUFunction< Return >::RWTThreadIOUFunction ( const RWTThreadIOUFunction< Return > &  second)
inline

Binds a new handle to the runnable instance, if any, pointed to by the handle second.

template<class Return >
RWTThreadIOUFunction< Return >::~RWTThreadIOUFunction ( )
inline

Destructor.

Member Function Documentation

template<class Return>
RWTFunctor<Return()> RWTThreadIOUFunction< Return >::getFunctor ( ) const

Gets the current functor instance, if any, associated with the runnable. Possible exceptions include RWTHRInvalidPointer and RWTHRInternalError.

template<class Return>
static RWTThreadIOUFunction<Return> RWTThreadIOUFunction< Return >::make ( )
static

Constructs and returns an RWTThreadIOUFunction object with an undefined functor. The setFunctor() member must be used to define a functor prior to starting.

template<class Return>
static RWTThreadIOUFunction<Return> RWTThreadIOUFunction< Return >::make ( const RWTFunctor< Return()> &  functor)
static

Constructs and returns an RWTThreadIOUFunction that executes the specified functor when started.

template<class Return>
static RWTThreadIOUFunction<Return> RWTThreadIOUFunction< Return >::make ( const RWTIOUEscrow< Return > &  escrow,
const RWTFunctor< Return()> &  functor 
)
static

Constructs and returns an RWTThreadIOUFunction that executes the specified functor when started, and places the functor result in escrow.

template<class Return>
static RWTThreadIOUFunction<Return> RWTThreadIOUFunction< Return >::make ( const RWTFunctor< Return()> &  functor,
const RWThreadAttribute attr 
)
static

Constructs and returns an RWTThreadIOUFunction that executes the specified functor when started. A new thread is created using the attributes given by attr.

template<class Return>
static RWTThreadIOUFunction<Return> RWTThreadIOUFunction< Return >::make ( const RWTIOUEscrow< Return > &  escrow,
const RWTFunctor< Return()> &  functor,
const RWThreadAttribute attr 
)
static

Constructs and returns an RWTThreadIOUFunction that executes the specified functor when started. A new thread is created using the attributes given by attr, and the functor result is stored in escrow.

template<class Return>
RWTIOUResult<Return> RWTThreadIOUFunction< Return >::operator() ( ) const

Returns the IOU that will receive the result of the function.

template<class Return >
RWTThreadIOUFunction< Return > & RWTThreadIOUFunction< Return >::operator= ( const RWTThreadIOUFunction< Return > &  second)
inline

Binds this to the runnable instance, if any, pointed to by the handle second.

template<class Return>
RWTIOUResult<Return> RWTThreadIOUFunction< Return >::result ( ) const

Returns the IOU that will receive the result of the function.

template<class Return>
void RWTThreadIOUFunction< Return >::setFunctor ( const RWTFunctor< Return()> &  functor)

Sets the functor to be executed by this runnable. Possible exceptions include RWTHRInvalidPointer and RWTHRInternalError.

template<class Return>
void RWTThreadIOUFunction< Return >::setIOUEscrow ( const RWTIOUEscrow< Return > &  escrow)

Specifies an IOU escrow that is to receive the result of the function. The new IOU is not 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, a new escrow instance is automatically created to capture the next result. Possible exceptions include RWTHRInvalidPointer and RWTHRInternalError.

Copyright © 2023 Rogue Wave Software, Inc., a Perforce company. All Rights Reserved.