Show: Delphi C++
Display Preferences

Using TTask from the Parallel Programming Library

From RAD Studio
Jump to: navigation, search

Go Up to Using the Parallel Programming Library


The Parallel Programming Library (PPL) provides a TTask class to run one task or multiple tasks in parallel. A Task is a unit of work you need to get done. The PPL does the association between the task and the thread that performs the task so you can run several tasks in parallel without having to create your own custom threads and managing them.

TTask creates and manages interaction with instances of ITask. ITask is an interface that provides a range of methods and properties to Start, Wait, Cancel and also a property for Status (Created, WaitingToRun, Running, Completed, WaitingForChildren, Canceled, Exception).

Running a Task in the Background

One functionality of TTask is to prevent the user interface from locking up if you want to start something in the background. The following code example shows how to run a single task and start it:

Delphi:

 
procedure TForm1.Button1Click(Sender: TObject);
var
 aTask: ITask;
begin
 aTask := TTask.Create (procedure ()
   begin
     sleep (3000); // 3 seconds
     ShowMessage ('Hello');
   end);
 aTask.Start;
end;

C++:

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

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    _di_ITask task = TTask::Create([&](){
        Sleep(3000);
        ShowMessage("Hello");
    });
    task->Start();
}

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

class TCppTask : public TCppInterfacedObject<TProc> {
public:
    void __fastcall Invoke() {
        Sleep(3000);
        ShowMessage("Hello");
    }
};
 
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    _di_ITask task = TTask::Create(_di_TProc(new TCppTask()));
    task->Start();
}

Waiting for Tasks to Complete

TTask provides WaitForAll and WaitForAny to wait for the completion of all or any tasks. WaitForAll returns when all of the tasks are completed, whereas WaitForAny tells you the first one that is completed. For example, if you have two tasks A and B which take 3 and 5 seconds respectively, the time for you to get a result is:

The following example uses the WaitForAll method:

Delphi:

 
procedure TForm1.MyButtonClick(Sender: TObject);
var 
  tasks: array of ITask; 
  value: Integer; 
begin 
  Setlength (tasks ,2); 
  value := 0; 
 
 tasks[0] := TTask.Create (procedure () 
  begin 
   sleep(3000);
   TInterlocked.Add (value, 3000); 
  end); 
 tasks[0].Start; 
 
 tasks[1] := TTask.Create (procedure () 
   begin 
   sleep (5000);
   TInterlocked.Add (value, 5000);
 end); 
 tasks[1].Start; 
 
 TTask.WaitForAll(tasks); 
 ShowMessage ('All done: ' + value.ToString); 
end;

C++:

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

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    _di_ITask tasks[2];
    int value = 0;
 
    tasks[0] = TTask::Create([&](){
        Sleep(3000);
        TInterlocked::Add(value, 3000);
    });
    tasks[0]->Start();
 
    tasks[1] = TTask::Create([&](){
        Sleep(5000);
        TInterlocked::Add(value, 5000);
    });
    tasks[1]->Start();
 
    TTask::WaitForAll(tasks, sizeof(tasks)/sizeof(tasks[0])-1);
    ShowMessage("All done! " + IntToStr(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<TProc> {
public:
    TCppTask(int &value, int increase) : m_value(value), m_increase(increase) {}
    void __fastcall Invoke() {
        Sleep(m_increase);
        TInterlocked::Add(m_value, m_increase);
    }
protected:
    int& m_value;
    int m_increase;
};
 
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    _di_ITask tasks[2];
    int value = 0;
 
    tasks[0] = TTask::Create(_di_TProc(new TCppTask(value, 3000)));
    tasks[0]->Start();
 
    tasks[1] = TTask::Create(_di_TProc(new TCppTask(value, 5000)));
    tasks[1]->Start();
 
    TTask::WaitForAll(tasks,(sizeof(tasks)/sizeof(tasks[0])-1));
    ShowMessage("All done! " + IntToStr(value));
}

See Also

Personal tools
In other languages
Previous Versions
Assistance