2017-10-11 41 views
1

在Ada中嵌套“然后中止”结构合法吗?如果是,我可以如何正确使用它们?我有这样的代码:嵌套“然后中止”怪异行为

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; 

我希望这段代码应该在2秒后退出。但相反,它连续打印“B”而没有延迟(它忽略了delay 10.0)。它看起来代码这样的行为:

  1. 执行Put_Line("A")和等待2秒钟内
  2. 退出“然后中止”
  3. 执行循环忽略delay 10.0

如果不是delay 4.0我们插入delay 1.0(然后在循环内发生中止),程序正常工作。我认为这是非常危险的,因为“放弃那么”可库函数里面,例如:

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; 

有人能解释为什么这个节目在这个奇怪的方式表现?

回答

2

A Comparison of the Asynchronous Transfer of Control Features in Ada and the Real-Time Specification所述,“异步选择语句正确处理嵌套的ATC,例如,如果外部触发语句的延迟在内部延迟未决时到期,则内部延迟将被取消,并且ATC将会颁布出内部不可饶恕的部分...“

下面的变化按预期打印ABBBBC。触发语句的外部指定了5秒的超时时间;嵌套的触发语句指定三秒超时。由于后者的可中止部分仅消耗其第二个预算的一秒,随后的loop可以在外部超时之前打印四个B。将外部delay更改为1.0以重现您的示例中看到的效果。

with Ada.Text_IO; use Ada.Text_IO; 

procedure Main is 

    task TestTask; 

    task body TestTask is 
    begin 
     select 
     delay 5.0; 
     Put("C"); 
     then abort 
     select 
      delay 3.0; 
     then abort 
      Put("A"); 
      delay 1.0; 
     end select; 
     loop 
      Put("B"); 
      delay 1.0; 
     end loop; 
     end select; 
    end TestTask; 

begin 
    null; 
end Main;