import core.atomic; // write a test of runActor static final synchronized class SharedCounter { this() {} private int count; void inc() { atomicOp!"+="(count, 1); } int value() { return count; } } static struct TestActor { import std.exception; this(shared(SharedCounter) c1, shared(SharedCounter) c2) { this.c1 = c1; this.c2 = c2; } @ThreadEvent void inc() { c1.inc(); enforce(false); } bool isEnd() { return c1.value > 2; } void onResurrection(Throwable) { c2.inc(); } shared(SharedCounter) c1, c2; } auto scnt1 = new shared SharedCounter(), scnt2 = new shared SharedCounter(); auto con = runPhoenixActor!TestActor(scnt1, scnt2); con.inc(); Thread.sleep(dur!"msecs"(100)); assert(scnt1.value == 1); assert(scnt2.value == 1); con.inc(); Thread.sleep(dur!"msecs"(100)); assert(scnt1.value == 2); assert(scnt2.value == 2); con.inc(); Thread.sleep(dur!"msecs"(100)); assert(scnt1.value == 3); assert(scnt2.value == 3); con.inc(); Thread.sleep(dur!"msecs"(100)); assert(scnt1.value == 3); assert(scnt2.value > 0);
runActorと同様に,アクターAを別スレッドで起動しますが,Aで例外が飛んだ場合,ただちに復帰します.