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

Mad*_*adu 2 parallel-processing concurrency ada task

我在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;
Run Code Online (Sandbox Code Playgroud)

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

声明必须在"开始"之前

提到getCount程序的定义.

tra*_*god 6

直接的问题是你已经指定了一个子程序体, 而没有首先任务体处理序列语句部分中指定相应的子程序声明.它应该在声明部分,如图所示这里.

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

function Get_Count return Integer;
Run Code Online (Sandbox Code Playgroud)

拥有这样的身体:

function Get_Count return Integer is
begin
   return Count;
end Get_Count;
Run Code Online (Sandbox Code Playgroud)


Mar*_*c C 6

一个声明一定要来"开始"前,和你的"getCount将"的声明之后的"开始".重新定位:

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

begin
Run Code Online (Sandbox Code Playgroud)

但实际上,请注意trashgod的建议.