Ada protected objects
last modified November 23, 2026
A protected object is a passive unit that holds shared data and controls who may touch it. It has no thread of control of its own: tasks call its operations, and the object lets one writer in at a time. A protected procedure or entry runs with exclusive access, while protected functions may read in parallel as long as no writer is active. This article builds a counter, shares it between tasks, adds entries with barriers, and finishes with a protected queue.
A protected object
A protected declaration has a specification, a private part holding the state,
and a body with the operations. Bump is a protected procedure and
Value is a protected function written as an expression function.
The calls look like ordinary subprogram calls, but the object serializes them.
with Ada.Text_IO;
use Ada.Text_IO;
procedure Counter_Demo is
protected Counter is
procedure Bump;
function Value return Integer;
private
Count : Integer := 0;
end Counter;
protected body Counter is
procedure Bump is
begin
Count := Count + 1;
end Bump;
function Value return Integer is (Count);
end Counter;
begin
Counter.Bump;
Counter.Bump;
Counter.Bump;
Put_Line ("count =" & Integer'Image (Counter.Value));
end Counter_Demo;
The private part of the specification hides Count from the rest of
the program, so the only way to change it is to call Bump. Even
in this single-task program the object enforces its rules: a function cannot
assign to Count, and an entry can only run when its barrier allows
it. The next program adds tasks that call the same object at the same time.
$ gnatmake -gnat2022 -gnatwa counter_demo.adb $ ./counter_demo count = 3
Mutual exclusion with tasks
Three tasks add one to the counter fifty thousand times each. If they updated a
plain integer directly, two tasks could read the same old value and one update
would be lost. Because Add is a protected procedure, the object
runs the additions one after another, and the total is always 150000.
with Ada.Text_IO;
use Ada.Text_IO;
procedure Shared_Counter is
protected Counter is
procedure Add (Amount : Integer);
function Value return Integer;
private
Total : Integer := 0;
end Counter;
protected body Counter is
procedure Add (Amount : Integer) is
begin
Total := Total + Amount;
end Add;
function Value return Integer is (Total);
end Counter;
task type Worker is
end Worker;
task body Worker is
begin
for I in 1 .. 50_000 loop
Counter.Add (1);
end loop;
end Worker;
begin
declare
A : Worker;
B : Worker;
C : Worker;
begin
null;
end;
Put_Line ("total =" & Integer'Image (Counter.Value));
end Shared_Counter;
The three task objects live in a nested block. A task is activated before the
statements of its enclosing block, and the block cannot finish until its tasks
have terminated. That is why the total can be printed after the end
of the block and not before it. The protected object does not create the tasks;
it only makes their updates safe.
$ gnatmake -gnat2022 -gnatwa shared_counter.adb $ ./shared_counter total = 150000
Protected functions are read-only
A protected function may read the state, but it may not change it. The compiler
checks this rule, so a function that tries to update Count is
rejected rather than allowed to run without exclusive access. The following
program intentionally contains such a function.
with Ada.Text_IO;
use Ada.Text_IO;
procedure Bad_Function is
protected Counter is
function Peek return Integer;
private
Count : Integer := 0;
end Counter;
protected body Counter is
function Peek return Integer is
begin
Count := Count + 1;
return Count;
end Peek;
end Counter;
begin
Put_Line ("count =" & Integer'Image (Counter.Peek));
end Bad_Function;
The assignment inside Peek produces the diagnostic below. If the
function needs to change the object, it must be declared as a procedure, and
then callers know that a write takes place.
$ gnatmake -gnat2022 -gnatwa bad_function.adb x86_64-linux-gnu-gcc-14 -c -gnat2022 -gnatwa bad_function.adb bad_function.adb:14:10: error: protected function cannot modify its protected object gnatmake: "bad_function.adb" compilation error
Entries and barriers
An entry adds a condition to a protected operation. The condition is called a
barrier and is written in the body after the entry profile. Withdraw
can start only while the balance is at least 100, so a caller that asks too
early waits. A select with a delay alternative gives
up after a fixed time and leaves the account unchanged.
with Ada.Text_IO;
use Ada.Text_IO;
procedure Account_Demo is
protected Account is
procedure Deposit (Amount : Integer);
entry Withdraw (Amount : Integer);
function Balance return Integer;
private
Money : Integer := 50;
end Account;
protected body Account is
procedure Deposit (Amount : Integer) is
begin
Money := Money + Amount;
end Deposit;
entry Withdraw (Amount : Integer) when Money >= 100 is
begin
Money := Money - Amount;
end Withdraw;
function Balance return Integer is (Money);
end Account;
begin
Put_Line ("balance =" & Integer'Image (Account.Balance));
select
Account.Withdraw (100);
Put_Line ("withdrew 100");
or
delay 0.2;
Put_Line ("not enough money yet");
end select;
Account.Deposit (80);
Account.Withdraw (100);
Put_Line ("balance =" & Integer'Image (Account.Balance));
end Account_Demo;
The first call finds the barrier closed, because 50 is less than 100, so the
delay alternative runs instead. The deposit raises the balance to 130, which
opens the barrier, and the second call completes immediately. A barrier cannot
refer to the parameters of its own entry: it is evaluated before the call
parameters are available, and the compiler reports "Amount" is
undefined if it tries. A barrier must depend only on the protected state.
$ gnatmake -gnat2022 -gnatwa account_demo.adb $ ./account_demo balance = 50 not enough money yet balance = 30
An applied example: a protected queue
The queue stores four integers in a ring buffer. Put waits while
the buffer is full and Get waits while it is empty, so the two
barriers keep the producer and the consumer in step. The circular index update
uses an if expression to wrap from the last slot back to the first.
with Ada.Text_IO;
use Ada.Text_IO;
procedure Queue_Demo is
Capacity : constant := 4;
type Item_Array is array (Positive range <>) of Integer;
protected Queue is
entry Put (Value : Integer);
entry Get (Value : out Integer);
function Count return Natural;
private
Items : Item_Array (1 .. Capacity) := [others => 0];
Head : Positive := 1;
Tail : Positive := 1;
Used : Natural := 0;
end Queue;
protected body Queue is
entry Put (Value : Integer) when Used < Capacity is
begin
Items (Tail) := Value;
Tail := (if Tail = Capacity then 1 else Tail + 1);
Used := Used + 1;
end Put;
entry Get (Value : out Integer) when Used > 0 is
begin
Value := Items (Head);
Head := (if Head = Capacity then 1 else Head + 1);
Used := Used - 1;
end Get;
function Count return Natural is (Used);
end Queue;
task Producer;
task body Producer is
begin
for I in 1 .. 8 loop
Queue.Put (I * I);
end loop;
end Producer;
task Consumer;
task body Consumer is
Value : Integer;
begin
for I in 1 .. 8 loop
Queue.Get (Value);
Put_Line ("got" & Integer'Image (Value));
end loop;
end Consumer;
begin
null;
end Queue_Demo;
The array type is declared outside the protected type because an anonymous
array cannot be a component, and a protected component cannot be a constant.
The producer may run ahead until four values are waiting, then blocks on
Put; the consumer takes them out in the same order. Because the
main procedure is the master of both tasks, the program waits for them even
though its own statements are just null.
Protected objects scale well for shared data that is accessed often and held
briefly. For a long conversation between two tasks, the rendezvous from the
previous article is often a better fit, and the two mechanisms can be combined.
GNAT also supports a ceiling locking policy, declared with
pragma Locking_Policy (Ceiling_Locking); in a configuration pragma
file, which turns the protected object into a priority ceiling protocol on
real-time systems.
$ gnatmake -gnat2022 -gnatwa queue_demo.adb $ ./queue_demo got 1 got 4 got 9 got 16 got 25 got 36 got 49 got 64
Next steps
The next article leaves concurrency and returns to algorithms with recursion: a function that calls itself, a base case that stops it, the call tree of naive Fibonacci, memoisation, and the depth at which the run-time stack gives out.
Source
In this article we have declared protected objects, run protected procedures and
functions, shared a counter between three tasks, seen a protected function
rejected for modifying its object, used entries with barriers and a timed
select, and built a protected ring-buffer queue. The
tasks and rendezvous
articles explain the task bodies and entry calls that these examples use.
Author
List all Ada tutorials.