Ada 准备好从入口点终止的任务

Ada put a task ready to terminate from entry point

我的任务只有一个 Stop 条目和一个 else 部分,我如何实现该任务在无限循环中执行 else 部分中的操作,但是当调用 Stop 时它已准备好终止

这里是 select 部分

select  
  accept Stop do  
    exit;  
  end Stop;  
else  
  delay 3.0;  
  --do something
end select;

这样做(为清楚起见添加了假设的循环语句):

loop
   select
      accept Stop;
      exit;
   or
      delay 3.0;
      --  do something
   end select;
end loop;

请注意,只有在需要处理其参数时才需要 accept 语句的主体。因为 Stop 有 none,所以 accept 语句有一个主体是没有意义的。

您的错误来自 LRM, 5.7 的这条规则:

An exit_statement that applies to a given loop_statement shall not appear within a body or accept_statement, if this construct is itself enclosed by the given loop_statement.

我还把你的 else 改成了 or 这样 Stop 就是在整个等待时间内接受,我认为这就是您想要的。对于 elseStop 仅在 select 被执行时被接受,而不是delay 开始后。