at:tutorial:actors
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
at:tutorial:actors [2007/04/06 20:50] – tvcutsem | at:tutorial:actors [2020/02/05 21:26] (current) – elisag | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | < | + | ====== Concurrent Programming with Actors |
- | + | ||
- | ===== Concurrent Programming with Actors ===== | + | |
Concurrency is an integral part of the AmbientTalk programming language. Rather than relying on [[wp> | Concurrency is an integral part of the AmbientTalk programming language. Rather than relying on [[wp> | ||
- | ==== Threads vs Actors ==== | + | ===== Threads vs Actors |
In traditional programming languages, the control flow of a concurrent program is divided over a number of threads. Each thread operates concurrently and control can switch from one thread to another non-deterministically. If two threads have access to the same data (objects), they might cause erroneous behaviour (so-called //race conditions// | In traditional programming languages, the control flow of a concurrent program is divided over a number of threads. Each thread operates concurrently and control can switch from one thread to another non-deterministically. If two threads have access to the same data (objects), they might cause erroneous behaviour (so-called //race conditions// | ||
Line 15: | Line 13: | ||
Generally speaking, an active object is an object that encapsulates its own thread of control. An active object also has a message queue or mailbox from which it processes incoming messages. Each message is processed sequentially. An active object responds to an incoming message by invoking the method corresponding to the message. The method is executed by the active object' | Generally speaking, an active object is an object that encapsulates its own thread of control. An active object also has a message queue or mailbox from which it processes incoming messages. Each message is processed sequentially. An active object responds to an incoming message by invoking the method corresponding to the message. The method is executed by the active object' | ||
- | ==== Actors and Far References ==== | + | ===== AmbientTalk |
In AmbientTalk, | In AmbientTalk, | ||
Line 27: | Line 25: | ||
}; | }; | ||
}; | }; | ||
- | >>< | + | >>< |
</ | </ | ||
As you can see, actors are created similar to objects. The '' | As you can see, actors are created similar to objects. The '' | ||
- | So what exactly is a far reference to an object? The terminology stems from the E language: it is an object reference that refers to an object hosted by another actor. The main difference between regular object references and far references is that regular references allow direct, synchronous access to an object, while far references | + | So what exactly is a far reference to an object? The terminology stems from the E language: it is an object reference that refers to an object hosted by another actor. The main difference between regular object references and far references is that regular references allow direct, synchronous access to an object, while far references |
- | ==== Asynchronous Message Sending ==== | + | < |
+ | If the object referred to by a far reference is tagged with one or more type tags, the far reference itself is tagged with the same type tags. Hence, an object located on a remote actor can be tested for its types // | ||
+ | </ | ||
- | AmbientTalk, | + | The figure below summarizes AmbientTalk' |
+ | |||
+ | {{ : | ||
+ | |||
+ | ===== Asynchronous Message Sending ===== | ||
+ | |||
+ | AmbientTalk, | ||
Far references, like the reference stored in the variable '' | Far references, like the reference stored in the variable '' | ||
Line 50: | Line 56: | ||
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 and closures are always passed **by reference** | + | - Objects and closures are always passed **by far reference** |
- Native data types like numbers, text, tables, ... are always passed **by copy** | - Native data types like numbers, text, tables, ... are always passed **by copy** | ||
Line 63: | Line 69: | ||
}; | }; | ||
}; | }; | ||
- | >>< | + | >>< |
</ | </ | ||
Line 87: | Line 93: | ||
</ | </ | ||
- | ==== Isolates ==== | + | ===== Isolates |
- | 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 | + | 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-passed |
- | 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 // | + | 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 // |
< | < | ||
Line 105: | Line 111: | ||
}; | }; | ||
}; | }; | ||
- | >>< | + | >>< |
</ | </ | ||
- | The '' | + | The '' |
An isolate differs from a regular object as follows: | An isolate differs from a regular object as follows: | ||
Line 130: | Line 136: | ||
</ | </ | ||
- | < | + | < |
- | A word of warning: isolates | + | Isolates |
< | < | ||
def ==(other) { | def ==(other) { | ||
Line 137: | Line 143: | ||
} | } | ||
</ | </ | ||
+ | |||
+ | On a related note, it is good practice to consider isolates as // | ||
</ | </ | ||
- | It is important to note that an isolate has no access whatsoever to its encompassing scope. | + | As already explained, |
< | < | ||
Line 152: | Line 160: | ||
</ | </ | ||
- | Sometimes | + | However, sometimes |
< | < | ||
Line 163: | Line 171: | ||
</ | </ | ||
- | ==== Futures ==== | + | < |
+ | Since AmbientTalk 2.12 the interpreter is smart enough to figure out the lexically free variables of an isolate itself. If no variables are explicitly listed, the interpreter will try to figure out which lexically free variables it should implicitly copy. Unlike explicitly listed variables (like '' | ||
+ | </ | ||
- | As you may have noticed previously, asynchronous message sends do not return any value (that is, they return '' | + | ===== Futures ===== |
- | === The Concept === | + | As you may have noticed previously, asynchronous message sends do not return any value (that is, they return '' |
- | The most well-known language feature to reconcile return values with asynchronous message sends is the notion of a //future//. Futures are objects that represent return values that may not yet have been computed. Once the asynchronously invoked method has completed, the future is replaced with the actual return value, and objects that referred to the future transparently refer to the return value. | + | |
+ | |||
+ | |||
+ | ==== The Concept ==== | ||
+ | |||
+ | The most well-known language feature | ||
+ | |||
+ | Futures are objects that represent return values that may not yet have been computed. Once the asynchronously invoked method has completed, the future is replaced with the actual return value, and objects that referred to the future transparently refer to the return value. | ||
Using futures, it is possible to re-implement the previous example of requesting our calculator actor to add two numbers as follows: | Using futures, it is possible to re-implement the previous example of requesting our calculator actor to add two numbers as follows: | ||
Line 177: | Line 194: | ||
</ | </ | ||
- | === Enabling futures === | ||
- | Futures are a frequently recurring language feature in concurrent and distributed languages (for example, in ABCL, the actor-based concurrent language). They are also commonly known by the name of // | + | ==== Enabling futures ==== |
+ | |||
+ | In AmbientTalk, | ||
To enable futures, it suffices to import the futures module and to enable it, as follows: | To enable futures, it suffices to import the futures module and to enable it, as follows: | ||
Line 188: | Line 206: | ||
</ | </ | ||
- | The first statement imports the futures module into the current lexical scope. This enables you as a developer to use some additional language constructs exported by the futures module, as will be explained later. The second statement enables the futures behaviour, causing any asynchronous message send to return a future rather than '' | + | The first statement imports the futures module into the current lexical scope. This enables you as a developer to use some additional language constructs exported by the futures module, as will be explained later. The second statement enables the futures behaviour, causing any asynchronous message send to return a future rather than '' |
- | === Working | + | < |
+ | In what follows we provide an overview on how to work with futures. More information pertaining to the API of the futures language module can be found in the [[: | ||
+ | </ | ||
- | We have yet to describe what objects can do with futures that are // | + | ==== Working |
- | Blocking a thread on a future | + | We have described |
- | The solution proposed in the [[http:// | + | Blocking a thread on a future can be a major source of deadlocks, like any form of blocking. In the actor paradigm where communication between actors should remain strictly asynchronous, |
- | <note>Should add example here</note> | + | The solution proposed in the [[http:// |
+ | |||
+ | As an example of a pipeline of message sends, consider the following code: | ||
+ | |||
+ | <code> | ||
+ | def booleanFuture := remoteObject< | ||
+ | booleanFuture< | ||
+ | ... | ||
+ | } ifFalse: { | ||
+ | ... | ||
+ | } | ||
+ | </code> | ||
+ | |||
+ | In this example, the message '' | ||
+ | |||
+ | ==== Working with Resolved Futures ==== | ||
When a future eventually becomes resolved with a value, any messages that were accumulated by the future are forwarded asynchronously to the actual return value, such that it appears as if the original object had sent the messages to the actual return value in the first place. | When a future eventually becomes resolved with a value, any messages that were accumulated by the future are forwarded asynchronously to the actual return value, such that it appears as if the original object had sent the messages to the actual return value in the first place. | ||
+ | <note important> | ||
AmbientTalk only allows one method to be synchronously invoked on a future, the '' | AmbientTalk only allows one method to be synchronously invoked on a future, the '' | ||
- | + | </ | |
- | === Working with Resolved Futures === | + | |
As explained above, it is always correct to use asynchronous message sends to communicate with a future. Sometimes, however, we may want to perform some operation on the return value other than message sending, for example, printing it to the screen. If you print the future directly, you get the following: | As explained above, it is always correct to use asynchronous message sends to communicate with a future. Sometimes, however, we may want to perform some operation on the return value other than message sending, for example, printing it to the screen. If you print the future directly, you get the following: | ||
Line 243: | Line 278: | ||
</ | </ | ||
- | Or, you can specify a stripe | + | Or, you can specify a type tag to only catch specific exceptions: |
< | < | ||
Line 254: | Line 289: | ||
</ | </ | ||
- | The '' | + | The '' |
< | < | ||
Line 264: | Line 299: | ||
</ | </ | ||
- | === Futures and Striped Messages === | + | Finally, it is useful to know that '' |
- | Explain: | + | < |
- | '' | + | def fut := when: calculator<-add(1,2) becomes: { |sum| |
- | '' | + | |
+ | }; | ||
+ | </ | ||
- | === Conditional Synchronisation | + | When the future for ''< |
- | explain: explicit futures using '' | + | ==== Futures and Annotated Messages ==== |
- | ==== Actor Mirrors ==== | + | As previously explained, there are two modes for enabling futures in AmbientTalk. Invoking '' |
- | explain: mirror factory, message | + | When a message send is annotated with the '' |
- | ==== Nesting Actors ==== | + | < |
+ | o< | ||
+ | </ | ||
+ | |||
+ | When a message send is annotated with the '' | ||
+ | |||
+ | < | ||
+ | o< | ||
+ | </ | ||
+ | |||
+ | When a message send is annotated with the '' | ||
+ | |||
+ | < | ||
+ | o< | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | More details about the '' | ||
+ | </ | ||
+ | |||
+ | Finally, it is possible to first invoke '' | ||
+ | |||
+ | ==== Conditional Synchronisation with Futures ==== | ||
+ | |||
+ | Futures are useful to synchronise on the return value of an asynchronous message send. However, objects hosted by different actors may often want to synchronise based on other events or conditions. In such cases, futures can be created and resolved explicitly. The interface to the programmer is about the same as that specified by the E language: | ||
+ | |||
+ | < | ||
+ | // to create an explicit future: | ||
+ | def [future, resolver] := makeFuture(); | ||
+ | |||
+ | // to explicitly resolve a future | ||
+ | resolver.resolve(val); | ||
+ | </ | ||
+ | |||
+ | The '' | ||
+ | |||
+ | The resolver also defines a '' | ||
+ | |||
+ | As an example of such conditional synchronization, | ||
+ | |||
+ | < | ||
+ | def makeDatingService() { | ||
+ | def people := []; // a list of Questionnaire objects | ||
+ | object: { | ||
+ | def match(lonelyHeart) { | ||
+ | // if an ideal mate is found in the list, | ||
+ | // | ||
+ | // otherwise | ||
+ | // | ||
+ | // the lonelyHeart later, when an ideal made | ||
+ | // has registered with the dating service | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Let us assume that a person is simply identified by a name and its sex: | ||
+ | < | ||
+ | def makePerson(nam, | ||
+ | object: { | ||
+ | def name := nam; | ||
+ | def sex := sx; | ||
+ | } | ||
+ | }; | ||
+ | </ | ||
+ | |||
+ | The dating service has a little database stored as a simple list. This list does not contain person objects but rather Questionnaire objects. The questionnaire contains the logic necessary to match people. We will assume for the sake of the example that 2 people match if they are of the opposite sex. In addition, a questionnaire object can keep track of an " | ||
+ | |||
+ | < | ||
+ | def makeQuestionnaire(p) { | ||
+ | def idealPersonResolver; | ||
+ | object: { | ||
+ | def person := p; | ||
+ | def matches(otherQ) { otherQ.person.sex != p.sex }; | ||
+ | def wait() { | ||
+ | def [future, resolver] := makeFuture(); | ||
+ | idealPersonResolver := resolver; | ||
+ | future | ||
+ | }; | ||
+ | def notify(name) { idealPersonResolver.resolve(name) }; | ||
+ | }; | ||
+ | }; | ||
+ | </ | ||
+ | |||
+ | When a questionnaire is asked to '' | ||
+ | |||
+ | Armed with these abstractions, | ||
+ | |||
+ | < | ||
+ | def makeDatingService() { | ||
+ | def people := []; // a list of Questionnaire objects | ||
+ | object: { | ||
+ | def match(lonelyHeart) { | ||
+ | def lonelyHeartQ := makeQuestionnaire(lonelyHeart); | ||
+ | { |return| | ||
+ | people.each: | ||
+ | // an ideal mate was found | ||
+ | if: (idealMateQ.matches(lonelyHeartQ).and: | ||
+ | idealMateQ.notify(lonelyHeart.name); | ||
+ | // remove the person from the database | ||
+ | people := people.filter: | ||
+ | return(idealMateQ.person.name) // notify lonelyHeart | ||
+ | } | ||
+ | }; | ||
+ | // no ideal mate was found, store its questionnaire in the database | ||
+ | people := people + [lonelyHeartQ]; | ||
+ | lonelyHeartQ.wait(); | ||
+ | }.escape(); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Below, we define an auxiliary method that illustrates how a lonely heart has to interact with the dating service. | ||
+ | |||
+ | < | ||
+ | def d := makeDatingService(); | ||
+ | def register(p) { | ||
+ | when: d< | ||
+ | system.println(p.name + " matched with " + name); | ||
+ | }; | ||
+ | }; | ||
+ | </ | ||
+ | |||
+ | The key to our conditional synchronization is that the '' | ||
+ | |||
+ | The complete source code of the above example can be found in the file '' | ||
+ | |||
+ | ===== Actor Mirrors ===== | ||
+ | |||
+ | An actor in AmbientTalk is primarily a //host// for regular objects. It is equipped with a message queue to receive asynchronous messages sent to one of its objects. The mirrors on these objects have corresponding meta-level operations such as '' | ||
+ | |||
+ | Some operations, such as creating and sending asynchronous messages are useful to reify at the //actor level//. With such a reification, | ||
+ | |||
+ | Overriding the actor' | ||
+ | |||
+ | < | ||
+ | def actor := reflectOnActor(); | ||
+ | def oldmirror := actor.becomeMirroredBy: | ||
+ | def send(msg) { | ||
+ | log(msg); | ||
+ | super^send(msg); | ||
+ | }; | ||
+ | }); | ||
+ | </ | ||
+ | |||
+ | Notice that, in this example, the new metaobject protocol is an extension of the old protocol. This enables it to invoke its parent' | ||
+ | |||
+ | < | ||
+ | For a good use case of actor mirrors, see the '' | ||
+ | </ | ||
+ | |||
+ | Other methods that can be overridden are '' | ||
+ | |||
+ | ===== Nesting Actors ===== | ||
+ | |||
+ | In AmbientTalk, | ||
+ | |||
+ | < | ||
+ | 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 }; | ||
+ | }; | ||
+ | }; | ||
+ | </ | ||
+ | |||
+ | If both the '' | ||
+ | |||
+ | Recall that isolates could be given access to their enclosing lexical scope either by specifying accessed variables as formal parameters to their initializing closure or by having the interpreter derive the lexically free variables automatically. If the programmer wants to make explicit the fact that '' | ||
+ | |||
+ | < | ||
+ | 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 }; | ||
+ | }; | ||
+ | }; | ||
+ | </ | ||
- | lexical scoping rules for nested | + | It still makes sense to nest actors, but each actor will have its own local copy of lexically shared variables. Furthermore, |
at/tutorial/actors.1175885416.txt.gz · Last modified: 2007/04/07 17:18 (external edit)