iTask-将参数赋值给匿名过程

时间:2018-07-11 19:06:36

标签: multithreading delphi

我需要创建一定数量的iTask,以在动态数组和记录中的其他字段中执行操作。每个iTask在此阵列的特定部分中运行。数组是记录中的字段,该字段作为var参数传递给iTask。

array字段中的操作进展顺利,但是在所有任务完成其工作之后,其他记录字段未返回任何值。在另一个仅在阵列上运行且仍有效的问题中,我曾从Dalija寻求帮助,但现在我在其他领域遇到了麻烦。

这是我的代码:

program ProjectTest;

{$APPTYPE CONSOLE}
{$R *.res}

uses
  System.SysUtils,
  System.Threading;

type
  myrec = record
    vet: array of integer;
    total: integer;
    average: integer;
  end;

// (1) ===> here is the major procedure that populates the dyn. array and
// calculates other two fields  :  myrec.total  and myrec.avg

procedure ProcA(const pin, pfin: integer; var Prec: myrec);
var
  vind: integer;
begin
  for vind := pin to pfin do
    begin
      Prec.vet[vind] := vind * 10;
      Prec.total := Prec.total + Prec.vet[vind];        // sum all array values
    end;
  Prec.average := Trunc(Prec.total / Length(Prec.vet)); // calculates the average

end;

// (2)  Here iTask is created and calls ProcA

function CreateTask(first, last: integer; var Pmyrec: myrec): ITask;

var
  mylocalrec: myrec;

begin
  mylocalrec := Pmyrec;
  Result := TTask.Create(
    procedure
    begin
      ProcA(first, last, mylocalrec)
    end);
end;

procedure Test;
var
  Recarray: myrec;
  Ptasks: array of ITask;
  vind, indtask, vslice: integer;
  vfirst, vlast, vthreads, vsize: integer;
begin

  vthreads := 4;
  vsize := 16;

  SetLength(Ptasks, vthreads);
  SetLength(Recarray.vet, vsize);

  // Initialize the array , just to check after iTask execution
  for vind := low(Recarray.vet) to high(Recarray.vet) do
    Recarray.vet[vind] := -33;

  // initialize the sum and average field just to check after iTask execution
  Recarray.total := -1;
  Recarray.average := -2;

  // portion of array to scan for each iTask
  vslice := Length(Recarray.vet) div vthreads;

  for indtask := low(Ptasks) to high(Ptasks) do
    begin
      vfirst := indtask * vslice;
      vlast := (indtask + 1) * vslice - 1;

      if (Length(Recarray.vet) mod vthreads <> 0) and (indtask = high(Ptasks)) then vlast := high(Recarray.vet);

      Ptasks[indtask] := CreateTask(vfirst, vlast, Recarray);
    end;

  // Starting all Tasks
  for indtask := low(Ptasks) to high(Ptasks) do
    Ptasks[indtask].Start;

  // Waits for all Tasks been concluded
  TTask.WaitForAll(Ptasks);

  // (3) Here it is listed the array contents and it is ok
  for vind := low(Recarray.vet) to high(Recarray.vet) do
      Writeln(' Array position  : ' + Format('%.3d', [vind]) + '   content  : ' + Recarray.vet[vind].tostring);

  Writeln(' =========================================================');

// (4) Here is is listed fields recarray.total and recarray.avg and they were not
// processed inside the iTask .    I expected to see the computed values for those fields

  Writeln(' Array sum   : ' + Format('%.0d', [Recarray.total]) + '    Array average   : ' + Format('%5.2n', [Recarray.average * 1.0]));

end;

begin
  Test;
  Readln;
end.

输出为:

 Array position  : 000   content  : 0
 Array position  : 001   content  : 10
 Array position  : 002   content  : 20
 Array position  : 003   content  : 30
 Array position  : 004   content  : 40
 Array position  : 005   content  : 50
 Array position  : 006   content  : 60
 Array position  : 007   content  : 70
 Array position  : 008   content  : 80
 Array position  : 009   content  : 90
 Array position  : 010   content  : 100
 Array position  : 011   content  : 110
 Array position  : 012   content  : 120
 Array position  : 013   content  : 130
 Array position  : 014   content  : 140
 Array position  : 015   content  : 150
 =========================================================
 Array sum   : -1    Array average   : -2,00

问题是:运行所有iTask之后,只有动态数组字段recarray.vet包含正确的值。像运行iTask之前一样,字段recarray.totalrecarray.average仍包含其初始值。

如何正确更新这些字段中的值,以便在任务完成运行后它们将包含正确的值?

2 个答案:

答案 0 :(得分:2)

虽然您的代码似乎只有一个问题-如何用任务填充记录中的整数字段-当您解决该问题时,您将遇到另一个问题-从多个线程读写相同的内存位置。

1。如何在记录中填充整数字段?

