Section 13.3 - Protected Types

A protected type is a passive data object that provides protection of data consistency even when multiple tasks attempt to access its data. Protected types are very efficient, which is why they were added to Ada in 1995. Protected types can be considered to be a very advanced form of "semaphore" or "monitor".

A protected type contains data that tasks can access only through a set of protected operations defined by the developer. There are three kinds of protected operations:

  1. Protected functions, which provide read-only access to the internal data. Multiple tasks may simultaneously call a protected function.
  2. Protected procedures, which provide exclusive read-write access to the internal data. When one task is running a protected procedure, no other task can interact with the protected type.
  3. Protected entries, which are just like protected procedures except that they add a "barrier". A barrier is some Boolean expression that must become true before the caller may proceed. The barrier would usually depend on some internal data value protected by the protected type. If the barrier is not true when the caller makes a request, the caller is placed in a queue to wait until the barrier becomes true.

Protected types tend to be very efficient, since high-overhead operations called "full context switches" aren't usually necessary to implement them. Protected types are often implemented using techniques such as interrupt disables, priority ceiling locking, or spin locks. In fact, protected types are often more efficient than using semaphores directly, which is a little surprising; see the Ada Rationale (Part 2, section 9.1.3) if you're curious why protected types can be so efficient. However, this also means that any protected operation should be short and fast; significant processing should be done elsewhere. Protected operations generally should do things like increment or decrement a value, set a flag, set an access value or two, or other similar quick operations. Lengthy operations may increase the maximum system latency (the time it takes for the system to respond to a new situation), which in many systems is a bad thing.

A protected type can be created as a single instance (i.e. a single protected variable) or as a full Ada type. As the latter you can do anything you would do with a regular type, including placing them in records or arrays.


Let's say you're creating a protected type and you want to create an operation that changes the protected type's data. This operation can always occur - it doesn't need to wait for some special circumstance. Which of the following should this operation be?

  1. Protected function.
  2. Protected procedure.
  3. Protected entry.

You may also:

PREVIOUS Go back to the previous section

NEXT     Skip to the next section

OUTLINE  Go up to lesson 13 outline

David A. Wheeler (

The master copy of this file is at "".