TParallel.For: Store values in a TList while they are calculated in a TParallel.For loop

谁说我不能喝 提交于 2019-12-10 20:12:32

问题


I want to use a TParallel.&For loop to calculate, for example, the prime numbers between 1 and 100000 and save all these prime numbers in AList: TList<Integer>:

procedure TForm1.Button1Click(Sender: TObject);
var
  i: Integer;
  AList: TList<Integer>;
  LoopResult: Tparallel.TLoopResult;
begin
  AList:=TList<Integer>.Create;
  TParallel.&For(1, 100000,
    procedure(AIndex: Integer)
    begin
      if IsPrime(AIndex) then
      begin
        //add the prime number to AList
      end;
    end);

  //show the list
  for i := 0 to AList.Count-1 do
  begin
    Memo1.Lines.Add(IntToStr(AList[i]));
  end;

end;

The calculations can be performed in parallel without issue but the TList is a shared resource. How can I add confirmed primes to the list in a threadsafe way?


回答1:


You would simply call AList.Add(AIndex), and then Sort() the list after the loop is finished. But TList is not thread-safe, so you need a lock around the Add(), like a TCriticalSection or TMutex:

procedure TForm1.Button1Click(Sender: TObject);
var
  i: Integer;
  AList: TList<Integer>;
  ALock: TCriticalSection;
  LoopResult: TParallel.TLoopResult;
begin
  AList := TList<Integer>.Create;
  ALock := TCriticalSection.Create;

  TParallel.&For(1, 100000,
    procedure(AIndex: Integer)
    begin
      if IsPrime(AIndex) then
      begin
        ALock.Enter;
        try
          AList.Add(AIndex);
        finally
          ALock.Leave;
        end;
      end;
    end);

  AList.Sort;
  for i := 0 to AList.Count-1 do
  begin
    Memo1.Lines.Add(IntToStr(AList[i]));
  end;

  ALock.Free;
  AList.Free;
end;

Or use TThreadList<T> instead:

procedure TForm1.Button1Click(Sender: TObject);
var
  i: Integer;
  AList: TThreadList<Integer>;
  LList: TList<Integer>;
  LoopResult: TParallel.TLoopResult;
begin
  AList := TThreadList<Integer>.Create;

  TParallel.&For(1, 100000,
    procedure(AIndex: Integer)
    begin
      if IsPrime(AIndex) then
      begin
        AList.Add(AIndex);
      end;
    end);

  LList := AList.LockList;
  try
    LList.Sort;
    for i := 0 to LList.Count-1 do
    begin
      Memo1.Lines.Add(IntToStr(LList[i]));
    end;
  finally
    AList.UnlockList;
  end;

  AList.Free;
end;


来源:https://stackoverflow.com/questions/37532891/tparallel-for-store-values-in-a-tlist-while-they-are-calculated-in-a-tparallel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!