User Tools

Site Tools


at:tutorial:actors

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revisionBoth sides next revision
at:tutorial:actors [2007/04/07 20:10] tvcutsemat:tutorial:actors [2007/04/07 20:20] – added tvcutsem
Line 332: Line 332:
 ==== Nesting Actors ==== ==== Nesting Actors ====
  
-lexical scoping rules for nested actors+In AmbientTalk, objects can be nested inside other objects and functions can be defined in other functions. It makes sense to ask whether actors can also be nested. The answer is that yes, actors too can be nested. However, this is not as straightforward as it seems. Consider the following example: 
 + 
 +<code> 
 +def outer := actor: { 
 +  def x := 1; 
 +  def get() { x }; 
 +  def set(v) { x := v }; 
 + 
 +  def inner := actor: { 
 +    def get() { x }; 
 +    def set(v) { x := v }; 
 +  }; 
 +}; 
 +</code> 
 + 
 +If both the ''outer'' and ''inner'' actors lexically see ''x'', they could modify it concurrently, reintroducing race conditions on the internal state of an actor. Therefore, when defining an actor using a block of code, we disallow access to the enclosing lexical scope by the new actor. It is as if it was defined at top-level. Hence, actors behave similarly to [[#isolates|isolates]] in this respect. The above example is incorrect in that ''inner'' will not be able to read or modify ''x''
 + 
 +Recall that isolates could be given selective access to their enclosing lexical scope by creating a local copy of the variable. We allow actors to do the same. Hence, the above example can be written properly as: 
 + 
 +<code> 
 +def outer := actor: { 
 +  def x := 1; 
 +  def get() { x }; 
 +  def set(v) { x := v }; 
 + 
 +  def inner := actor: { |x| 
 +    def get() { x }; 
 +    def set(v) { x := v }; 
 +  }; 
 +}; 
 +</code> 
 + 
 +Hence, it still makes sense to nest actors, but each actor will operate on its own local copy of the variable. Furthermore, the value bound to the copied variable is parameter-passed according to inter-actor parameter passing rules, so if ''x'' would be bound to an object, then ''outer'' would have a normal reference to the object, while ''inner'' would receive a far reference to the object.
at/tutorial/actors.txt · Last modified: 2020/02/05 21:26 by elisag