actors library?

xancorreu xancorreu at gmail.com
Wed Jan 25 10:29:13 PST 2012


Thanks Gehr for your examples. Very illustrative.
I wanted what you do in example 1 like a library. It's easy import 
actors library than to define your own actors (class).

Thanks,
Xan.

Al 24/01/12 21:11, En/na Timon Gehr ha escrit:
> On 01/24/2012 07:51 PM, xancorreu wrote:
>> Al 24/01/12 13:37, En/na Dejan Lekic ha escrit:
>>> Xan, read this article please:
>>> http://www.informit.com/articles/article.aspx?p=1609144
>>>
>>> You have exactly what you are looking for in the D runtime and
>>> standard library.
>> I read it and **after** I post the question. I don't know how
>> std.concurrency is related to actors model. Can you enlight me?
>>
>> Thanks,
>> Xan.
>
> std.concurrency is an implementation of the actor model.
> 'Actor model' does not imply 'Object Oriented'.
>
> Example 1:
>
> import std.stdio, std.concurrency;
> void myActor() {
>     try {
>         for(;;){
>             receive(
>                 (int i){ writeln("Received integer: ",i); }
>             );
>         }
>     }catch(Exception e){
>         // cleanup
>     }
> }
>
> void main() {
>     auto actor = spawn(&myActor);
>     foreach(i;0..10) actor.send(i);
> }
>
> Example 2:
>
> import std.stdio, std.concurrency;
> import core.thread;
> alias Thread.sleep sleep;
> void ping() {
>     Tid pong;
>     try {
>         for(;;){
>             receive(
>                 (string s){
>                     writeln("ping received ",s);
>                     sleep(dur!"seconds"(1));
>                     pong.send("ping");
>                 },
>                 (Tid newPong){ pong = newPong; }
>             );
>         }
>     }catch(Exception e){}
> }
>
> void pong(Tid ping) {
>     try {
>         ping.send("pong");
>         for(;;){
>             receive(
>                 (string s){
>                     writeln("pong received ",s);
>                     sleep(dur!"seconds"(1));
>                     ping.send("pong");
>                 }
>             );
>         }
>     }catch(Exception e){}
> }
>
> void main() {
>     auto a1 = spawn(&ping);
>     auto a2 = spawn(&pong,a1);
>     a1.send(a2);
>     sleep(dur!"seconds"(10));
> }



More information about the Digitalmars-d-learn mailing list