美文网首页
代码 (Ada) 3. PROTECTION

代码 (Ada) 3. PROTECTION

作者: 何大炮 | 来源:发表于2017-08-21 13:59 被阅读0次

对比左右2个程序:

左边是unprotected,右边是protected

Rather small syntactical differences:
• package is replaced by protected for the module which encapsu-
late the data (Store) which potentially needs protection.
• Some of the procedures are replaced by entries.


左边是unprotected,右边是protected

On the right side, the keyword protected as well as the specific structure around the entry definitions provide a set of rules which restrict access to those operations under certain conditions:

  1. procedures enforce mutual exclusion with all other operations in the same protected object.
  2. entries enforce the same constraints as procedures, yet are also associated with a guard (boolean expression) which needs to be ful lled before a task is allowed to enter (otherwise it is blocked and neatly queued up until the guard opens up again)
  3. functions are side-effect free with respect to the protected data, i.e. the compiler will treat any write access to the protected data from within a protected function as an error.

In short: there can only ever be maximally one task inside a protected object if this task entered via a protected procedure or entry. Alternatively, multiple tasks can use one or multiple protected functions concurrently.

protected object 就像是一个特殊数据结构,被各种task取用或者调用。

练习

题目代码
  1. What will happen if you exchange the two inner for-loops (if anything)?

After we exchange their positions, protected objects are executed in front of unprotected object and different task will finish the first loop at different time spots due to the protected entry. So most of interleaves on “store” can be avoided

  1. What will happen if you increment both values in the same for-loop (if anything)?

After I put 2 ‘inc’ in the same loop, every tasks will be executed one by one in “Protected_Element.Inc”, which means that all tasks will be released from “Protected_Element.Inc” at different time spot in each iteration. Therefore, the possibility of operating on the same unprotect resource is limited

  1. What will happen if you count only to 10 inside each task (if anything)?

After I modified ‘count only to 10 inside each task’, because there are 10 tasks and the count is 10, all the output is 10*10 = 100. Although, the mutual exclusion is not guaranteed in unprotected object, but as the count is quite small --- only 10, the interleave occurs rarely.

相关文章

  • 代码 (Ada) 3. PROTECTION

    对比左右2个程序: Rather small syntactical differences:• package ...

  • 关于程序媛Ada Lovelace的十条冷知识

    1. Ada是英国著名诗人拜伦的女儿。 2. Ada还是一位伯爵夫人。 3. Ada是世界上第一位程序员。也是世界...

  • 代码(Ada)2. TASKS

    • Every task independently executes the same sequence of ...

  • 2018-11-13 是是是f

    爱迪生aDA的爱迪生aDA的爱迪生aDA的爱迪生aDA的爱迪生aDA的爱迪生aDA的爱迪生aDA的爱迪生aDA的爱...

  • protection

    我得比我妈还爱我才能成最爱自己的人。 每天的化妆就是一种遮盖,一种面具。 现在发现事实我真的会好想一点,也终于卸下...

  • Protection

    画画于我, 犹如臭氧层之于地球。 它与自我的思维和情绪、 或与他人的思想碰撞, 不断地进行合成和分裂, 形成了包裹...

  • Dubbo SPI之Adaptive详解

    前期准备 一. 增加pom 二. 添加代码 1. shuqi.dubbotest.spi.adaptive.Ada...

  • 懂你英语Level3-Unit1-Part2- [6]Dialo

    1. What’s the matter? 2. Why do you look so sad? 3. Ada h...

  • 无标题文章

    Process=Polictics=Protection

  • Proxy(代理)

    Protection Proxy Virtual Proxy

网友评论

      本文标题:代码 (Ada) 3. PROTECTION

      本文链接:https://www.haomeiwen.com/subject/ihaurxtx.html