Ada Example : multi tasking
This example shows how Ada language fully integrates multitask programming.
Most of the complexity in multitasking systems is the way of sharing ressource.
Here, Ada is inventive and defers slightly : The returned value of a Task is truly safe.
Computes zetha(2) S = Sum 1/k^2 with k in [1 .. n] = 1.6449 = pi^2 /6
with Ada.Text_IO; use Ada.Text_IO; with Ada.Task_Identification; procedure Main is Ct_Nb_TOp : constant := 100000000; Ct_Nb_Tack : constant := 10; Ct_Nb_Op : constant := Ct_Nb_TOp/Ct_Nb_Tack; pragma PRIORITY (1); task type Task_A is entry Init_A( Val : in integer ); entry End_A ( S : in out Long_Float ); end Task_A; task body Task_A is VA : integer := 0; Sum : Long_Float := 0.0 ; begin accept Init_A( Val : in integer ) do VA:=Val; Put_line("Init_A " & VA'Img); end Init_A; Put_line("Do_A" & VA'Img); for I in VA .. VA+ (Ct_Nb_Op-1) loop Sum := Sum + 1.0/(Long_Float(I)*Long_Float(I)); end loop; accept End_A (S : in out Long_Float ) do S:=Sum+S; Put_line("End_A" & VA'Img); end End_A; end Task_A; A_Array : array (0.. Ct_Nb_Tack) of Task_A; Global_Sum : Long_Float := 0.0 ; begin Put_line("Main"); for I in 0 .. Ct_Nb_Tack loop Put_line(" " & Ada.Task_Identification.Image(A_Array(I)'Identity)); A_Array(I).Init_A(I*Ct_Nb_Op+1); end loop; for I in reverse 0 .. Ct_Nb_Tack loop A_Array(I).End_A(Global_Sum); Put_line("End_A from main" & Global_Sum'Img); end loop; end Main;