at:tutorial:actors
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | Next revisionBoth sides next revision | ||
at:tutorial:actors [2007/04/01 12:21] – added tvcutsem | at:tutorial:actors [2007/04/01 12:50] – added tvcutsem | ||
---|---|---|---|
Line 50: | Line 50: | ||
But what happens when the method to invoke asynchronously has parameters that need to be passed. How does parameter passing work in the context of inter-actor message sending? The rules are simple enough: | But what happens when the method to invoke asynchronously has parameters that need to be passed. How does parameter passing work in the context of inter-actor message sending? The rules are simple enough: | ||
- | - Objects are always passed **by reference** | + | - Objects |
- | - Native data types like numbers, | + | - Native data types like numbers, text, tables, ... are always passed **by copy** |
+ | |||
+ | Generally speaking, any object that encapsulates a lexical scope is passed by reference, because passing such an object by copy would entail passing the entire lexical scope by copy - a costly operation. Objects without a lexical scope, such as methods, can be copied without having to recursively copy any scope. | ||
When an object is passed by reference, we mean that the formal parameter of a method will be bound to a far reference to the original object. When it is passed by copy, the formal parameter will be bound to a local copy of the object. For example, consider the following '' | When an object is passed by reference, we mean that the formal parameter of a method will be bound to a far reference to the original object. When it is passed by copy, the formal parameter will be bound to a local copy of the object. For example, consider the following '' | ||
< | < | ||
- | >>def calculator := actor: { | + | >def calculator := actor: { |
def add(x, | def add(x, | ||
customer< | customer< | ||
}; | }; | ||
}; | }; | ||
- | ><far ref to:< | + | >><far ref to:< |
</ | </ | ||
Line 67: | Line 69: | ||
< | < | ||
- | >> | + | > |
def result(sum) { | def result(sum) { | ||
system.println(" | system.println(" | ||
}; | }; | ||
}; | }; | ||
- | >nil | + | >>nil |
</ | </ | ||
- | Because of the parameter passing rules described above, the '' | + | Because of the parameter passing rules described above, the '' |
< | < | ||
Line 81: | Line 83: | ||
</ | </ | ||
- | futures | + | < |
+ | The parameter passing semantics just described lead to a model where the only references that cross actor boundaries are far references. In combination with the message sending semantics described previously, this guarantees that asynchronous messages are the only type of messages that can cross actor boundaries, ensuring that concurrent (and as will be shown later, also distributed) communication is strictly asynchronous. In such a model, deadlocks cannot occur (an actor is never blocked) and race conditions within one single actor can never occur. These properties significantly reduce the complexity of concurrent programs. | ||
+ | </ | ||
=== Isolates === | === Isolates === | ||
- | isolate stripe, by-copy, scoping rules, no external method | + | The parameter passing semantics defined above rule out any possibility for an object to be passed by copy. The reason for this semantics is that objects encapsulate a lexical scope, and parameter passing an object by-copy would require the entire lexical scope to be parameter-[assed as well. |
+ | |||
+ | To enable objects to be passed by copy between actors, a special type of objects is introduced. These objects are called **isolates** because they are // | ||
+ | |||
+ | < | ||
+ | >def complexNumber := isolate: { | ||
+ | def re; // assume cartesian coordinates | ||
+ | def im; | ||
+ | def init(re,im) { | ||
+ | self.re := re; | ||
+ | self.im := im; | ||
+ | }; | ||
+ | def +(other) { | ||
+ | self.new(re+other.re, | ||
+ | }; | ||
+ | }; | ||
+ | >>< | ||
+ | </ | ||
+ | |||
+ | The '' | ||
+ | |||
+ | An isolate differs from a regular object as follows: | ||
+ | - it has **no** access to its surrounding lexical scope; this means that an isolate only has access to its local fields and methods. An isolate does have access to the global lexical scope of its actor. | ||
+ | - it is parameter-passed | ||
+ | - external method | ||
+ | |||
+ | === Futures === | ||
+ | |||
+ | futures language construct | ||
=== Actor Mirrors === | === Actor Mirrors === |
at/tutorial/actors.txt · Last modified: 2020/02/05 21:26 by elisag