Ada 95 Quality and Style Guide Chapter 10

Chapter 10: Improving Performance - TOC - 10.6 TYPES

10.6.2 Protected Types

guideline

  • For mutual exclusion, when measured performance indicates, use protected types as an alternative to tasking rendezvous.
  • To implement an interrupt handler, when performance measurement indicates, use a protected procedure.

  • example

       -- (1) Using protected objects
       --     (See ACES V2.0, test "a9_pt_prot_access_02")
       protected Object is
          function Read return Float;
          procedure Write (Value : in Float);
       private
          Data : Float;
       end Object;
       protected body Object is
          function Read return Float is
          begin
             return Data;
          end Read;
          procedure Write (Value : in Float) is
          begin
             Data := Value;
          end Write;
       end Object;
       task type Modify is
       end Modify;
       type Mod_Bunch is array (1 .. 5) of Modify;
       task body Modify is
          ...
       begin -- Modify
          for I in 1 .. 200 loop
             The_Value := Object.Read;
             Object.Write (The_Value - 0.125);
             if The_Value < -1.0E7 then
                The_Value := 1.0;
             end if;
          end loop;
       end Modify;
       ...
       -- Block statement to be timed
       declare
          Contending_Tasks : array (1 .. 5) of Modify;
       begin
          null;  -- 5 tasks contend for access to protected data
       end;
       ------------------------------------------------------------------------------
       -- (2) Using monitor task
       --     (See ACES V2.0, test "tk_rz_entry_access_02")
       Task Object is
          entry Write (Value : in     Float);
          entry Read  (Value :    out Float);
       end Object;
       task body Object is
          Data : Float;
       begin -- Object
          loop
             select
                accept Write (Value : in     Float) do
                   Data := Value;
                end Write;
             or
                accept Read  (Value :    out Float) do
                   Value := Data;
                end Read;
             or
                terminate;
             end select;
          end loop;
       end Object;
       -- Task type Modify declared as above
       -- Block statement to be timed as above
    

    rationale

    Protected objects are meant to be much faster than tasks used for the same purpose (see Guideline 6.1.1). Determine the impact of using protected objects to provide access safely to encapsulated data in a concurrent program.


    < Previous Page Search Contents Index Next Page >
    1 2 3 4 5 6 7 8 9 10 11
    TOC TOC TOC TOC TOC TOC TOC TOC TOC TOC TOC
    Appendix References Bibliography