我只知道从Delphi XE7中有Parallel.For很酷。所以我尝试转换一小段代码,例如:
procedure TestParallel;
var
i, j, u, v: integer;
map: array of array of integer;
begin
SetLength(map, 101, 101);
u := 100; v := 100;
for i := 0 to u do
begin
for j := 0 to v do
begin
if (map[i][j] <> 0) then
begin
map[i][j] := 1;
end;
end;
end;
end;
到:
procedure TestParallel;
var
i, u, v: integer;
map: array of array of integer;
begin
SetLength(map, 101, 101);
u := 100; v := 100;
TParallel.&For(0, u, procedure(i: integer)
var
j: integer;
begin
for j := 0 to v do
begin
if (map[i][j] <> 0) then
begin
map[i][j] := 1;
end;
end;
end);
end;
更新
是的,上面的代码编译,以下不是(如果地图数组作为var参数传递)
type
data = array of array of integer;
procedure TestParallel(var map: data);
var
i, u, v: integer;
begin
SetLength(map, 101, 101);
u := 100; v := 100;
TParallel.&For(0, u, procedure(i: integer)
var
j: integer;
begin
for j := 0 to v do
begin
if (map[i][j] <> 0) then
begin
map[i][j] := 1;
end;
end;
end);
end;
但不幸的是,编译器说:
[dcc64 Error] Project2.dpr(70): E2555 Cannot capture symbol 'map'
那么,如何使这项工作尽可能简单?