Are nested "then abort" constructions legal in Ada? If yes, how properly can I use them? I have this code:
with Ada.Text_IO; use Ada.Text_IO;
procedure Main is
task TestTask is
end TestTask;
task body TestTask is
begin
select
delay 2.0;
Put_Line("C"); -- never executed
then abort
select
delay 5.0;
then abort
Put_Line("A");
delay 4.0;
end select;
loop
Put_Line("B");
delay 10.0;
end loop;
end select;
end TestTask;
begin
null;
end Main;
I expect that this code should exit after 2 seconds. But instead, it prints continuously "B" without even delay (it ignores delay 10.0
). It looks that code behaves in this way:
- Execute
Put_Line("A")
and wait 2 seconds - Exit from inner "then abort"
- Execute loop ignoring
delay 10.0
If instead of delay 4.0
we insert delay 1.0
(then aborting occurs inside the loop), the program works properly. I think it is very dangerous, because "abort then" can be inside library function, for example:
procedure Main is
----- It's function from library -----
procedure Foo is
begin
select
delay 5.0;
then abort
Put_Line("A");
delay 4.0;
end select;
end;
---------------------------------------
task TestTask is
end TestTask;
task body TestTask is
begin
select
delay 2.0;
Put_Line("C"); -- never executed
then abort
Foo;
loop
Put_Line("B");
delay 10.0;
end loop;
end select;
end TestTask;
begin
null;
end Main;
Can someone explain why this program behaves in this weird way?