gpt4 book ai didi

delphi - Delphi threadvar 是否适用于 Parallel.For?

转载 作者:行者123 更新时间:2023-12-03 14:40:29 26 4
gpt4 key购买 nike

来自here它说

"The ThreadVar keyword starts a set of variable definitions that are used by threads. Each thread is given a separate instance of each variable, thereby avoiding data conflicts, and preserving thread independence. "

那么我可以像这样在 Parallel.For 中使用吗?

threadvar
threadID: integer;

procedure TForm5.Button1Click(Sender: TObject);
var
Tot: Integer;
begin
TParallel.For(1, Max, procedure (I: Integer)
begin
threadID := i; // each thread gets its own threadID?
if IsPrime (threadID) then
TInterlocked.Increment (Tot);
end);
end;

最佳答案

您当然可以将 threadvar 与 PPL 代码一起使用。在内部,PPL 代码位于系统线程库之上,因此 threadvar 按照您的预期工作。

关于delphi - Delphi threadvar 是否适用于 Parallel.For?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41977879/

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