I’ve got a simple package in Ada with procedures and functions. I’d like to have all the functions and procedures in a protected type.
e.g. for a simple .adb file
package body Pack is
procedure procedure1 (B : in out Integer) is
begin
B := new Integer;
end procedure1;
procedure procedure2 (B: in out Integer) is
begin
B.Cont(B.First-1) := 1;
end procedure2;
function procedure3 (B : Integer) return Boolean is
begin
return B.First = B.Last;
end procedure3;
end pack;
and or a simple .ads
package body Pack is
procedure procedure1 (B : in out Integer);
procedure procedure2 (B: in out Integer);
function procedure3 (B : Integer) return Boolean;
end pack;
How would I go about it?
The thing about a protected type is that it protects something (against concurrent access). It’s hard to see from your code what it is you want to protect.
If, say, you wanted to do a thread-safe increment, you might have a spec like
(this is far from perfect; you’d like to be able to specify the initial
Valuewhen you declare aT, but that’s starting to get complicated).In this case, the thing to be protected is the
Value. You want to be sure that if two tasks callIncrementat the “same” time, one withBy => 3and one withBy => 4, theValueends up being incremented by 7.The body could look like
Recommended reading: the Wikibooks section on protected types.