记录是值类型,动态数组是引用类型。这就是为什么您的代码可以更新数组中的值但不能更新记录中的值的原因。

让我们看看这里发生了什么。

function CreateTask(first, last: integer; var pmyrec: myrec): ITask;
var
  mylocalrec: myrec;
begin
  mylocalrec := pmyrec;

由于mylocalrecpmyrec是记录(值类型),因此它们的内容将占据两个不同的存储位置。以上分配会将pmyrec内容复制到mylocalrec

这等同于以下内容:

mylocalrec.vet := pmyrec.vet;
mylocalrec.total := pmyrec.total;
mylocalrec.average := pmyrec.average;

由于totalaverage也是值类型,因此它们的内容将被复制,并且从这一点出发,mylocalrec中那些整数字段中的任何一个的任何更改都不会对原始pmyrec。这就是代码失败的原因。

为什么将动态数组字段vetpmyrec分配到mylocalrec有用?

因为动态数组是引用类型-从一个变量到另一个变量的赋值仅复制引用(指针)的值,而不是实际内容。两个vet变量都指向开始运行任务之前分配的同一数组。

要解决上述问题,您必须传递某种引用类型而不是值类型。最简单的事情是声明记录指针类型并传递该指针。

type
  myrec = record
    vet: array of integer;
    total: integer;
    average: integer;
  end;

  pmyrec = ^myrec;


function CreateTask(first, last: integer; rec: pmyrec): ITask;
var
  mylocalrec: pmyrec;
begin
  mylocalrec := rec;
  Result := TTask.Create(
    procedure
    begin
      ProcA(first, last, mylocalrec^)
    end);
end;

...
Ptasks[indtask] := CreateTask(vfirst, vlast, @Recarray);

2。如何解决线程问题?

解决原始问题后,您将遇到线程问题。从多个线程读取和写入相同的内存位置是不安全的。您得到的结果可能不正确。虽然您可以运行数千次代码,并且所有值可能都是正确的,但迟早您会遇到无法使用它们的情况。

在您的情况下,填充动态数组是安全的,因为每个线程在数组的不同部分上运行,并且在您的任务运行时不会重新分配数组(其大小不会更改)。您的那部分代码是线程安全的。

计算totalaverage并不是线程安全的。

您必须将此类代码与主线程同步-在这种情况下,所有读取和写入操作都将从主线程完成,并且您将获得正确的结果。或者您必须在所有任务完成后运行此类代码。哪个更适合特定情况。

procedure ProcA(const pin, pfin: integer; prec: pmyrec);
var
  vind: integer;
  total: integer;
begin
  total := 0;
  for vind := pin to pfin do
    begin
      prec.vet[vind] := vind * 10;
      total := total + prec.vet[vind];        // sum all array values
    end;

  TThread.Synchronize(nil,
    procedure
    begin
      prec.total := prec.total + total;
      prec.average := Trunc(prec.total / Length(prec.vet)); // calculates the average
    end);
end;

但是,在任务中与主线程同步将导致使用TTask.WaitForAll方法的死锁,该方法也会在您的情况下从主线程运行。要解决这一部分,您还必须从另一个线程运行整个Test方法。

  TTask.Run(
    procedure
    begin
      Test;
    end);

当我们将所有这些部分放在一起时,完整的代码将是:

program ProjectTest;

{$APPTYPE CONSOLE}
{$R *.res}

uses
  System.SysUtils,
  System.Classes,
  System.Threading;

type
  myrec = record
    vet: array of integer;
    total: integer;
    average: integer;
  end;

  pmyrec = ^myrec;

// (1) ===> here is the major procedure that populates the dyn. array and
// calculates other two fields  :  myrec.total  and myrec.avg

procedure ProcA(const pin, pfin: integer; prec: pmyrec);
var
  vind: integer;
  total: integer;
begin
  total := 0;
  for vind := pin to pfin do
    begin
      prec.vet[vind] := vind * 10;
      total := total + prec.vet[vind];        // sum all array values
    end;

  TThread.Synchronize(nil,
    procedure
    begin
      prec.total := prec.total + total;
      prec.average := Trunc(prec.total / Length(prec.vet)); // calculates the average
    end);
end;

// (2)  Here iTask is created and calls ProcA

function CreateTask(first, last: integer; rec: pmyrec): ITask;
var
  mylocalrec: pmyrec;
begin
  mylocalrec := rec;
  Result := TTask.Create(
    procedure
    begin
      ProcA(first, last, mylocalrec);
    end);
end;

procedure Test;
var
  Recarray: myrec;
  Ptasks: array of ITask;
  vind, indtask, vslice: integer;
  vfirst, vlast, vthreads, vsize: integer;
