Show: Delphi C++
Display Preferences

Using TTask.IFuture from the Parallel Programming Library

From RAD Studio
Jump to: navigation, search

Go Up to Using the Parallel Programming Library


Future objects are objects that implement the IFuture interface, which defines a mechanism to access the result of a function asynchronously.

To create and use a future object:

  1. Call TTask.Future<T> to create a future object. The specified function starts running in the background.
  2. Read the Value property of the future object at any time to retrieve the result of the specified function. If the function execution in the background has not finished yet, Value blocks the program execution until the function finishes.

For example:

Delphi:

FutureObject := TTask.Future<Integer>(function: Integer
    begin
        Sleep(3000);
        Result := 16;
    end);
// …
MyValue := FutureObject.Value;

C++:

If you are using a Clang-enhanced C++ compiler, you can use lambda expressions:

auto FutureObject = TTask::Future<int>([&]()-> int {
    Sleep(3000);
    return 16;
});
// …
int MyValue = FutureObject->Value;

For any C++ compiler, you can declare a TProc subclass and use an instance of it casted as _di_TProc:

class TCppTask : public TCppInterfacedObject<TFunc<int> > {
public:
    int __fastcall Invoke() {
        Sleep(3000);
        return 16;
    }
};
// …
System::DelphiInterface<IFuture__1<int> > FutureObject = TTask::Future<int>(new TCppTask());
// …
int MyValue = FutureObject->Value;

See Also

Personal tools
In other languages
Previous Versions
Assistance