gpt4 book ai didi

multithreading - 退出Parallel.For循环的最快方法?

转载 作者:行者123 更新时间:2023-12-03 18:30:13 27 4
gpt4 key购买 nike

当用户单击“取消”按钮或关闭/销毁表单时,我需要以最快的方式退出TParallel.For循环。我已经尝试了TParallel.TLoopState.StopTParallel.TLoopState.Break:

var
BreakCondition: Boolean;

procedure TForm2.DoStartLoop;
begin
BreakCondition := False;
System.Threading.TParallel.For(1, 50,
procedure(idx: Integer; LS: TParallel.TLoopState)
begin
if BreakCondition then
begin
//LS.&BREAK;
LS.STOP;
//EXIT;
end
else
DoProcessValue(idx);
end);
end;

不幸的是,Embarcadero的 TParallel.TLoopState.StopTParallel.TLoopState.Break文档仅说明了以下内容:

Embarcadero Technologies does not currently have any additional information.



我也觉得循环不会很快中断。有没有更好的办法?

最佳答案

From the TParallel.For documentation:

If control of the iteration itself is needed from the iterator event, the iterator event handler should be one using the TParallel.TLoopState parameter. When present, the event handler will be given an instance of TParallel.TLoopState from which state information from Faulted,Stopped, or ShouldExit can be monitored, or the iteration loop itself can be controlled with the Break or Stop methods.



跟踪LoopState的方法是使用具有以下签名的方法:
TIteratorStateEvent = 
procedure (Sender: TObject; AIndex: Integer; const LoopState: TLoopState) of object;

或使用其匿名版本:
class function &For(AStride, ALowInclusive, AHighInclusive: Integer; 
const AIteratorEvent: TProc<Integer, TLoopState>; APool: TThreadPool): TLoopResult;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

如果文档失败,最简单的方法是在源代码中搜索该类,或者让代码完成工作。
TLoopState = class
private [...]
public
procedure Break;
procedure Stop;
function ShouldExit: Boolean; <<-- this looks useful

property Faulted: Boolean read GetFaulted;
property Stopped: Boolean read GetStopped; <<-- or this
property LowestBreakIteration: Variant read GetLowestBreakIteration;
end;

例子:
procedure TForm1.btnParallelForClick(Sender: TObject);
var
Tot: Integer;
SW: TStopwatch;
begin
try
// counts the prime numbers below a given value
Tot :=0;
SW :=TStopWatch.Create;
SW.Start;
//Use a method that supports LoopState
TParallel.For(2,1,Max,procedure(I:Int64; State: TLoopState)
begin
//check loopstate every now and again.
if State.ShouldExit then exit;
if IsPrime(I) then TInterlocked.Increment(Tot);
end);
SW.Stop;
Memo1.Lines.Add(Format('Parallel For loop. Time (in milliseconds): %d - Primes found: %d', [SW.ElapsedMilliseconds,Tot]));
except on E:EAggregateException do
ShowMessage(E.ToString);
end;
end;

关于multithreading - 退出Parallel.For循环的最快方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43790289/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com