Ada 条目和when 语句的使用
我是 Ada 编程语言的新手,正在研究并发编程,但我在一个实现上遇到了问题。这可能是一个非常愚蠢的问题。代码是:
type status is array(1..6) of boolean; --boolean values for each track
track_available :status:=(others=>true); --true if track is available
protected track_handler is
entry track_req(n:in track_part_type); --n is track number
entry track_rel(n:in track_part_type); --n is track number
end track_handler;
protected body track_handler is
--implement entries
entry track_req(n: in track_part_type) when track_available(n) is --here where the error occurs
begin
req(n);
end track_req;
entry track_rel(n: in track_part_type) when track_available(n) is
begin
rel(n);
end track_rel;
end track_handler;
procedure req(nr : track_part_type) is
begin
--null;
track_available(nr):=false;
end req;
procedure rel(nr : track_part_type) is
begin
--null;
track_available(nr):=true;
end rel;
这里我收到“when track_available(n)”语句的编译错误,指出“n 未定义”。我认为变量 n 超出了范围,但我还需要检查数组的第 n 个索引是 true 还是 false。我怎样才能克服这个问题?
谢谢。
I am a newbie in Ada programming language and I am working on concurrent programming, but I am having a problem with one implementation. This might be very dummy question. The code is:
type status is array(1..6) of boolean; --boolean values for each track
track_available :status:=(others=>true); --true if track is available
protected track_handler is
entry track_req(n:in track_part_type); --n is track number
entry track_rel(n:in track_part_type); --n is track number
end track_handler;
protected body track_handler is
--implement entries
entry track_req(n: in track_part_type) when track_available(n) is --here where the error occurs
begin
req(n);
end track_req;
entry track_rel(n: in track_part_type) when track_available(n) is
begin
rel(n);
end track_rel;
end track_handler;
procedure req(nr : track_part_type) is
begin
--null;
track_available(nr):=false;
end req;
procedure rel(nr : track_part_type) is
begin
--null;
track_available(nr):=true;
end rel;
Here I get a compilation error for "when track_available(n)" statement saying that "n is undefined". I think variable n is out of scope, but I also need to check if the n'th index of the array is true or false. How can I overcome this problem?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您实际上不能在条目自己的防护中使用条目的参数。我猜你已经得到了这么多。
按照守卫的工作方式,所有守卫都会在等待开始之前进行评估,并且只有当时处于活动状态的才可用。它们不会被定期重新评估或动态读取或任何东西。
这意味着很难获得正确的防护逻辑,除非您编写代码以便只有受保护对象中的其他条目才能修改防护。如果您想使用受保护对象外部的一些数据来控制其行为,您可能需要使用除守卫之外的一些机制来做到这一点。比如检查入口内部并立即退出之类的。
不过,您尝试执行的操作有一种可能性:条目系列。您应该能够在守卫中使用条目族索引。
规格将更改为:
主体将更改为
You can't actually use an entry's parameters in its own guard. You got that much, I gather.
The way guards work, all of them are evaluated before the wait starts, and only the ones that are active at that time will be available. They don't get periodicly re-evaluated or dynamicaly read or anything.
This means it will be tough to get the logic for your guards right, unless you write your code so that only other entries in your protected object modify the guards. If you want to use some data from outside of your protected object to control its behavior, you will probably need to use some mechanisim other than guards to do it. Like check just inside the entry and exit immediately or something.
There is one possibility for what you are trying to do though: Entry families. You should be able to use an entry family index in a guard.
The spec would change to:
And the body would change to
在下面的代码中,您尝试在 (n: in track_part_type) 完全定义 track_available(n) 之前使用它。
另请参阅http://en.wikibooks.org/wiki/Ada_Programming/Tasking#Protected_types
国家气象局
In the code below you are trying to use track_available(n), before it has been fully defined by (n: in track_part_type).
See also http://en.wikibooks.org/wiki/Ada_Programming/Tasking#Protected_types
NWS