Ada tasks
last modified November 23, 2026
A task is a piece of a program that runs at the same time as the rest. Ada declares tasks like objects and starts them automatically, and the language defines when a task finishes and who waits for it. The examples here show the life cycle, the timing statements, and a few tasks working on separate data.
A task and its body
Worker is declared in the declarative part of the main
procedure, and its body follows in the same part. The task is activated when
the declarative part is elaborated, before the main procedure reaches its
begin. The small delay in the body lets the main procedure print
first, which makes the interleaving visible.
with Ada.Text_IO;
use Ada.Text_IO;
procedure Tasks_Main is
task Worker;
task body Worker is
begin
delay 0.1;
Put_Line ("worker: start");
delay 0.2;
Put_Line ("worker: done");
end Worker;
begin
Put_Line ("main: waiting");
delay 1.0;
Put_Line ("main: done");
end Tasks_Main;
delay 0.1 suspends the task for a tenth of a second, and
delay 1.0 keeps the main procedure alive long enough for the task
to finish. A task does not run in a separate program: when the main procedure
reaches its end, it waits for the tasks it declared, so the program ends after
both lines of the task have been printed.
$ gnatmake -gnat2022 -gnatwa tasks_main.adb $ ./tasks_main main: waiting worker: start worker: done main: done
Task types and discriminants
A task type describes a family of tasks. Each object of the type is a separate task, and a discriminant gives each object its own identity. The three objects below are elaborated one after another and start running concurrently.
with Ada.Text_IO;
use Ada.Text_IO;
procedure Workers_Main is
Results : array (1 .. 3) of Natural := [others => 0];
task type Worker (Id : Positive);
task body Worker is
begin
delay Duration (Id) * 0.1;
Results (Id) := Id * Id;
end Worker;
A : Worker (1);
B : Worker (2);
C : Worker (3);
begin
delay 0.5;
for R of Results loop
Put_Line ("result =" & Natural'Image (R));
end loop;
end Workers_Main;
Each worker waits for a time that depends on its Id and then
stores its square in its own slot of Results. The slots are
disjoint, so the tasks can write without any synchronization. The main
procedure waits half a second and then prints the whole array in order.
$ gnatmake -gnat2022 -gnatwa workers_main.adb $ ./workers_main result = 1 result = 4 result = 9
Delay and delay until
delay takes a duration and suspends the task for that long.
delay until takes an absolute time, which is useful for periodic
work: each beat is scheduled relative to the same starting point, so the
intervals do not drift by the time spent printing. The clock comes from
Ada.Calendar; the calendar types get their own article later, and
here Clock and Time only schedule the beats.
with Ada.Text_IO;
with Ada.Calendar;
use Ada.Text_IO;
use Ada.Calendar;
procedure Timed_Main is
Start : constant Ada.Calendar.Time := Ada.Calendar.Clock;
task Heart;
task body Heart is
begin
for I in 1 .. 3 loop
Put_Line ("beat" & Positive'Image (I));
delay until Start + Duration (I) * 0.2;
end loop;
end Heart;
task Watchdog;
task body Watchdog is
begin
delay 1.0;
Put_Line ("watchdog: enough");
end Watchdog;
begin
delay 1.2;
Put_Line ("main: done");
end Timed_Main;
Start is read once, and the heart waits until
Start + Duration (I) * 0.2 before each beat. The conversion
Duration (I) turns the loop index into the fixed-point type that
delay expects. The watchdog fires once at 1.0 seconds, and the
main procedure ends at 1.2 seconds.
$ gnatmake -gnat2022 -gnatwa timed_main.adb $ ./timed_main beat 1 beat 2 beat 3 watchdog: enough main: done
Task hierarchies
A task body has its own declarative part, so it can declare tasks of its own. The outer task becomes the master of the inner one: it does not finish until its dependent task has finished. The same rule applies to the main procedure, which is the master of the tasks declared directly in it.
with Ada.Text_IO;
use Ada.Text_IO;
procedure Master_Main is
task Outer;
task body Outer is
task Inner;
task body Inner is
begin
delay 0.2;
Put_Line ("inner: done");
end Inner;
begin
delay 0.1;
Put_Line ("outer: done");
end Outer;
begin
Put_Line ("main: statements done");
end Master_Main;
The main procedure prints its line at once, the outer task prints after a short delay, and the inner task finishes 0.2 seconds after it was created. The program ends only after the last of them, even though the main procedure had no statements left.
$ gnatmake -gnat2022 -gnatwa master_main.adb $ ./master_main main: statements done outer: done inner: done
An applied example: parallel sums
The last program divides the numbers from 1 to 400 into four slices and gives one slice to each task. Every task computes the sum of its slice and stores it in its own slot. The main procedure waits and then adds the four partial results.
with Ada.Text_IO;
use Ada.Text_IO;
procedure Sums_Main is
Slice : constant := 100;
Results : array (1 .. 4) of Natural := [others => 0];
task type Summer (Part : Positive);
task body Summer is
First : constant Positive := (Part - 1) * Slice + 1;
Total : Natural := 0;
begin
for N in First .. First + Slice - 1 loop
Total := Total + N;
end loop;
Results (Part) := Total;
end Summer;
A : Summer (1);
B : Summer (2);
C : Summer (3);
D : Summer (4);
begin
delay 0.5;
for R of Results loop
Put_Line ("sum =" & Natural'Image (R));
end loop;
Put_Line ("grand total =" & Natural'Image
(Results (1) + Results (2) + Results (3) + Results (4)));
end Sums_Main;
The four sums are 5050, 15050, 25050, and 35050, and the grand total is 80200. The delay in the main procedure is only a convenient wait; it is not synchronization. A slower task would leave its slot at zero when the main procedure printed it. The next article replaces the delay with entries, which let a caller wait for a task to reach a particular point.
$ gnatmake -gnat2022 -gnatwa sums_main.adb $ ./sums_main sum = 5050 sum = 15050 sum = 25050 sum = 35050 grand total = 80200
Next steps
Tasks run on their own, but the examples above only touch data that no other task reads at the same time. The next article introduces entries and the rendezvous, the mechanism that lets a task and its caller meet at a known point and exchange data safely.
Source
In this article we have declared a task and its body, created task types with
discriminants, scheduled work with delay and
delay until, nested tasks inside tasks, and split a computation
across four workers. The records article explains
the components that a task type can add after its discriminants.
Author
List all Ada tutorials.