Ada并发执行过程指针数组
Ada concurrent execution of array of procedure pointers
我正在尝试同时执行存储在数组中的所有过程,但没有成功。我可以按顺序执行它们:
with ada.text_io; -- Tell compiler to use i/o library
use ada.text_io; -- Use library routines w/o fully qualified names
with Ada.Numerics.Float_Random; -- Use numerics for the random generation
use ada.Numerics.Float_Random;
procedure Main is
type Callback_Procedure is access procedure; -- Type that contains a procedure pointer
type arr is array (Natural range <>) of Callback_Procedure; -- Array starting from 0 toi infinite of procedure pointers
seed : Generator; -- Seed for the generator
-- Procedure that will execute "concurrently" the procedures passed in the array
procedure EjecutarConcurrentemente(procs : in arr ) is
begin
for index in 0 .. procs'Last loop
procs(index).all; -- .all dereferences the pointer so it is the procedure itself (the value pointed by the pointer)
end loop;
end EjecutarConcurrentemente;
function rndDelay return Duration is
num : Duration;
begin
num := Duration(Random(seed));
return num * Duration(10);
end ;
procedure test0 is
begin
delay rndDelay;
put("test0");
New_Line;
end test0;
procedure test1 is
begin
delay rndDelay;
put("test1");
New_Line;
end test1;
procedure test2 is
begin
delay rndDelay;
put("test2");
New_Line;
end test2;
procedure test3 is
begin
delay rndDelay;
put("test3");
New_Line;
end test3;
procedure test4 is
begin
delay rndDelay;
put("test4");
New_Line;
end test4;
procedure test5 is
begin
delay rndDelay;
put("test5");
New_Line;
end test5;
procs : arr(0..5);
begin
Reset(seed);
procs(0) := test0'Access;
procs(1) := test1'Access;
procs(2) := test2'Access;
procs(3) := test3'Access;
procs(4) := test4'Access;
procs(5) := test5'Access;
EjecutarConcurrentemente(procs);
end Main;
Main 过程主体用之前声明的 6 个过程填充一个数组,并使用该数组调用 EjecutarConcurrentemente,必须是这个新过程必须同时执行数组过程。
我被告知要使用 Task 功能,并且 testX 必须是不带参数的过程。
希望我解释得很好,而且我刚刚开始使用 Ada,所以代码很可能格式不正确。
谢谢。
谁告诉你使用任务并发执行谁给了你正确的答案。要同时 运行 所有六个过程,您需要六个任务(和六个 processors/processor 核心)。
这是完美运行的代码:
with ada.text_io; -- Tell compiler to use i/o library
use ada.text_io; -- Use library routines w/o fully qualified names
with Ada.Numerics.Float_Random; -- Use numerics for the random generation
use ada.Numerics.Float_Random;
procedure Main is
type Callback_Procedure is access procedure; -- Type that contains a procedure pointer
type arr is array (Natural range <>) of Callback_Procedure; -- Array starting from 0 toi infinite of procedure pointers
seed : Generator; -- Seed for the generator
task type Ex (proc : Callback_Procedure);
type A is access Ex;
task body Ex is
begin
proc.all; --.all dereferences the pointer so it is the procedure itself (the value pointed by the pointer)
end Ex;
-- Procedure that will execute concurrently the procedures passed in the array
procedure EjecutarConcurrentemente(procs : in arr ) is
p : A;
begin
for index in 0 .. procs'Last loop
p := new Ex(procs(index));
end loop;
end EjecutarConcurrentemente;
function rndDelay return Duration is
num : Duration;
begin
num := Duration(Random(seed));
return num * Duration(10);
end ;
procedure test0 is
begin
delay rndDelay;
put("test0");
New_Line;
end test0;
procedure test1 is
begin
delay rndDelay;
put("test1");
New_Line;
end test1;
procedure test2 is
begin
delay rndDelay;
put("test2");
New_Line;
end test2;
procedure test3 is
begin
delay rndDelay;
put("test3");
New_Line;
end test3;
procedure test4 is
begin
delay rndDelay;
put("test4");
New_Line;
end test4;
procedure test5 is
begin
delay rndDelay;
put("test5");
New_Line;
end test5;
procs : arr(0..5);
begin
Reset(seed);
procs(0) := test0'Access;
procs(1) := test1'Access;
procs(2) := test2'Access;
procs(3) := test3'Access;
procs(4) := test4'Access;
procs(5) := test5'Access;
EjecutarConcurrentemente(procs);
end Main;
我只是用一个过程指针作为参数创建一个任务,然后在数组循环中我做
new Ex(procs(index));
它会立即使用给定的过程启动一个新任务
我正在尝试同时执行存储在数组中的所有过程,但没有成功。我可以按顺序执行它们:
with ada.text_io; -- Tell compiler to use i/o library
use ada.text_io; -- Use library routines w/o fully qualified names
with Ada.Numerics.Float_Random; -- Use numerics for the random generation
use ada.Numerics.Float_Random;
procedure Main is
type Callback_Procedure is access procedure; -- Type that contains a procedure pointer
type arr is array (Natural range <>) of Callback_Procedure; -- Array starting from 0 toi infinite of procedure pointers
seed : Generator; -- Seed for the generator
-- Procedure that will execute "concurrently" the procedures passed in the array
procedure EjecutarConcurrentemente(procs : in arr ) is
begin
for index in 0 .. procs'Last loop
procs(index).all; -- .all dereferences the pointer so it is the procedure itself (the value pointed by the pointer)
end loop;
end EjecutarConcurrentemente;
function rndDelay return Duration is
num : Duration;
begin
num := Duration(Random(seed));
return num * Duration(10);
end ;
procedure test0 is
begin
delay rndDelay;
put("test0");
New_Line;
end test0;
procedure test1 is
begin
delay rndDelay;
put("test1");
New_Line;
end test1;
procedure test2 is
begin
delay rndDelay;
put("test2");
New_Line;
end test2;
procedure test3 is
begin
delay rndDelay;
put("test3");
New_Line;
end test3;
procedure test4 is
begin
delay rndDelay;
put("test4");
New_Line;
end test4;
procedure test5 is
begin
delay rndDelay;
put("test5");
New_Line;
end test5;
procs : arr(0..5);
begin
Reset(seed);
procs(0) := test0'Access;
procs(1) := test1'Access;
procs(2) := test2'Access;
procs(3) := test3'Access;
procs(4) := test4'Access;
procs(5) := test5'Access;
EjecutarConcurrentemente(procs);
end Main;
Main 过程主体用之前声明的 6 个过程填充一个数组,并使用该数组调用 EjecutarConcurrentemente,必须是这个新过程必须同时执行数组过程。
我被告知要使用 Task 功能,并且 testX 必须是不带参数的过程。
希望我解释得很好,而且我刚刚开始使用 Ada,所以代码很可能格式不正确。
谢谢。
谁告诉你使用任务并发执行谁给了你正确的答案。要同时 运行 所有六个过程,您需要六个任务(和六个 processors/processor 核心)。
这是完美运行的代码:
with ada.text_io; -- Tell compiler to use i/o library
use ada.text_io; -- Use library routines w/o fully qualified names
with Ada.Numerics.Float_Random; -- Use numerics for the random generation
use ada.Numerics.Float_Random;
procedure Main is
type Callback_Procedure is access procedure; -- Type that contains a procedure pointer
type arr is array (Natural range <>) of Callback_Procedure; -- Array starting from 0 toi infinite of procedure pointers
seed : Generator; -- Seed for the generator
task type Ex (proc : Callback_Procedure);
type A is access Ex;
task body Ex is
begin
proc.all; --.all dereferences the pointer so it is the procedure itself (the value pointed by the pointer)
end Ex;
-- Procedure that will execute concurrently the procedures passed in the array
procedure EjecutarConcurrentemente(procs : in arr ) is
p : A;
begin
for index in 0 .. procs'Last loop
p := new Ex(procs(index));
end loop;
end EjecutarConcurrentemente;
function rndDelay return Duration is
num : Duration;
begin
num := Duration(Random(seed));
return num * Duration(10);
end ;
procedure test0 is
begin
delay rndDelay;
put("test0");
New_Line;
end test0;
procedure test1 is
begin
delay rndDelay;
put("test1");
New_Line;
end test1;
procedure test2 is
begin
delay rndDelay;
put("test2");
New_Line;
end test2;
procedure test3 is
begin
delay rndDelay;
put("test3");
New_Line;
end test3;
procedure test4 is
begin
delay rndDelay;
put("test4");
New_Line;
end test4;
procedure test5 is
begin
delay rndDelay;
put("test5");
New_Line;
end test5;
procs : arr(0..5);
begin
Reset(seed);
procs(0) := test0'Access;
procs(1) := test1'Access;
procs(2) := test2'Access;
procs(3) := test3'Access;
procs(4) := test4'Access;
procs(5) := test5'Access;
EjecutarConcurrentemente(procs);
end Main;
我只是用一个过程指针作为参数创建一个任务,然后在数组循环中我做
new Ex(procs(index));
它会立即使用给定的过程启动一个新任务