2009-08-27
気がついたら std.thread から core.thread になってた。
ThreadGroup て言う機能が増えていた。
複数のスレッドをグループ化して扱うもの。
サンプルを書いてみた。
クロージャも一緒に使ってみる。
import core.thread; import std.stdio; auto func(string name,long w) { return (){ for(int i = 0;i < 10;i++) { writefln("%s = %d",name,i); Thread.sleep( w ); } }; } void main() { ThreadGroup tg = new ThreadGroup; tg.create(func("foo",10_000_000)); //1秒毎にカウント すぐに実行 tg.add(new Thread(func("bar",5_000_000))); //0.5秒毎にカウント tg.add(new Thread(func("baz",5_000_000))); //0.5秒毎にカウント writeln("thread start"); foreach(t;tg) { if(!t.isRunning) t.start;//未実行のスレッドを起動 } writeln("thread all join"); tg.joinAll; //すべてのスレッドの終了を待つ writeln("thread end"); }