begin

  vthreads := 4;
  vsize := 16;

  SetLength(Ptasks, vthreads);
  SetLength(Recarray.vet, vsize);

  // Initialize the array , just to check after iTask execution
  for vind := low(Recarray.vet) to high(Recarray.vet) do
    Recarray.vet[vind] := -33;

  // initialize the sum and average field just to check after iTask execution
  Recarray.total := -1;
  Recarray.average := -2;

  // portion of array to scan for each iTask
  vslice := Length(Recarray.vet) div vthreads;

  for indtask := low(Ptasks) to high(Ptasks) do
    begin
      vfirst := indtask * vslice;
      vlast := (indtask + 1) * vslice - 1;

      if (Length(Recarray.vet) mod vthreads <> 0) and (indtask = high(Ptasks)) then vlast := high(Recarray.vet);

      Ptasks[indtask] := CreateTask(vfirst, vlast, @Recarray);
    end;

  // Starting all Tasks
  for indtask := low(Ptasks) to high(Ptasks) do
    Ptasks[indtask].Start;

  // Waits for all Tasks been concluded
  TTask.WaitForAll(Ptasks);

  // (3) Here it is listed the array contents and it is ok
  for vind := low(Recarray.vet) to high(Recarray.vet) do
      Writeln(' Array position  : ' + Format('%.3d', [vind]) + '   content  : ' + Recarray.vet[vind].tostring);

  Writeln(' =========================================================');

// (4) Here is is listed fields recarray.total and recarray.avg and they were not
// processed inside the iTask .    I expected to see the computed values for those fields

  Writeln(' Array sum   : ' + Format('%.0d', [Recarray.total]) + '    Array average   : ' + Format('%5.2n', [Recarray.average * 1.0]));

end;

begin
  TTask.Run(
    procedure
    begin
      Test;
    end);
end.

答案 1 :(得分:0)

我晚饭后5分钟消化道:)

program Project1;

{$APPTYPE CONSOLE}
{$R *.res}

uses
  System.SysUtils,
  System.SyncObjs,
  System.Threading;

type
  Pmyrec = ^Tmyrec;
  Tmyrec = record
    vet: array of integer;
    total: integer;
    average: integer;
  end;

// (1) ===> here is the major procedure that populates the dyn. array and
// calculates other two fields  :  Tmyrec.total  and Tmyrec.avg

procedure ProcA(const pin, pfin: integer; const Prec: Pmyrec);
var
  vind: integer;
begin
  for vind := pin to pfin do
    begin
      Prec.vet[vind] := vind * 10;
      TInterlocked.Add(Prec.total, Prec.vet[vind]);     // sum all array values
    end;
end;

// (2)  Here iTask is created and calls ProcA

function CreateTask(first, last: integer; const Pmyrec: Pmyrec): ITask;
begin
  Result := TTask.Create(
    procedure
    begin
      ProcA(first, last, Pmyrec)
    end);
end;

procedure Test;
var
  Recarray: Tmyrec;
  Ptasks: array of ITask;
  vind, indtask, vslice: integer;
  vfirst, vlast, vthreads, vsize: integer;
begin

  vthreads := 4;
  vsize := 16;

  SetLength(Ptasks, vthreads);
  SetLength(Recarray.vet, vsize);

  // Initialize the array , just to check after iTask execution
  for vind := low(Recarray.vet) to high(Recarray.vet) do
    Recarray.vet[vind] := -33;

  // initialize the sum and average field just to check after iTask execution
  Recarray.total := -1;
  Recarray.average := -2;

  // portion of array to scan for each iTask
  vslice := Length(Recarray.vet) div vthreads;

  for indtask := low(Ptasks) to high(Ptasks) do
    begin
      vfirst := indtask * vslice;
      vlast := (indtask + 1) * vslice - 1;

      if (Length(Recarray.vet) mod vthreads <> 0) and (indtask = high(Ptasks)) then vlast := high(Recarray.vet);

      Ptasks[indtask] := CreateTask(vfirst, vlast, @Recarray);
    end;

  // Starting all Tasks
  for indtask := low(Ptasks) to high(Ptasks) do
    Ptasks[indtask].Start;

  // Waits for all Tasks been concluded
  TTask.WaitForAll(Ptasks);
  Recarray.average := Trunc(Recarray.total / Length(Recarray.vet)); // calculates the average


  // (3) Here it is listed the array contents and it is ok
  for vind := low(Recarray.vet) to high(Recarray.vet) do
      Writeln(' Array position  : ' + Format('%.3d', [vind]) + '   content  : ' + Recarray.vet[vind].tostring);

  Writeln(' =========================================================');

// (4) Here is is listed fields recarray.total and recarray.avg and they were not
// processed inside the iTask .    I expected to see the computed values for those fields

  Writeln(' Array sum   : ' + Format('%.0d', [Recarray.total]) + '    Array average   : ' + Format('%5.2n', [Recarray.average * 1.0]));

end;

begin
  Test;
  Readln;
end.