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

Example

The following piece of code is an example of how the user of this library can use this facility. This code shows how a unary function pointer can be 'adapted' to a future unary function object and used.

Suppose int (*foo)(int) is a unary function pointer that expects an integer argument and returns an integer result. It can be converted to a future unary function object and used as follows:

  
future_pointer_to_unary_function<

int,int>::future_type fval

= (future_ptr_fun(foo))(4); // (A)

// do some unrelated work // (B)

int final = fval + 3; // (C)

The code executes as follows (see figure 2 for a pictorial representation of the execution of this code):
In the statement labeled (A) the following sequence of operations takes place: The unary function pointer foo is converted into a future unary function object with the use of the adaptor future_ptr_fun. Then the operator '()' is invoked on the resultant future unary function object with argument 4. This results in a task being scheduled to compute foo with argument 4 and a future handle is returned.
In the statement(s) labeled (B) the user code can perform computations that do not need the results of the foo function described above.
In the statement labeled (C) the result of the foo function is required and the user code adds 3 to this result and assigns it to final. Note that fval is of a future type while 3 is of type int. Since + is only defined between similar types (and not between future and int types) this statement would be valid only if there was a way to go from the future type to its Result type (i.e., int type). As mentioned before, every future type has to provide a cast operator to its Result type. In the implementation of the cast operator, the current thread waits for the task corresponding to this future resolution to finish and returns the value returned by the task.

  
Figure: The execution pattern of the example given in page gif: The statement labeled (A) creates an int future object out of the foo function pointer by first creating a future_ptr_to_unary_function object using the future_ptr_fun adaptor and schedules it with argument 4. Statement (B) represents an unrelated computation. Statement (C) requires the future to be resolved as a result of the addition operation between the int future object and an integer constant 3 to produce 'final'.


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

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