ZetCode

Ada rendezvous

last modified November 23, 2026

An entry is the meeting point between a task and its caller. The caller waits, the task accepts, and the two exchange parameters during the rendezvous. This article covers the basic accept, the select statement with its alternatives, guarded entries, and a small producer-consumer pipeline.

An entry and an accept

An entry is declared in the task specification like a procedure, and the task body accepts it with accept ... do ... end. The caller of Printer.Greet blocks until the task reaches the accept, and both execute the accept body together. That meeting is called a rendezvous.

server_main.adb
with Ada.Text_IO;
use Ada.Text_IO;

procedure Server_Main is
   task Printer is
      entry Greet (Name : String);
   end Printer;

   task body Printer is
   begin
      accept Greet (Name : String) do
         Put_Line ("welcome, " & Name);
      end Greet;
   end Printer;
begin
   Printer.Greet ("Ada");
   Put_Line ("main: after the call");
end Server_Main;

The task accepts exactly one call and then reaches the end of its body. The main procedure calls the entry, prints its own line after the rendezvous, and ends. The task has already finished by then, so the program terminates normally.

$ gnatmake -gnat2022 -gnatwa server_main.adb
$ ./server_main
welcome, Ada
main: after the call

Parameters in both directions

Entry parameters have modes, just like subprogram parameters. Values of mode in are copied into the rendezvous, and values of mode out are copied back when it ends. The caller cannot continue until the task has produced the results.

divide_main.adb
with Ada.Text_IO;
use Ada.Text_IO;

procedure Divide_Main is
   task Calculator is
      entry Divide (A, B : Integer; Quotient, Remainder : out Integer);
   end Calculator;

   task body Calculator is
   begin
      accept Divide (A, B : Integer; Quotient, Remainder : out Integer) do
         Quotient  := A / B;
         Remainder := A mod B;
      end Divide;
   end Calculator;

   Q : Integer;
   R : Integer;
begin
   Calculator.Divide (47, 5, Q, R);
   Put_Line ("47 / 5 = " & Integer'Image (Q) & " remainder" & Integer'Image (R));
end Divide_Main;

Divide computes both the quotient and the remainder inside the accept body. After the rendezvous, the two out parameters hold the results and the main procedure prints them. The leading space before the 9 comes from Integer'Image.

$ gnatmake -gnat2022 -gnatwa divide_main.adb
$ ./divide_main
47 / 5 =  9 remainder 2

Select, else, and timeouts

A call to an entry waits as long as it has to. A caller that cannot wait can wrap the call in select ... else ... end select: if the rendezvous cannot start immediately, the else part runs instead. A delay alternative puts a time limit on the wait, and the first alternative that becomes possible is chosen.

select_main.adb
with Ada.Text_IO;
use Ada.Text_IO;

procedure Select_Main is
   task Lock is
      entry Acquire;
      entry Release;
   end Lock;

   task body Lock is
   begin
      accept Acquire;
      delay 0.3;
      accept Release;
   end Lock;

   task Slow is
      entry Ping;
   end Slow;

   task body Slow is
   begin
      delay 1.0;
      accept Ping;
   end Slow;
begin
   Lock.Acquire;
   select
      Lock.Acquire;
      Put_Line ("second acquire");
   else
      Put_Line ("lock is busy");
   end select;
   Lock.Release;
   Put_Line ("released");
   select
      Slow.Ping;
      Put_Line ("pinged");
   or
      delay 0.2;
      Put_Line ("gave up waiting");
   end select;
   delay 0.8;
   Slow.Ping;
   Put_Line ("late ping");
   Put_Line ("done");
end Select_Main;

Lock accepts Acquire, then delays before accepting Release. The second Acquire therefore finds no ready accept and takes the else branch. The call to Release does wait, because it has no alternative, and it returns when the lock reaches its second accept. Slow accepts Ping after one second, so the select with a 0.2 second delay ends in the timeout. The later call to Ping then completes the rendezvous without any output of its own.

