2016-12-21 11 views
3

私はタスクBを呼び出すタスクAを持っています。
Bタスクが終了しましたが、タスクAはそのことを認識せず、呼び出しは今終了したタスクBです。
タスクBはタスクAから呼び出して終了しているので、タスクAを終了します。Adaでタスクが終了しているかどうかを確認する方法

タスクBがタスクAから終了しているかどうかを確認するにはどうすればよいですか?

私はGNAT Programming Studioを使用しています。
終了する前にBで設定されたグローバル変数を作成することができましたが、Bの状態がAであることがわかります。

答えて

5

次の例では、一連のタスクが完了したときに、メインタスクが決定できるようにするために「終端属性を使用していますRM 9.9

if not B'Terminated then 
    -- do something 
end if; 
0

を参照してください、B'Terminatedを確認することができます。

------------------------------------------------------------------ 
-- Parallel Array Filling -- 
------------------------------------------------------------------ 
with Ada.Text_IO; use Ada.Text_IO; 
with Ada.Numerics.Discrete_Random; 
with Ada.Calendar; use Ada.Calendar; 

procedure Array_Fill is 
    Start_Time : Time := Clock; 
    Num_Tasks : constant := 8; 
    Slice_Size : constant Natural := 8_388_608; 
    Max_Size : constant Natural := Num_Tasks * Slice_Size -1; 
    subtype Values is Natural range 0..10_000; 

    type Big_Array is array(Natural range 0..Max_Size) of Integer; 
    type Big_Access is access Big_Array; 

    -- Instantiate the generic package Ada.Numerics.Discrete_Random 
    -- to produce random numbers in the range of the subtype Values 
    -- defined above 
    package Rand_Nums is new Ada.Numerics.Discrete_Random(Values); 
    use Rand_Nums; 

    -- dynamically allocate the integer array, initializing all array 
    -- elements to the lowest valid value for type Integer 
    The_Array : Big_Access := new Big_Array'(Others => Integer'First); 

    task type Filler is 
     -- Set_Low is a synchronization point between the main task and 
     -- an instance of the task type Filler. This point allows the main 
     -- task to send an integer value >= 0 directly to the Filler task 
     Entry Set_Low(Item : in Natural); 
    end Filler; 

    task body Filler is 
     Id   : Natural; 
     Low_Index : Natural; 
     High_Index : Natural; 
     Seed  : Generator; 
    begin 
     Reset(Seed); 

     -- the Filler task waits for the main task to send a value to the 
     -- entry Set_Low. 
     accept Set_Low(Item : in Natural) do 
     Id := Item; 
     end Set_Low; 

     Low_Index := Id * Slice_Size; 
     High_Index := Low_Index + (Slice_Size - 1); 

     for I in Low_Index..High_Index loop 
     The_Array(I) := Random(Seed); 
     end loop; 
    end Filler; 

    -- create an array type containing Num_Tasks of Filler tasks; 
    type Task_List is array(Natural range 0..Num_tasks - 1) of Filler; 

    -- Create an instance of the task array. The tasks start as soon 
    -- as this array is created 
    List  : Task_List; 

begin -- begin the execution of the main task 
    -- Send ID values to each instance of the Filler task in the array 
    -- List. 
    for I in Task_List'Range loop 
     List(I).Set_Low(I); 
    end loop; 

    loop -- waits for all the tasks to terminate 
     if List(0)'Terminated and then 
     List(1)'Terminated and then 
     List(2)'Terminated and then 
     List(3)'Terminated and then 
     List(4)'Terminated and then 
     List(5)'Terminated and then 
     List(6)'Terminated and then 
     List(7)'Terminated then 
     exit; -- exit this loop 
     end if; 
    end loop; 

    Put_Line("Elapsed time " & Duration'Image(Clock - Start_Time) & " seconds"); 
end Array_Fill; 
関連する問題