Skip to content
Tutorials Center
My learning

Topics

Lesson 22 of 36 ยท Tasking and Concurrency

Select, Delay and Termination

Learn Ada's selective accept: select with several accepts, guards, delay alternatives, the terminate alternative, abort and priorities, with runnable C stand-ins.

A server task often has to wait for more than one kind of request, give up after a while, and shut down cleanly when its clients are gone. Ada packs all three needs into one statement: select. This lesson covers selective accept with guards, the delay and terminate alternatives, abort and task priorities.

As before, the Ada code is read-only on this site because no Ada compiler runs here. Runnable parts are C stand-ins in POSIX threads.

Selective accept

A select inside a task body offers several accept alternatives at once. Whichever has a waiting caller first is served.

ADA
task Server is
   entry Add (N : Integer);
   entry Report (Total : out Integer);
end Server;

task body Server is
   Sum : Integer := 0;
begin
   loop
      select
         accept Add (N : Integer) do
            Sum := Sum + N;
         end Add;
      or
         accept Report (Total : out Integer) do
            Total := Sum;
         end Report;
      or
         terminate;
      end select;
   end loop;
end Server;

If both entries have callers queued, the language does not say which is picked first; the choice is arbitrary but fair enough that no entry starves in practice. Code that depends on the choice is a bug.

Guards

Each alternative can start with when Condition =>. A closed alternative is not considered this round.

ADA
select
   when Count < Max =>
      accept Put (X : Integer) do ... end Put;
or
   when Count > 0 =>
      accept Get (X : out Integer) do ... end Get;
end select;

All guards are evaluated once, when the select is reached, not continuously. If every guard is false and there is no else, delay or terminate alternative, Program_Error is raised. Guards on a task entry resemble the barriers of a protected object, but protected barriers are re-checked as the state changes, which makes them more flexible for most shared-data problems.

Inside a select

accept serve a waiting caller
delay give up after a time
terminate stop when clients are gone
when guard closes an arm

Only one alternative is chosen per pass.

1 / 2

The delay alternative

Add or delay to wake up if nothing happens in time:

ADA
select
   accept Heartbeat;
   Missed := 0;
or
   delay 1.0;
   Missed := Missed + 1;
   Put_Line ("no heartbeat");
end select;

This is a watchdog: if a client does not call Heartbeat within a second, the task counts a miss. delay until (with Ada.Calendar or Ada.Real_Time times) schedules periodic work without drift, which is why real-time Ada programs use Ada.Real_Time with delay until Next_Time.

The terminate alternative

A server in an endless loop would keep the program alive forever. The terminate alternative solves that: it is chosen when the server's master, and every other task depending on that master, is finished or waiting at a terminate alternative too. The server then stops silently. It is the standard way to let service tasks end when all clients are done, and it can only be combined with accept alternatives, not with delay.

Abort and priorities

abort Task_Name; stops a task immediately, running no handlers in the task body. It is a last resort: the task may be killed in the middle of updating data. Prefer cooperative ending via terminate or an entry such as Stop. For real-time work, pragma Priority (n) (or Priority => aspects) gives tasks and protected objects priorities, and a higher-priority ready task preempts a lower one. The Ravenscar profile restricts tasking to a small, analyzable subset (no dynamic task creation, no select with delay) for safety-critical systems; see SPARK and the tasking subset.

Stand-in: a timed wait (C, runnable)

select ... or delay has a close C relative: waiting on a condition variable with a deadline. The worker below posts its result after 20 ms. The first wait uses a 500 ms limit (success); the second, with nothing posted, uses 50 ms and times out.

C C stand-in: wait with a timeout

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Stand-in: terminate when clients finish (C, runnable)

The server loop below runs until the number of active clients drops to zero, then leaves, the idea behind terminate.

C C stand-in: server stops when clients are gone

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. When are the guards of a select statement evaluated?

    Guards are evaluated on entry to the select; a closed alternative stays closed for that pass.
  2. 2. What does the terminate alternative do?

    It is the clean way to end service tasks once no client can call them any more.
  3. 3. Which is the main reason to avoid abort?

    abort gives the task no chance to finish its update; use a Stop entry or terminate instead.
  4. 4. What is raised if all guards are false and there is no else, delay or terminate alternative?

    A select with no open alternative is a program error.

Summary

  • select lets a task wait for several entries at once; the choice among ready ones is arbitrary.
  • when guards are evaluated once on entering the select.
  • or delay gives timeouts and watchdogs; delay until gives drift-free periodic work.
  • terminate ends service tasks when no client can call them any more.
  • Prefer cooperative shutdown to abort.

Next: Exceptions in Ada