スレッド
サンプルがなくて困る…newして関数ポインタを渡すか、委譲。多分Threadを継承してint runをオーバーライドしてもOK。
startで走らせてwaitで待つ。普通。
import std.thread; import std.c.stdio; int threadA(void* p){ for(int i = 0; i < 20; i++){ printf("A : %d\n", i); } return 0; } int threadB(void* p){ for(int i = 0; i < 20; i++){ printf("B : %d\n", i); } return 0; } void main(){ Thread a = new Thread(&threadA); Thread b = new Thread(&threadB); a.start; b.start; a.wait; b.wait; }
何故かエラー。
thread.d(19): constructor std.thread.Thread.this (uint) does not match argument types (int(*)(void*)) thread.d(19): cannot implicitly convert expression (& threadA) of type int(*)(void*) to int delegate() thread.d(20): constructor std.thread.Thread.this (uint) does not match argument types (int(*)(void*)) thread.d(20): cannot implicitly convert expression (& threadB) of type int(*)(void*) to int delegate()
あー。「this(int(* fp)(void*));」はないもんね…というかないのかよ…使いづらい。
とりあえず「new Thread(&threadA, cast(void*)0);」とか相当ひどいことして解決。酷い。
せめて「this(int (*fp)(void *), void *arg = null, size_t stacksize = 0)」とかならいいのに。
そういえばDのnullってどうなってるんだろう。後で調べよう。
兎も角実行。
C:\d>thread A : 0 A : 1 A : 2 ... A : 14 B : 0 ... B : 14 A : 15 ... A : 19 B : 15 ...
実装方法Rubyと同じなのかな。よくわからない…まあいいか。