信号量可以使用数字运算吗?
我的意思是,添加或减去信号量是否可以接受?我的例子如下:
semaphore secureTarget = 7;
semaphore allClearAlert = 0;
semaphore bellAlert = 0;
Archer:
start();
wait(secureTarget);
wait(allClearAlert);
fireAtTarget();
signal(secureTarget);
wait(secureTarget - 7);
signal(bellAlert);
end();
Boy:
start();
signal(allClearAlert);
wait(bellAlert);
end();
这看起来可以接受吗?如果有帮助,我想回答的最初问题是:
射箭俱乐部有七个目标。俱乐部的弓箭手必须参加比赛 以确保目标。一旦弓箭手锁定了目标,她就必须等待 直到解除警报后她才能开火。曾经一个 弓箭手完成射击,她离开了目标。最后完成比赛的弓箭手 铃声响起,表示全部射击完毕。只有那时 收集箭的小男孩冒险出去是否安全? 当所有的箭都收集完毕后,男孩离开了射击场 弓箭手的线条和声音都已解除。
By that I mean, is it acceptable to add to or subtract from semaphores? The example I have is the following:
semaphore secureTarget = 7;
semaphore allClearAlert = 0;
semaphore bellAlert = 0;
Archer:
start();
wait(secureTarget);
wait(allClearAlert);
fireAtTarget();
signal(secureTarget);
wait(secureTarget - 7);
signal(bellAlert);
end();
Boy:
start();
signal(allClearAlert);
wait(bellAlert);
end();
Does that seem acceptable? If it helps, the initial question I'm trying to answer is:
An archery club has seven targets. Archers in the club must compete
to secure a target. Once an archer secures her target she must wait
until the all-clear has been sounded before she can fire. Once an
archer finishes firing she leaves her target. The last archer to finish
sounds the bell that signifies that all have finished firing. Only then
is it safe for the small boy who collects the arrows to venture forth.
When all the arrows have been collected the boy gets out of the firing
lines and sounds the all-clear to the archers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
信号量只能使用 signal() 和 wait() 方法递增,您不能按照您的描述显式更改变量。我无法明确给出解决方案 - 看看您的历史记录,我认为我正在为同一模块做相同的课程,并且我不想因抄袭而完成,但您可能会发现 信号量小书很有用。
编辑:您不必只使用信号量。您可以使用其他类型的共享数据,只要使用互斥信号量来控制对这些变量的并发访问即可。
Semaphores can only be incremented using the signal() and wait() methods, you can't explictly change the variable as you describe. I can't give the solution explictly - looking at your history I think I'm doing the same coursework for the same module and I don't want to be done for plagiarism, but you may find the Little Book of Semaphores useful.
EDIT: you don't have to just use semaphores. You can use other types of shared data, as long as you use a mutex semaphore to control concurrent access to those variables.