수색…


통사론

  • 작업 Task_Name;
  • 작업 Task_Name은 항목 끝입니다.
  • 태스크 본문 Task_Name은 선언입니다 begin 코드 종료;

하나의 간단한 작업

with Ada.Text_IO; use Ada.Text_IO;

procedure Main is
   task My_Task;
   task body My_Task is
   begin
      Put_Line ("Hello from My_Task");
   end;
begin
   Put_Line ("Hello from Main");
end;

결과

Put_Line 의 순서는 Put_Line 수 있습니다.

Hello from My_Task
Hello from Main

하나의 간단한 작업과 하나의 루프

with Ada.Text_IO; use Ada.Text_IO;

procedure Main is
   task My_Task;
   task body My_Task is
   begin
      for I in 1 .. 4 loop
         Put_Line ("Hello from My_Task");
      end loop;
   end;
begin
   Put_Line ("Hello from Main");
end;

결과

Put_Line 의 순서는 Put_Line 수 있습니다.

Hello from My_Task
Hello from Main
Hello from My_Task
Hello from My_Task
Hello from My_Task

하나의 간단한 작업과 두 개의 루프

with Ada.Text_IO; use Ada.Text_IO;

procedure Main is
   task My_Task;
   task body My_Task is
   begin
      for I in 1 .. 4 loop
         Put_Line ("Hello from My_Task");
      end loop;
   end;
begin
   for I in 1 .. 4 loop
      Put_Line ("Hello from Main");
   end loop;
end;

결과

Put_Line 의 순서는 Put_Line 수 있습니다.

Hello from My_Task
Hello from My_Task
Hello from Main
Hello from My_Task
Hello from Main
Hello from My_Task
Hello from Main
Hello from Main

2 개의 간단한 작업과 2 개의 루프

with Ada.Text_IO; use Ada.Text_IO;

procedure Main is
   task My_Task_1;
   task My_Task_2;
   
   task body My_Task_1 is
   begin
      for I in 1 .. 4 loop
         Put_Line ("Hello from My_Task_1");
      end loop;
   end;
   
   task body My_Task_2 is
   begin
      for I in 1 .. 4 loop
         Put_Line ("Hello from My_Task_2");
      end loop;
   end;
begin
   null;
end;

결과

Put_Line 의 순서는 Put_Line 수 있습니다.

Hello from My_Task_1
Hello from My_Task_1
Hello from My_Task_2
Hello from My_Task_1
Hello from My_Task_2
Hello from My_Task_1
Hello from My_Task_2
Hello from My_Task_2

입력 후 숫자를 증가시키는 작업

사용자가 호출 할 수 Incrementor.Increment K 내에서 키를 눌러 횟수를 '0' .. '9' 하고는 전화를 가능 Incrementor.Increment 빨리 이상의 task Incrementor 증가 할 수 I .

with Ada.Text_IO;
with Ada.Integer_Text_IO;

procedure Main is
   use Ada.Text_IO;
   task Incrementor is
      entry Increment;
   end;
   task body Incrementor is
      use Ada.Integer_Text_IO;
      I : Integer := 0;
   begin
      loop
         accept Increment;
         I := I + 1;
         Put (I, 0);
         delay 0.1;
      end loop;
   end;
   K : Character;
begin
   loop
      Get_Immediate (K);
      if K in '0' .. '9' then
         for I in 1 .. Natural'Value (K & "") loop
            Incrementor.Increment;
         end loop;
      end if;
   end loop;
end;

인터럽트 처리

인터럽트는 매개 변수없이 보호 된 프로 시저에 의해 처리됩니다.

------------------------------------------------------------------
-- Interrupt Counting Package --
------------------------------------------------------------------
with Ada.Interrupts.Names; use Ada.Interrupts.Names;

package Ctl_C_Handling is

   protected CTL_C_Handler is
      procedure Handle_Int with
        Interrupt_Handler,
        Attach_Handler => SIGINT;
      entry Wait_For_Int;
   private
      Pending_Int_Count : Natural := 0;
   end Ctl_C_Handler;

   task CTL_Reporter is
      entry Stop;
   end CTL_Reporter;

end Ctl_C_Handling;

패키지 본문은 보호 된 프로 시저가 작동하는 방법을 보여줍니다. 이 경우, 인터럽트가 처리되는 것보다 빨리 도달하기 때문에 보호 오브젝트에 부울이 사용되지 않습니다. CTL_Reporter 태스크는 수신 된 인터럽트를 처리합니다.

with Ada.Text_IO; use Ada.Text_IO;
with Ctl_C_Handling; use CTL_C_Handling;
with Ada.Calendar; use Ada.Calendar;

package body Ctl_C_Handling is

   -------------------
   -- CTL_C_Handler --
   -------------------

   protected body CTL_C_Handler is

      ----------------
      -- Handle_Int --
      ----------------

      procedure Handle_Int is
      begin
         Pending_Int_Count := Pending_Int_Count + 1;
      end Handle_Int;

      ------------------
      -- Wait_For_Int --
      ------------------

      entry Wait_For_Int when Pending_Int_Count > 0 is
      begin
         Pending_Int_Count := Pending_Int_Count - 1;
      end Wait_For_Int;

   end CTL_C_Handler;

   ------------------
   -- CTL_Reporter --
   ------------------

   task body CTL_Reporter is
      type Second_Bin is mod 10;
      type History is array(Second_Bin) of Natural;

      ---------------------
      -- Display_History --
      ---------------------

      procedure Display_History(Item : History) is
         Sum : Natural := 0;
      begin
         for I in Item'Range loop
            Put_Line("Second: " & Second_Bin'Image(I) & " :" & Natural'Image(Item(I)));
            Sum := Sum + Item(I);
         end loop;
         Put_Line("Total count: " & Natural'Image(Sum));
         New_Line(2);
      end Display_History;

      One_Second_Count : Natural := 0;
      Next_Slot : Second_Bin := 0;
      Next_Second : Time := Clock + 1.0;
      Ten_Second_History : History := (Others => 0);

   begin
      loop
         Select
            Accept Stop;
            exit;
         else
            select
               CTL_C_Handler.Wait_For_Int;
               One_Second_Count := One_Second_Count + 1;
            or
               delay until Next_Second;
               Next_Second := Next_Second + 1.0;
               Ten_Second_History(Next_Slot) := One_Second_Count;
               Display_History(Ten_Second_History);
               Next_Slot := Next_Slot + 1;
               One_Second_Count := 0;
            end Select;
         end Select;
      end loop;
   end CTL_Reporter;
end Ctl_C_Handling;

이 패키지를 실행하는 예제 프로그램은 다음과 같습니다.

------------------------------------------------------------------
-- Ada2012 Interrupt Handler Example --
------------------------------------------------------------------
with Ada.Text_IO; use Ada.Text_IO;
with Ctl_C_Handling; use CTL_C_Handling;

procedure Interrupt01 is
begin
   Delay 40.0;
   CTL_Reporter.Stop;
   Put_Line("Program ended.");
end Interrupt01;


Modified text is an extract of the original Stack Overflow Documentation
아래 라이선스 CC BY-SA 3.0
와 제휴하지 않음 Stack Overflow