Created
June 16, 2015 17:09
-
-
Save zeroows/04f3a7c3588638143725 to your computer and use it in GitHub Desktop.
Ping Pong Actor based example in D language
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment