Ada中如何紧急通知任务?
我有一个执行顺序逻辑的任务,我想阻止该任务从另一个任务执行其逻辑。有没有办法做到这一点而不引起会合? 我怎样才能暂停任务? 提前致谢。
I have a task that perform sequential logic, and I want to stop this task from performing its logic from another task. Is there a way to do that without causing a rendezvous?
How can I suspend the task?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用异步控制权转移并将要停止的部分放入abortable_part中,或者直接使用abort 终止任务。
如果您使用 GNAT,您可以查看 GNAT.Tasking 包。
You can use Asynchronous Transfer of Control and put the part you want to stop into the abortable_part, or directly use abort to kill the task.
If you use GNAT, you could have a look at the GNAT.Tasking package.
一般来说,最好用“检查点”来构建顺序逻辑,例如受保护的对象标志,可以在其中进行简短的测试以查看是否有中止信号。受保护对象被设计为一种轻量级并发机制来支持这种快速测试。
它真的需要在语句序列中的任何点被中断吗?完成语句块或迭代并进行标志检查所需的额外几微秒或毫秒的成本真的那么不可接受吗?您预计需要中止处理序列的频率是多少?
拥有明确定义的检查点来测试信号以提前终止处理可以确保序列以已知状态退出,这有助于验证正确的操作并在出现问题时进行调试。
Generally it's better to structure your sequential logic with "check points", such as a protected object flag, where a brief test can be made to see if there's been a signal to abort. Protected objects are designed to be a lightweight concurrency mechanism to support this sort of fast test.
Does it really need to be interruptible at any point in the statement sequence? Is the cost of the few extra micro- or milliseconds needed to complete a statement block or iteration and make a flag check really that unacceptable? How often do you anticipate needing to abort the processing sequence?
Having well-defined checkpoints at which to test for a signal to prematurely terminate processing can ensure that the sequence exits in a known state, which aids verifying correct operation and debugging if something goes awry.
您可能会考虑通过在 受保护的对象。
在我看来,您正在寻找某种锁定方案。使用 Ada 保护对象实现各种不同的锁定方案相当容易,这样您就不需要在特定任务之间进行显式握手。
You might look at protecting whatever this operation or data is by implementing it inside a protected object.
It sounds to me like you are looking for some kind of locking scheme. It is fairly easy to implement all kinds of different locking schemes with Ada protected objects, and this way you don't need explicit handshaking between specific tasks.