$ gnatmake -gnat2022 -gnatwa select_main.adb
$ ./select_main
lock is busy
released
gave up waiting
late ping
done

Guards and terminate

A guard is a condition written before an accept alternative. The alternative is open only while the condition is true, so a buffer can accept Put only when it is not full and Get only when it is not empty. The terminate alternative lets the task end when no caller can ever reach it again.

guarded_main.adb
with Ada.Text_IO;
use Ada.Text_IO;

procedure Guarded_Main is
   task Buffer is
      entry Put (Value : Integer);
      entry Get (Value : out Integer);
   end Buffer;

   task body Buffer is
      Item : Integer := 0;
      Full : Boolean := False;
   begin
      loop
         select
            when not Full =>
               accept Put (Value : Integer) do
                  Item := Value;
                  Full := True;
               end Put;
         or
            when Full =>
               accept Get (Value : out Integer) do
                  Value := Item;
                  Full := False;
               end Get;
         or
            terminate;
         end select;
      end loop;
   end Buffer;

   Value : Integer;
begin
   Buffer.Put (11);
   Buffer.Get (Value);
   Put_Line ("first  =" & Integer'Image (Value));
   Buffer.Put (22);
   Buffer.Get (Value);
   Put_Line ("second =" & Integer'Image (Value));
end Guarded_Main;

The main procedure alternates between Put and Get. The first put fills the buffer, the get empties it, and the second pair does the same with 22. When the main procedure ends, the buffer has no possible callers left and takes the terminate alternative instead of waiting forever.

$ gnatmake -gnat2022 -gnatwa guarded_main.adb
$ ./guarded_main
first  = 11
second = 22

An applied example: producer and consumer

The final program connects three tasks with one-slot buffer. The producer computes the squares of 1 to 4 and puts them into the buffer; the consumer takes them out and prints them. Because the buffer holds one value, the producer has to wait whenever the consumer has not caught up.

pipe_main.adb
with Ada.Text_IO;
use Ada.Text_IO;

procedure Pipe_Main is
   task Buffer is
      entry Put (Value : Integer);
      entry Get (Value : out Integer);
   end Buffer;

   task body Buffer is
      Item : Integer := 0;
      Full : Boolean := False;
   begin
      loop
         select
            when not Full =>
               accept Put (Value : Integer) do
                  Item := Value;
                  Full := True;
               end Put;
         or
            when Full =>
               accept Get (Value : out Integer) do
                  Value := Item;
                  Full := False;
               end Get;
         or
            terminate;
         end select;
      end loop;
   end Buffer;

   task Producer;
   task body Producer is
   begin
      for I in 1 .. 4 loop
         Buffer.Put (I * I);
      end loop;
   end Producer;

   task Consumer;
   task body Consumer is
      Value : Integer;
   begin
      for I in 1 .. 4 loop
         Buffer.Get (Value);
         Put_Line ("got" & Integer'Image (Value));
      end loop;
   end Consumer;
begin
   null;
end Pipe_Main;

The main procedure has no statements of its own, but it is the master of the three tasks, so the program waits for all of them. The buffer serializes the producer and the consumer: every put is followed by a get before the next put can start. The output is therefore in the order the producer generated it.

$ gnatmake -gnat2022 -gnatwa pipe_main.adb
$ ./pipe_main
got 1
got 4
got 9
got 16

Next steps

Entries synchronize a caller with a task, but shared data that many tasks read and write needs a different tool. The next article introduces protected objects, whose procedures and functions provide mutual exclusion without an explicit rendezvous.

Source

Ada Reference Manual

GNAT User's Guide

AdaCore learning resources

In this article we have declared entries, accepted them in task bodies, passed data in both directions, used select with else and a timeout, guarded entries with barriers, ended a task with terminate, and connected a producer and a consumer through a one-slot buffer. The tasks article explains the task bodies and the task master that this article relies on.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all Ada tutorials.