混入电话并接受Ada中的选择?

时间:2019-02-26 17:18:22

标签: ada

是否可以在一个select语句中将受保护的入口调用(带有屏障)与accepts混合使用?在不使用延迟和两个不同的连续选择的情况下,一个用于接受,一个用于进入呼叫:

task body Task is
begin
  select
    accept Get;
  or
    accept Put;
  or
    Protected_Object.Alive;
  end select;
end Task;

protected body Protected_Object is
begin
  entry Alive when Is_Alive is
  ...

我希望Task在等待对Get,Put或Alive中的障碍打开的呼叫时被锁定。

2 个答案:

答案 0 :(得分:3)

根据您希望更频繁地发生的情况,有两种选择:

如果您希望/希望获取/放入事件比活着发生的次数更多:

task body Test is
begin
    loop
        select
            accept Get;
        or
            accept Put;
        or 
            -- Pick a value that makes sense
            -- This is how long you check Get
            -- and Put before trying Alive
            delay 0.0001;  

            -- Asynchronous transfer of control
            -- will try Protected_Object.Alive
            -- until the second delay is finished
            select
                -- Pick a value that makes sense
                -- Should be much smaller than the above
                -- delay
                delay 0.0000001;  
            then abort
                Protected_Object.Alive;
            end select;

        end select;

    end loop;
end Test;

如果您希望/希望Alive检查的次数多于Get / Put,则:

task body Test is
begin
    loop
        select
            accept Get;
        or
            accept Put;
        else

            -- Asynchronous transfer of control
            -- will try Protected_Object.Alive
            -- until the delay is finished
            select
                -- Pick a value that makes sense
                delay 0.0001;  
            then abort
                Protected_Object.Alive;
            end select;

        end select;

    end loop;
end Test;

无论哪种方式,如果需要,您都需要决定如何/何时结束该循环。

答案 1 :(得分:0)

请注意,您可以这样做:

select
   Protected_Object.Alive;
then abort
   select
      accept Get;
   or
      accept Put;
   end select;
end select;

这可能会给您您想要的,尽管了解ATC可能很棘手。