From: Shark8 <onewingedshark@gmail.com>
Subject: Interresting difference in Normal-Returns/Expression-Functions and Extended-Returns.
Date: Mon, 22 Apr 2013 11:27:09 -0700 (PDT)
Date: 2013-04-22T11:27:09-07:00 [thread overview]
Message-ID: <97967083-d21d-4de2-aeb8-76d0d5818993@googlegroups.com> (raw)
I recently ran into unexpected behavior in the differences between a normal return and an extended return [in GNAT]: namely an extended return used to generate items in an array of tasks will *ALWAYS* execute in a sequential manner whereas the array generated with an expression-function or normal return is executed simultaneously.
Is there some subtle difference in the extended return that I'm unaware of? (Is this a bug?)
--------------------
-- Experiment.adb --
--------------------
Pragma Ada_2012;
With
Ada.Text_IO,
Ada.Numerics.Float_Random
;
Procedure Experiment is
-----------------------------------------------------
-- RNG; This sets up random numbers for durations. --
-----------------------------------------------------
Package RNG is
Function Random Return Duration;
Private
Use Ada.Numerics.Float_Random;
G : Generator;
Function Random Return Duration is
( Duration( Random(G) ) );
End RNG;
Package Body RNG is
Begin
Ada.Numerics.Float_Random.Reset( G );
End RNG;
--------------------------------------------------
-- Resource; Protected object handling Text-IO. --
--------------------------------------------------
protected Resource is
procedure Write( Input : In String );
procedure New_Line;
private
end Resource;
protected body Resource is
procedure Write( Input : In String ) is
begin
Ada.Text_IO.Put_Line( Input );
end Write;
Procedure New_Line is begin Write(""); end New_Line;
end Resource;
---------------------------------------------------------
-- Testing; This is the task-type we are playing with. --
---------------------------------------------------------
Task Type Testing( Text : Not Null Access Constant String ) is
end Testing;
Task Body Testing is
D : Duration := RNG.Random; -- Get a random duration.
Begin
delay D; -- Delay for that duration.
-- Then print the Text for the task as well as the Delay.
Resource.Write( Text.ALL & ASCII.HT & Duration'Image(D) );
End Testing;
-- Make; these functions make an access-to-tasks for arrays.
-- Note: these should all be equivelant.
Function Make_EF(Input : in String) Return Not Null Access Testing is
( New Testing( Text => New String'(Input) ) );
Function Make_ER(Input : in String) Return Not Null Access Testing is
begin
Return Result : Not Null Access Testing:= New Testing(New String'(Input));
End Make_ER;
Function Make_NR(Input : in String) Return Not Null Access Testing is
begin
Return New Testing(New String'(Input));
End Make_NR;
Begin
Resource.Write("TEST_1:"& ASCII.HT &"Expression Function");
TEST_1:
declare
Function Make(Input : in String) Return Not Null Access Testing
Renames Make_EF;
P : Constant Array (Positive Range <>) of
Not Null Access Constant Testing:=
( Make("Bob"), Make("Steve"), Make("Dave"), Make("Joey") );
begin
null;
end TEST_1;
Resource.New_Line;
Resource.Write("TEST_2:"& ASCII.HT &"Normal Return Function");
TEST_2:
declare
Function Make(Input : in String) Return Not Null Access Testing
Renames Make_NR;
P : Constant Array (Positive Range <>) of
Not Null Access Constant Testing:=
( Make("Bob"), Make("Steve"), Make("Dave"), Make("Joey") );
begin
null;
end TEST_2;
Resource.New_Line;
Resource.Write("TEST_3:"& ASCII.HT &"Extended Return Function");
TEST_3:
declare
Function Make(Input : in String) Return Not Null Access Testing
Renames Make_ER;
P : Constant Array (Positive Range <>) of
Not Null Access Constant Testing:=
( Make("Bob"), Make("Steve"), Make("Dave"), Make("Joey") );
begin
null;
end TEST_3;
Resource.New_Line;
Resource.Write( "Terminating." );
End Experiment;
next reply other threads:[~2013-04-22 18:27 UTC|newest]
Thread overview: 33+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-04-22 18:27 Shark8 [this message]
2013-04-22 19:13 ` Interresting difference in Normal-Returns/Expression-Functions and Extended-Returns Dmitry A. Kazakov
2013-04-22 20:11 ` Shark8
2013-04-23 6:15 ` Simon Wright
2013-04-23 7:35 ` Stephen Leake
2013-04-23 8:58 ` Dmitry A. Kazakov
2013-05-02 2:20 ` Randy Brukardt
2013-05-02 4:41 ` Shark8
2013-04-23 6:32 ` egilhh
2013-04-23 15:24 ` Adam Beneschan
2013-04-24 5:56 ` egilhh
2013-04-24 14:40 ` Adam Beneschan
2013-04-25 5:30 ` egilhh
2013-04-25 5:41 ` Shark8
2013-04-23 16:00 ` Adam Beneschan
2013-04-23 16:52 ` Simon Wright
2013-04-23 17:57 ` Adam Beneschan
2013-04-25 8:48 ` egilhh
2013-04-25 15:19 ` Adam Beneschan
2013-05-02 2:33 ` Randy Brukardt
2013-05-02 2:38 ` Randy Brukardt
2013-05-02 16:15 ` Adam Beneschan
2013-05-02 22:00 ` Randy Brukardt
2013-05-03 1:11 ` Adam Beneschan
2013-05-03 5:34 ` Simon Wright
2013-05-03 6:43 ` egilhh
2013-05-03 16:49 ` Adam Beneschan
2013-05-03 23:09 ` Randy Brukardt
2013-04-23 23:00 ` Shark8
2013-04-23 23:16 ` Adam Beneschan
2013-05-02 2:28 ` Randy Brukardt
2013-05-02 16:37 ` Adam Beneschan
2013-04-23 16:33 ` Simon Wright
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox