SourcePro C++ 12.0 |
SourcePro® C++ API Reference Guide |
SourcePro C++ Documentation Home |
Handle class for functor-based, runnable objects. More...
#include <rw/thread/RWRunnableFunction.h>
Public Member Functions | |
RWRunnableFunction () | |
RWRunnableFunction (RWStaticCtor) | |
RWRunnableFunction (const RWRunnableFunction &second) | |
~RWRunnableFunction () | |
RWRunnableFunction & | operator= (const RWRunnableFunction &second) |
void | setFunctor (const RWFunctor0 &functor) |
RWFunctor0 | getFunctor () const |
Static Public Member Functions | |
static RWRunnableFunction | make () |
static RWRunnableFunction | make (const RWFunctor0 &functor) |
Protected Member Functions | |
RWRunnableFunctionImp & | body (void) const |
The RWRunnableFunction class is a handle class for functor-based 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 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.
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.
RWRunnableFunction is used to access a synchronous runnable, which executes the specified functor in the same thread that calls start().
Although functors are central to the inner workings of the Threading package, you may not need to deal with functors directly. Instead, the rwtMakeRunnableFunction() global template functions and macros can build the appropriate functor instance and use it to initialize an RWRunnableFunction object directly from a function pointer.
#include <rw/thread/RWRunnableFunction.h> #include <rw/thread/rwtMakeRunnableFunction.h> #include <rw/functor/rwtMakeFunctor0.h> void foo(int i) { cout << i << endl; } int main() { // Create an RWRunnableFunction that calls foo. RWRunnableFunction runnablefunc = rwtMakeRunnableFunction(foo,7); runnablefunc.start(); // prints 7 // Create a functor directly, passing 11 as client data. functor = rwtMakeFunctor0((void(*)(void))0, foo, 11); // Set runnablefunc to use the new functor: runnablefunc.setFunctor(functor); runnablefunc.start(); // prints 11 return 0; }
RWRunnableFunction::RWRunnableFunction | ( | ) | [inline] |
Constructs an empty RWRunnableFunction handle instance.
RWRunnableFunction::RWRunnableFunction | ( | RWStaticCtor | ) | [inline] |
Constructs a global static handle instance that may be assigned to before construction. The static constructor does not change the instance state.
RWRunnableFunction::RWRunnableFunction | ( | const RWRunnableFunction & | second | ) | [inline] |
Binds a new handle to the runnable instance, if any, pointed to by the handle second.
RWRunnableFunction::~RWRunnableFunction | ( | ) | [inline] |
Destructor.
RWRunnableFunctionImp& RWRunnableFunction::body | ( | void | ) | const [protected] |
Gets a reference for the body instance, if any.
RWTHRInvalidPointer | Thrown if this handle is not attached to a body. |
Reimplemented from RWRunnableHandle.
RWFunctor0 RWRunnableFunction::getFunctor | ( | ) | const |
Gets the current functor instance, if any, associated with the runnable. Possible exceptions include RWTHRInvalidPointer and RWTHRInternalError.
static RWRunnableFunction RWRunnableFunction::make | ( | const RWFunctor0 & | functor | ) | [static] |
Constructs and returns an RWRunnableFunction that executes the specified functor when started.
static RWRunnableFunction RWRunnableFunction::make | ( | ) | [static] |
Constructs and returns an RWRunnableFunction object with an undefined functor. The setFunctor() member must be used to define a functor prior to starting.
RWRunnableFunction & RWRunnableFunction::operator= | ( | const RWRunnableFunction & | second | ) | [inline] |
Binds this
to the runnable instance, if any, pointed to by the handle second.
void RWRunnableFunction::setFunctor | ( | const RWFunctor0 & | functor | ) |
Sets the functor to be executed by this runnable. Possible exceptions include RWTHRInvalidPointer and RWTHRInternalError.
© Copyright Rogue Wave Software, Inc. All Rights Reserved.
Rogue Wave and SourcePro are registered trademarks of Rogue Wave Software, Inc. in the United States and other countries. All other trademarks are the property of their respective owners.
Contact Rogue Wave about documentation or support issues.