Ada 获取任务中字段的值

Ada 获取任务中字段的值,ada,Ada,我试图获取任务中字段的值。Worker循环相当大,所以我只留下重要的部分。 基本上,当用户输入w时,我希望控制台打印关于这三个字段的信息 附加问题:任务体工作者有一个循环,我想在循环的最后做一个FinishedJob++;但是我也不知道怎么做。任务不是记录。它不包含可从任务外部直接访问的字段。 您可以提供任务条目来查询在类中本地声明的变量的当前值,但请记住,条目强制调用任务和被调用任务同步 你的奖金问题的措辞看起来像C或C++思维。艾达并不是简单的C或C++,它的开始和结束都替换了{和}。 您希

我试图获取任务中字段的值。Worker循环相当大,所以我只留下重要的部分。 基本上,当用户输入w时,我希望控制台打印关于这三个字段的信息


附加问题:任务体工作者有一个循环,我想在循环的最后做一个FinishedJob++;但是我也不知道怎么做。

任务不是记录。它不包含可从任务外部直接访问的字段。 您可以提供任务条目来查询在类中本地声明的变量的当前值,但请记住,条目强制调用任务和被调用任务同步

你的奖金问题的措辞看起来像C或C++思维。艾达并不是简单的C或C++,它的开始和结束都替换了{和}。 您希望FinishedJob计数的可见性如何?您似乎希望与其他任务(如程序主任务)共享该编号。最简单的方法是创建一个受保护的对象,该对象在辅助任务完成时递增,即外部循环,并在适当的时候由读取任务读取

此类受保护对象的一个可能示例是:

protected Counter is
   procedure Increment;
   entry Read(Num : out Natural);
private
   Tally : Natural := 0;
   Is_Updated : Boolean := False;
end Counter;

protected body Counter is
   procedure Increment is
   begin
      Tally := Tally + 1;
      Is_Updated := True;
   end Increment;

   entry Read(Num : out Natural) when Is_Updated is
   begin
      Num := Tally;
      Is_Updated := False;
   end Read;
end Counter;

写入程序在完成其外部循环后调用Counter.Increment。读取器调用Counter.Read并在新值可用时立即获取它。

任务不是记录。它不包含可从任务外部直接访问的字段。 您可以提供任务条目来查询在类中本地声明的变量的当前值,但请记住,条目强制调用任务和被调用任务同步

你的奖金问题的措辞看起来像C或C++思维。艾达并不是简单的C或C++,它的开始和结束都替换了{和}。 您希望FinishedJob计数的可见性如何?您似乎希望与其他任务(如程序主任务)共享该编号。最简单的方法是创建一个受保护的对象,该对象在辅助任务完成时递增,即外部循环,并在适当的时候由读取任务读取

此类受保护对象的一个可能示例是:

protected Counter is
   procedure Increment;
   entry Read(Num : out Natural);
private
   Tally : Natural := 0;
   Is_Updated : Boolean := False;
end Counter;

protected body Counter is
   procedure Increment is
   begin
      Tally := Tally + 1;
      Is_Updated := True;
   end Increment;

   entry Read(Num : out Natural) when Is_Updated is
   begin
      Num := Tally;
      Is_Updated := False;
   end Read;
end Counter;

写入程序在完成其外部循环后调用Counter.Increment。读卡器调用Counter.Read并在新值可用时立即获取它。

从屏幕截图中显示的内容来看,我认为您正在寻找一种方法来配置任务/工作人员Id、患者,然后在任务/工作人员启动后,监视其工作完成进度

假设上述情况,并考虑到Jim Rogers已经做出的重要标记,了解任务通常是通过所谓的任务鉴别而不是初始接受条目来配置的可能会很有趣。任务鉴别器可以从任务外部访问,请参见,并且根据定义是不可变的。因此,在您的情况下,您可以定义两个任务判别式,如中所示

task type T_My_Task
   (Worker_Id : Natural;
    Patient   : Boolean);
可以用作

declare
  T1 : T_My_Task  (1, True);
  T2 : T_My_Task  (2, True);
