Ada 95 Quality and Style Guide Chapter 6

Chapter 6: Concurrency - TOC - 6.1 CONCURRENCY OPTIONS

6.1.5 Dynamic Tasks

guideline

  • Minimize dynamic creation of tasks because of the potentially high startup overhead; reuse tasks by having them wait for new work on some appropriate entry queue.

  • example

    The approach used in the following example is not recommended. The example shows why caution is required with dynamically allocated task and protected objects. It illustrates how a dynamic task can be disassociated from its name:

    task type Radar_Track;
    type      Radar_Track_Pointer is access Radar_Track;
    Current_Track : Radar_Track_Pointer;
    ---------------------------------------------------------------------
    task body Radar_Track is
    begin
       loop
          -- update tracking information
          ...
          -- exit when out of range
          delay 1.0;
       end loop;
    ...
    end Radar_Track;
    ---------------------------------------------------------------------
    ...
    loop
       ...
       -- Radar_Track tasks created in previous passes through the loop
       -- cannot be accessed from Current_Track after it is updated.
       -- Unless some code deals with non-null values of Current_Track,
       -- (such as an array of existing tasks)
       -- this assignment leaves the existing Radar_Track task running with
       -- no way to signal it to abort or to instruct the system to
       -- reclaim its resources.
    
       Current_Track := new Radar_Track;
       ...
    end loop;
    

    rationale

    Starting up a task has significant overhead in many implementations. If an application has a need for dynamically created tasks, the tasks should be implemented with a top-level loop so that after such a task completes its given job, it can cycle back and wait for a new job.

    You can use dynamically allocated tasks and protected objects when you need to allow the number of tasks and protected objects to vary during execution. When you must ensure that tasks are activated in a particular order, you should use dynamically allocated tasks because the Ada language does not define an activation order for statically allocated task objects. In using dynamically allocated tasks and protected objects, you face the same issues as with any use of the heap.


    < 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