Richard Waller

Tasks

A task unit is a program unit that is obeyed concurrently with the rest of an Ada program. The corresponding activity, a new locus of control, is called a task in Ada terminology, and is similar to a thread, for example in Java Threads. The execution of the main program is also a task, the anonymous environment task. A task unit has both a declaration and a body, which is mandatory. A task body may be compiled separately as a subunit, but a task may not be a library unit, nor may it be generic. Every task depends on a master, which is the immediately surrounding declarative region - a block, a subprogram, another task, or a package. The execution of a master does not complete until all its dependent tasks have terminated. The environment task is the master of all other tasks; it terminates only when all other tasks have terminated.

Task units are similar to packages in that a task declaration defines entities exported from the task, whereas its body contains local declarations and statements of the task.

A single task is declared as follows:

task Single is  
     declarations of exported identifiers  
  end Single;  
  ...  
  task body Single is  
     local declarations and statements  
  end Single;  

A task declaration can be simplified, if nothing is exported, thus:

task No_Exports;  

  procedure Housekeeping is  
  
     task Check_CPU;  
     task Backup_Disk;  
  
     task body Check_CPU is  
        ...  
     end Check_CPU;  
  
     task body Backup_Disk is  
        ...  
     end Backup_Disk;  
     -- the two tasks are automatically created and begin execution  
  begin -- Housekeeping  
     null;  
     -- Housekeeping waits here for them to terminate  
  end Housekeeping;  

It is possible to declare task types, thus allowing task units to be created dynamically, and incorporated in data structures:

task type T is  
  ...  
end T;  
  ...  
Task_1, Task_2 : T;  
  ...  
  task body T is  
     ...  
end T;  

Task types are limited, i.e. they are restricted in the same way as limited private types, so assignment and comparison are not allowed.