我正在运行德尔菲XE2,并试图熟悉OmniThreadLibrary,我已经安装了3.03b。
我一直在看Parallel.ForEach的例子,并且不确定背景中发生了什么(这很可能是显而易见的--抱歉)。任何你能提供的信息,以帮助我更好地理解如何实现我的目标,将不胜感激。
假设我有一些记录,它只是两个相关值的容器,a和b,然后我想运行一个并行循环,返回这些记录的数组。使用OmniThreadLibrary可以做到这一点吗?
例如,以MultithreadingMadeSimple ForEachUnorderedPrimes示例为基础,我是否可以这样做:
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;我知道上面的代码示例有一些非常基本的问题,但是我想让您了解我想要做的是什么。
发布于 2014-11-18 00:17:34
我对这个例子有些困惑--这个应用程序不需要队列。适当的示例代码是:
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;所以你通常是怎么用Parallel.ForEach做的.
https://stackoverflow.com/questions/26978936
复制相似问题