Skip to content

Instantly share code, notes, and snippets.

@zeroows
Created June 16, 2015 17:09
Show Gist options
  • Save zeroows/04f3a7c3588638143725 to your computer and use it in GitHub Desktop.
Save zeroows/04f3a7c3588638143725 to your computer and use it in GitHub Desktop.
Ping Pong Actor based example in D language
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