Check out the new USENIX Web site. next up previous contents
Next: future_ptr_fun Up: Computation Future Functor Adaptors Previous: Computation Future Functor Adaptors

future_pointer_to_N-ary_function

This template class is defined as follows:

 template <class Arg1, class Arg2, , class ArgN,

class Result>

class future_pointer_to_N-ary_function

: public N-ary_function<Arg1, Arg2, , ArgN,

Result >

{

protected:

pointer_to_N-ary_function<Arg1, Arg2, , ArgN,

Result> Nf;

public:

typedef TaskN<Arg1, Arg2, , ArgN,

Result>::future_type future_type;

future_pointer_to_N-ary_function(

Result (*f)(Arg1, Arg2, , ArgN))

: Nf(ptr_fun(f))

{ }

future_pointer_to_N-ary_function()

{ clean up task lists }

future_type operator()(Arg1, Arg2, , ArgN);

};

This template class has N+1 parameters - N parameters (Arg1, , ArgN) for the N argument types of the N-ary function this corresponds to, and the last parameter(class Result) for the result type of the N-ary function.

This class is defined as a subclass of the N-ary_function template class provided in STL. (Note that STL defines only unary and binary function template classes. Ternary, quaternary function template classes etc. can be easily defined.)

The template class defines/exports a public type definition - future_type. This is the type of the handle that is returned when a '()' operator of an object of this class is invoked (see below). The implementation of future_type should ensure that an object of future_type should be castable to Result type.

The constructor of the template class takes one argument which is an N-ary function pointer. It converts this N-ary function pointer into a pointer_to_N-ary_function object using the ptr_fun adaptor provided in STL and stores it in its member defined by Nf.

The template class TaskN stands for a thread or a task implementation class that exports a future template data type. One requirement on this future data type is that it has a cast operator that can be used to convert it to Result type. In a typical implementation the future will have fields that mark the status of the underlying task and whether the future has been resolved. The cast operator will check this field to see if the task underlying the future has been scheduled and completed. If it is not completed it will block the current thread for the underlying to complete and then resolve the future to the value returned by the task. If the task is completed before this cast operator is invoked, the return value is saved and the future is marked to have a valid value. In this case, the cast operator returns immediately with the saved value as the value of the future.

The future_pointer_to_N-ary_function class also supports a '()' operator which is basically an invocation operator of the future function object. It expects N arguments and creates a task of type TaskN with the N-ary function objects and these N arguments and returns a future_type object. The created task is added to an internal list of tasks. When the destructor of the future_pointer_to_N-ary_function object is invoked (when the object is deleted or goes out of scope), this internal list and the tasks in that list are deleted and disposed.


next up previous contents
Next: future_ptr_fun Up: Computation Future Functor Adaptors Previous: Computation Future Functor Adaptors

Sundaresan Neelakantan
Thu May 15 16:11:49 PDT 1997