From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=0.2 required=5.0 tests=BAYES_00,INVALID_MSGID, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,701d3ed4f915aaf8 X-Google-Attributes: gid103376,public From: ncohen@watson.ibm.com (Norman H. Cohen) Subject: Re: Tasks vs Task Types Date: 1996/05/07 Message-ID: <4mofak$vtc@watnews1.watson.ibm.com>#1/1 X-Deja-AN: 153582633 distribution: world references: <4mnra4$qc6@gde.GDEsystems.COM> organization: IBM T.J. Watson Research Center reply-to: ncohen@watson.ibm.com newsgroups: comp.lang.ada Date: 1996-05-07T00:00:00+00:00 List-Id: In article <4mnra4$qc6@gde.GDEsystems.COM>, Michael Levasseur writes: |> What is the difference between Tasks and Task Types? A task is a thread of control. ---- Every Ada task (except the one that invokes the main subprogram) is associated with a variable called a task object. ----------- Like every Ada object, a task object belongs to a type, called a task type. --------- Tasks associated with task objects of the same type have identical sets of entries for intertask communication and execute the same task body (each with its own copy of the variables declared inside the task body). (The terms "task" and "task object" are often used loosely as if they were synonymous, which can create confusion.) The task type declaration task type Line_Serializer_Type is entry Open (File_Name: in String); entry Put_Line (Line: in String); entry Close; end Line_Serializer_Type; declares a task type Line_Serializer_Type. Given this declaration, we can declare task objects such as the following: File_1_Serializer : Line_Serializer_Type; File_2_Serializer : Line_Serializer_Type; Each declaration creates a task object and a task. Each task starts executing the task body. A task-type declaration must be accompanied by a task body, such as the following: task body Line_Serializer_Type is use Ada.Text_IO; File : File_Type; Opened : Boolean; begin loop -- Task is in the "closed" state. It can accept a call on -- Open, thus moving to the "opened" state, or terminate. -- A call on Open with a bad file name propagates an -- exception to the caller and leaves the task in the -- "closed" state. begin select accept Open (File_Name: in String) do Create (File, Out_File, File_Name); Opened := True; end Open; or terminate; end select; exception when others => Opened := False; end; if Opened then -- Task is in the "opened" state. It can accept a call on -- Close, thus moving to the "closed" state, or on -- Put_Line. loop select accept Put_Line (Line: in String) do Put_Line (File, Line); end Put_Line; or accept Close; Close (File); exit; end select; end loop; end if; end loop; end Line_Serializer_Type; Often it is desired to create only one task belonging to a particular task type. In that case, there is a notational shorthand: If you leave the word "type" out of the declaration, it declares both an anonymous type and a single task object belonging to that type: task Line_Serializer is entry Open (File_Name: in String); entry Put_Line (Line: in String); entry Close; end Line_Serializer; This is equivalent to having written task type Line_Serializer_Type is -- (as above) end Line_Serializer_Type; Line_Serializer: Line_Serializer_Type; except that the declared task type doesn't really have a name. -- Norman H. Cohen ncohen@watson.ibm.com