begin

  Put_Line (T1.Worker_Id'Image);
  Put_Line (T1.Patient'Image);

  Put_Line (T2.Worker_Id'Image);
  Put_Line (T2.Patient'Image);

end;
出于我自己的兴趣,我试着根据我认为你们可以如何处理这个问题来制作一些示例程序。我很确定还有更多的方法,我不会说这是最好的方法。我加入了一些评论,以澄清我试图做的事情。它可能对你有用

注意:我更新了下面的例子,关于我最初发布的内容,因为我对最初的设计不满意

main.adb主程序

with Ada.Text_IO; use Ada.Text_IO;
with Work_Force;  use Work_Force;

procedure Main is

   Worker_Configs : T_Worker_Config_Array :=
     (0 => (Patient => True ),
      1 => (Patient => False),
      2 => (Patient => False),
      3 => (Patient => True ),
      4 => (Patient => False));

   package My_Work_Force is
     new Generic_Work_Force (Worker_Configs);
   use My_Work_Force;

   User_Response : Character;

begin

   Main_Loop: loop

      Put_Line ("--==[ MENU ]==--");
      New_Line;
      Put_Line (" S - Show status workers");
      Put_Line (" Q - Quit");
      New_Line;
      Put (" ==> "); Get (User_Response);
      New_Line;

      case User_Response is

         when 'S' =>

            for Worker_Id in T_Worker_Id'Range loop
               declare
                  Worker_Config : T_Worker_Config :=
                    Get_Worker_Config (Worker_Id);
                  Worker_Status : T_Worker_Status :=
                    Get_Worker_Status (Worker_Id);
               begin
                  Put_Line (" Worker. . . : " & Worker_Id'Image);
                  Put_Line (" Patient . . : " & Worker_Config.Patient'Image);
                  Put_Line (" Jobs_Done . : " & Worker_Status.Jobs_Done'Image);
                  New_Line;
               end;
            end loop;

         when 'Q' =>

            --  The call "Terminate_All_Workers" is actually a request. The
            --  call is non-blocking thanks to the protected object "Manager"
            --  in the "Work_Force" package. The program itself, however,
            --  will not terminate until all tasks have terminated (i.e.
            --  exited the "Worker_Loop" (see task body of "T_Worker").

            My_Work_Force.Terminate_All_Workers;
            Put_Line ("Program will end when all workers have terminated.");
            New_Line;
            exit Main_Loop;

         when others =>

            Put_Line ("(Unknown option)");
            New_Line;

      end case;

   end loop Main_Loop;

end Main;
劳动力ads包规格;现在想不出更好的名字了

package Work_Force is

   --  Record type "T_Worker_Config" contains all items required to configure
   --  a worker. For now it contains only 1 item, but this can easily be
   --  extended.

   type T_Worker_Config is
      record
         Patient : Boolean;
      end record;

   type T_Worker_Config_Array is
     array (Natural range <>) of aliased T_Worker_Config;


   --  Record type "T_Worker_Status" contains all items related tot the status
   --  of the worker. As for type "T_Worker_Config", it now contains only 1
   --  item, but this can easily be extended.

   type T_Worker_Status is
      record
         Jobs_Done : Natural;
      end record;


   generic
      Workers_Config : T_Worker_Config_Array;      
   package Generic_Work_Force is

      --  The package "Generic_Work_Force" exposes a restricted set of
      --  subprograms such to have proper interface/implementation decoupling
      --  (and reduce the impact of changes in the implementation, etc.).

      subtype T_Worker_Id is
        Natural range Workers_Config'Range;

      function Get_Worker_Config
        (Worker_Id : T_Worker_Id) return T_Worker_Config;
      --  Gets the configuration from the worker "Worker_Id". A call to
      --  this subprogram is non-blocking.

      --  NOTE: "Get_Worker_Config" is not strictly necessary as the
      --  "Workers_Config" parameter of the generic package can be accessed
      --  directly from outside the package, but it looks nice and symmetric
      --  with respect to the subprogram "Get_Worker_Status" below.

      function Get_Worker_Status
        (Worker_Id : T_Worker_Id) return T_Worker_Status;
      --  Gets the status from the worker "Worker_Id". A call to this
      --  subprogram is non-blocking.

      procedure Terminate_All_Workers;
      --  Request to terminate all workers. A call to this subprogram is
      --  non-blocking.

      --  NOTE: there is no private part in this spec as there are no
      --  private types defined in the public part of the spec. All
      --  implementation details can reside in the body such that they
      --  can change independently from the spec, if necessary, without 
      --  the need to touch the spec file.

   end Generic_Work_Force;

end Work_Force;

从您在屏幕截图中显示的内容来看,在我看来,您正在寻找一种方法来配置任务/工作人员Id、患者,然后在任务/工作人员启动后,再寻找一种方法来监控其完成工作的进度

假设上述情况,并考虑到Jim Rogers已经做出的重要标记,了解任务通常是通过所谓的任务鉴别而不是初始接受条目来配置的可能会很有趣。任务鉴别器可以从任务外部访问,请参见,并且根据定义是不可变的。因此,在您的情况下,您可以定义两个任务判别式,如中所示

task type T_My_Task
   (Worker_Id : Natural;
    Patient   : Boolean);
可以用作

declare
  T1 : T_My_Task  (1, True);
  T2 : T_My_Task  (2, True);
begin

  Put_Line (T1.Worker_Id'Image);
  Put_Line (T1.Patient'Image);

  Put_Line (T2.Worker_Id'Image);
  Put_Line (T2.Patient'Image);

end;
出于我自己的兴趣,我试着根据我认为你们可以如何处理这个问题来制作一些示例程序。我很确定还有更多的方法,我不会说这是最好的方法。我加入了一些评论,以澄清我试图做的事情。它可能对你有用

注意:我更新了下面的例子,关于我最初发布的内容,因为我对最初的设计不满意

main.adb主程序

with Ada.Text_IO; use Ada.Text_IO;
with Work_Force;  use Work_Force;

procedure Main is

   Worker_Configs : T_Worker_Config_Array :=
     (0 => (Patient => True ),
      1 => (Patient => False),
      2 => (Patient => False),
      3 => (Patient => True ),
      4 => (Patient => False));

   package My_Work_Force is
     new Generic_Work_Force (Worker_Configs);
   use My_Work_Force;

   User_Response : Character;

begin

   Main_Loop: loop

      Put_Line ("--==[ MENU ]==--");
      New_Line;
      Put_Line (" S - Show status workers");
      Put_Line (" Q - Quit");
      New_Line;
      Put (" ==> "); Get (User_Response);
      New_Line;

      case User_Response is

         when 'S' =>

            for Worker_Id in T_Worker_Id'Range loop
               declare
                  Worker_Config : T_Worker_Config :=
                    Get_Worker_Config (Worker_Id);
                  Worker_Status : T_Worker_Status :=
                    Get_Worker_Status (Worker_Id);
               begin
                  Put_Line (" Worker. . . : " & Worker_Id'Image);
                  Put_Line (" Patient . . : " & Worker_Config.Patient'Image);
                  Put_Line (" Jobs_Done . : " & Worker_Status.Jobs_Done'Image);
                  New_Line;
               end;
            end loop;

         when 'Q' =>

            --  The call "Terminate_All_Workers" is actually a request. The
            --  call is non-blocking thanks to the protected object "Manager"
            --  in the "Work_Force" package. The program itself, however,
            --  will not terminate until all tasks have terminated (i.e.
            --  exited the "Worker_Loop" (see task body of "T_Worker").

            My_Work_Force.Terminate_All_Workers;
            Put_Line ("Program will end when all workers have terminated.");
            New_Line;
            exit Main_Loop;

         when others =>

            Put_Line ("(Unknown option)");
            New_Line;

      end case;

   end loop Main_Loop;

end Main;
劳动力ads包规格;现在想不出更好的名字了

package Work_Force is

   --  Record type "T_Worker_Config" contains all items required to configure
   --  a worker. For now it contains only 1 item, but this can easily be
   --  extended.

   type T_Worker_Config is
      record
         Patient : Boolean;
      end record;

   type T_Worker_Config_Array is
     array (Natural range <>) of aliased T_Worker_Config;


   --  Record type "T_Worker_Status" contains all items related tot the status
   --  of the worker. As for type "T_Worker_Config", it now contains only 1
   --  item, but this can easily be extended.

   type T_Worker_Status is
      record
         Jobs_Done : Natural;
      end record;


   generic
      Workers_Config : T_Worker_Config_Array;      
   package Generic_Work_Force is

      --  The package "Generic_Work_Force" exposes a restricted set of
      --  subprograms such to have proper interface/implementation decoupling
      --  (and reduce the impact of changes in the implementation, etc.).

      subtype T_Worker_Id is
        Natural range Workers_Config'Range;

      function Get_Worker_Config
        (Worker_Id : T_Worker_Id) return T_Worker_Config;
      --  Gets the configuration from the worker "Worker_Id". A call to
      --  this subprogram is non-blocking.

      --  NOTE: "Get_Worker_Config" is not strictly necessary as the
      --  "Workers_Config" parameter of the generic package can be accessed
      --  directly from outside the package, but it looks nice and symmetric
      --  with respect to the subprogram "Get_Worker_Status" below.

      function Get_Worker_Status
        (Worker_Id : T_Worker_Id) return T_Worker_Status;
      --  Gets the status from the worker "Worker_Id". A call to this
      --  subprogram is non-blocking.

      procedure Terminate_All_Workers;
      --  Request to terminate all workers. A call to this subprogram is
      --  non-blocking.

      --  NOTE: there is no private part in this spec as there are no
      --  private types defined in the public part of the spec. All
      --  implementation details can reside in the body such that they
      --  can change independently from the spec, if necessary, without 
      --  the need to touch the spec file.

   end Generic_Work_Force;

end Work_Force;

是否可以将屏幕截图中显示的代码的相关部分复制到上述问题中。屏幕截图中的代码几乎不可读。屏幕截图中的代码一点也不清晰。@SimonWright:很遗憾,我最初的编辑未能使图像可点击;看,哇
如屏幕截图所示,是否可以将代码的相关部分复制到上述问题中。屏幕截图中的代码几乎不可读。屏幕截图中的代码一点也不清晰。@SimonWright:很遗憾,我最初的编辑未能使图像可点击;看见