Function Ada保护类型
我在Ada中有一个简单的程序包和函数。我希望所有的函数和过程都是受保护的类型 e、 对于一个简单的.adb文件Function Ada保护类型,function,ada,procedure,Function,Ada,Procedure,我在Ada中有一个简单的程序包和函数。我希望所有的函数和过程都是受保护的类型 e、 对于一个简单的.adb文件 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)
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;
或者是一个简单的广告
package body Pack is
procedure procedure1 (B : in out Integer);
procedure procedure2 (B: in out Integer);
function procedure3 (B : Integer) return Boolean;
end pack;
我该怎么做呢?受保护类型的问题是它保护某些东西(防止并发访问)。很难从代码中看出您想要保护什么 比如说,如果你想做一个线程安全的增量,你可能会有一个类似的规范
package Pack is
protected type T is
procedure Set (To : Integer);
procedure Increment (By : Integer);
function Get return Integer;
private
Value : Integer := 0;
end T;
end Pack;
(这远非完美;您希望在声明T
时能够指定初始值,但这开始变得复杂起来)
在这种情况下,要保护的对象是值
。您需要确保,如果两个任务同时调用Increment
,一个任务调用By=>3
,另一个任务调用By=>4
,则值最终会增加7
尸体可能看起来像
package body Pack is
protected body T is
procedure Set (To : Integer) is
begin
Value := To;
end Set;
procedure Increment (By : Integer) is
begin
Value := Value + By;
end Increment;
function Get return Integer is
begin
return Value;
end Get;
end T;
end Pack;
推荐阅读:关于受保护的类型。受保护类型的一点是它保护某些东西(防止并发访问)。很难从代码中看出您想要保护什么
比如说,如果你想做一个线程安全的增量,你可能会有一个类似的规范
package Pack is
protected type T is
procedure Set (To : Integer);
procedure Increment (By : Integer);
function Get return Integer;
private
Value : Integer := 0;
end T;
end Pack;
(这远非完美;您希望在声明T
时能够指定初始值,但这开始变得复杂起来)
在这种情况下,要保护的对象是值
。您需要确保,如果两个任务同时调用Increment
,一个任务调用By=>3
,另一个任务调用By=>4
,则值最终会增加7
尸体可能看起来像
package body Pack is
protected body T is
procedure Set (To : Integer) is
begin
Value := To;
end Set;
procedure Increment (By : Integer) is
begin
Value := Value + By;
end Increment;
function Get return Integer is
begin
return Value;
end Get;
end T;
end Pack;
推荐阅读:关于受保护的类型。这段代码距离编译还有很长的路要走。在procedure1
中,B被声明为整数
,但您随后为其分配一个访问值(新整数
)。在另外两个子程序中,B似乎是记录类型。并且保留字body
不能出现在软件包规范1>中。这是无效的Ada代码。2> 您是指并发中的受保护类型吗?如果是这样的话,请阅读这里:这段代码距离编译还有很长的路要走。在procedure1
中,B被声明为整数
,但您随后为其分配一个访问值(新整数
)。在另外两个子程序中,B似乎是记录类型。并且保留字body
不能出现在软件包规范1>中。这是无效的Ada代码。2> 您是指并发中的受保护类型吗?如果在这里阅读:+1获得比我更好的链接:)您甚至可以将受保护对象隐藏在包体中,只需将包装子程序的规格保留在规范文件中。+1获得比我更好的链接:)您甚至可以将受保护对象隐藏在包体中,只需将包装子程序的规格保留在规范文件中。