Concurrency 在Ada任务中创建过程或函数

Concurrency 在Ada任务中创建过程或函数,concurrency,parallel-processing,task,ada,Concurrency,Parallel Processing,Task,Ada,我正在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 rang

我正在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;
包体缓冲区不可用
任务体缓冲区是
大小:常数:=10000;--缓冲容量
buf:Types.Item的数组(1..size);
计数:整数范围0..size:=0;
in_索引,out_索引:整数范围1..size:=1;
开始
过程getCount(currentCount:out Integer)为
开始
currentCount:=计数;
结束计数;
环
选择
什么时候算
接受put(项:在类型中。项)do
buf(in_索引):=项目;
端置;
in_索引:=in_索引模块大小+1;
计数:=计数+1;
或
当计数>0=>
接受get(项目:out Types.item)do
项目:=buf(外部索引);
最终得到;
out_index:=out_index mod size+1;
计数:=计数-1;
或
终止
终端选择;
端环;
末端缓冲器;
末端缓冲器;
当我编译这段代码时,我得到一个错误

声明必须在“开始”之前


参考
getCount
过程的定义。

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

更大的问题似乎是创建一个有界缓冲区,而a似乎更适合该缓冲区。示例可在和中找到。在
受保护类型绑定的\u缓冲区中
,可以添加

function Get_Count return Integer;
有这样一个身体:

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

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

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

begin

但事实上,要注意垃圾神的建议。

+1很好的发现。令人着迷的是@Keith Thompson改进的格式使我更容易看到它。