Tutorial: Using Futures from the Parallel Programming Library

From RAD Studio
Jump to: navigation, search

Go Up to Parallel Programming Library Tutorials

This tutorial shows how to use Futures from the Parallel Programming Library (PPL). A Future is a TTask with a return result and therefore it can be considered as a parallel function.

This tutorial consists of a simple application with two buttons. One of them starts a calculation and the other one returns the value. Using Futures, you obtain this value when you ask for it and if it is not already calculated, it blocks until it is completed.

Creating the Project

Create a new project:

  • For Delphi, choose File > New > Multi-Device Application - Delphi > Blank Application.
  • For C++Builder, choose File > New > Multi-Device Application - C++Builder > Blank Application.

Adding the Components

  1. Add two TButton components to the form.
  2. Set the Text property of the first button to Start Future String. This button will start a Future.
  3. Set the Text property of the other button to Request Future String. This button will request the result.

Implementing the TTask.Future Functionality

First, implement the event handlers for the OnClick events of the Start Future String and Request Future String buttons. For this, write code as follows:

Delphi: In Delphi, the function passes to TTask.Future as an anonymous method.

procedure TForm1.Button1Click(Sender: TObject);
begin
  FutureString:= TTask.Future<string>(
    function:string
    begin
      {Some calculation that takes time}
      Sleep(3000);
      Result:='Hello ' + Random(42).ToString;
  end);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
  Button2.Text := FutureString.Value;
end;

C++:

void __fastcall TForm1::Button1Click(TObject *Sender){
  FutureString = TTask::Future<System::String>(0, this->FutureHandler);
}

System::String __fastcall TForm1::FutureHandler(TObject* Sender){
  // Some calculation that takes time
  Sleep(3000);
  System::String str = System::String().sprintf(L"Hello %d", Random(42));
  return str;
}
void __fastcall TForm1::Button2Click(TObject *Sender){
  Button2->Text = FutureString->Value;
}

Then, add the following declaration:

  public
    { Public declarations }
    FutureString: IFuture<string>;

C++:

public:		// User declarations
	System::DelphiInterface<IFuture__1<System::String> > FutureString;
	System::String __fastcall FutureHandler(TObject* Sender);

Finally, you should also include the necessary libraries to the code.

Delphi:

For Delphi applications, add the following unit to the uses clause if they are not present:

uses
  System.Threading;

C++:

For C++ applications, add the following include operator to the project header file:

#include <System.Threading.hpp>

Running the Application

At this point, you can now run the application:

  • Press F9 or choose Run > Run.
    Futures parallel1.png Futures parallel2.png

Press the Start Future String to run the procedure and then press Request Future String to obtain the result. You will notice that if the value is not yet calculated, it will wait for it to be completed. Otherwise, the result will display immediately.

See Also