Delphi - OmniThreadLibrary Parallel.ForEach with Records [英] Delphi - OmniThreadLibrary Parallel.ForEach with Records
问题描述
我正在运行Delphi XE2并试图熟悉OmniThreadLibrary,我安装了3.03b。
我一直在寻找Parallel.ForEach示例,并且不知道后台发生了什么事情(这可能很明显 - 后来很抱歉)。任何你可以提供帮助我更好地理解如何实现我的目标的信息将不胜感激。
假设我有一些记录只是一个容器的2个相关值, a和b。然后我想运行一个返回这些记录数组的并行循环。使用OmniThreadLibrary可以做到这一点吗?
例如,以MultithreadingMadeSimple ForEachUnorderedPrimes为例,我可以做些什么:
函数GetMyRecordArray(n:Integer):myRecordArray; {只是一种myRecord数组}
var
a,b:Double;
记录:TOmniValue;
recordQueue:IOmniBlockingCollection;
我:整数;
begin
SetLength(RESULT,n)
recordQueue:= TOmniBlockingCollection.Create;
Parallel.ForEach(1,n).Execute(
procedure(const value:integer)
begin
a:= {一些函数的值};
b:= {有些函数的值};
recordQueue.Add(myRecord.New(a,b));
end;
end);
i:= 0;
记录在recordQueue中do
begin
i:= i + 1;
RESULT [i - 1]:= record;
end;
end;
我知道上面的代码示例中存在一些非常基本的问题,但我可以理解它是什么是我正在尝试做的。
我对这个例子有些困惑 - 对于这个应用程序来说队列不是必需的。适当的示例代码是:
pre $函数GetMyRecordArray(n:Integer):myRecordArray; {只是一种myRecord数组}
var
a,b:Double;
begin
SetLength(RESULT,n)
Parallel.ForEach(1,n).Execute(
procedure(const value:integer)
begin
a := {某些函数的值};
b:= {某些函数的值};
RESULT [值-1]:= myRecord.New(a,b);
end;
结束);
end;
所以你通常只需要使用Parallel.ForEach ...
I am running Delphi XE2 and trying to get familiar with the OmniThreadLibrary, I have 3.03b installed.
I have been looking at the Parallel.ForEach examples and am not sure of what's going on in the background (this may well be obvious later - sorry). Any information you can offer to help me better understand how to achieve my goal will be much appreciated.
Suppose I have some record that is just a container for 2 related values, a and b. I then want to run a parallel loop that returns an array of these records. Is it possible to do this using the OmniThreadLibrary?
For example, taking the MultithreadingMadeSimple ForEachUnorderedPrimes example as a base, can I do something along the lines of:
function GetMyRecordArray(n: Integer): myRecordArray; {Just a type of Array of myRecord}
var
a, b: Double;
record: TOmniValue;
recordQueue: IOmniBlockingCollection;
i: Integer;
begin
SetLength(RESULT, n)
recordQueue := TOmniBlockingCollection.Create;
Parallel.ForEach(1, n).Execute(
procedure (const value: integer)
begin
a := {SOME FUNCTION OF value};
b := {SOME FUNCTION OF value};
recordQueue.Add(myRecord.New(a,b));
end;
end);
i := 0;
for record in recordQueue do
begin
i := i + 1;
RESULT[i - 1] := record;
end;
end;
I know there are some pretty fundamental problems with the above code example but I hop you can understand what it is I'm trying to do.
I had some confusion with the example - a queue wasn't necessary for this application. Appropriate example code would be:
function GetMyRecordArray(n: Integer): myRecordArray; {Just a type of Array of myRecord}
var
a, b: Double;
begin
SetLength(RESULT, n)
Parallel.ForEach(1, n).Execute(
procedure (const value: integer)
begin
a := {SOME FUNCTION OF value};
b := {SOME FUNCTION OF value};
RESULT[value - 1] := myRecord.New(a,b);
end;
end);
end;
So pretty much how you would do it normally just with Parallel.ForEach...
这篇关于Delphi - OmniThreadLibrary Parallel.ForEach with Records的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!