【问题标题】:Creating procedure or function in Ada task在 Ada 任务中创建过程或函数
【发布时间】:2012-04-28 21:45:57
【问题描述】:

我正在 Ada 中创建以下任务,我希望它包含一个告诉我缓冲区计数的过程。我该怎么做?

package body Buffer is

  task body Buffer is

    size: constant := 10000; -- buffer capacity
    buf: array(1.. size) of Types.Item;
    count: integer range 0..size := 0;
    in_index,out_index:integer range 1..size := 1;

  begin
    procedure getCount(currentCount: out Integer) is
    begin   
      currentCount := count;
    end getCount;   

    loop
      select
        when count<size =>
          accept put(item: in Types.Item) do
            buf(in_index) := item;
          end put;
          in_index := in_index mod size+1;
          count := count + 1;
        or
          when count>0 =>
            accept get(item:out Types.Item) do
              item := buf(out_index);
            end get;
            out_index := out_index mod size+1;
            count := count - 1;
        or
          terminate;
      end select;
    end loop;
  end Buffer;

end Buffer;

当我编译这段代码时,我得到一个错误

声明必须在“开始”之前

参考getCount过程的定义。

【问题讨论】:

    标签: concurrency parallel-processing task ada


    【解决方案1】:

    声明必须在“开始”之前,并且您的“getCount”声明“开始”之后。重新定位:

    procedure getCount(currentCount: out Integer) is
    begin   
        currentCount := count;
    end getCount;   
    
    begin
    

    但说真的,请注意垃圾神的建议。

    【讨论】:

      【解决方案2】:

      直接的问题是您在task body已处理语句序列 部分中指定了subprogram body 而没有首先指定相应的subprogram declaration .它应该放在声明部分中,如here所示。

      更大的问题似乎是创建一个有界缓冲区,protected type 似乎更适合。示例可以在§II.9 Protected Types§9.1 Protected Types 中找到。在protected type Bounded_Buffer,你可以添加一个

      function Get_Count return Integer;
      

      拥有这样的身体:

      function Get_Count return Integer is
      begin
         return Count;
      end Get_Count;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-05-06
        • 2012-06-15
        • 1970-01-01
        • 1970-01-01
        • 2016-08-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多