comp.lang.ada
 help / color / mirror / Atom feed
From: ncohen@watson.ibm.com (Norman H. Cohen)
Subject: Re: Tasks vs Task Types
Date: 1996/05/07
Date: 1996-05-07T00:00:00+00:00	[thread overview]
Message-ID: <4mofak$vtc@watnews1.watson.ibm.com> (raw)
In-Reply-To: 4mnra4$qc6@gde.GDEsystems.COM


In article <4mnra4$qc6@gde.GDEsystems.COM>, Michael Levasseur
<levass@gdesystems.com> 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




  reply	other threads:[~1996-05-07  0:00 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1996-05-07  0:00 Tasks vs Task Types Michael Levasseur
1996-05-07  0:00 ` Norman H. Cohen [this message]
1996-05-07  0:00   ` Robert I. Eachus
1996-05-08  0:00 ` David Tannen
     [not found] <3191319D.2781E494@escmail.orl.mmc.com>
1996-05-10  0:00 ` David Tannen
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox