3
首先,D如何创建并行foreach(底层逻辑)?D并行循环
int main(string[] args)
{
int[] arr;
arr.length = 100000000;
/* Why it is working?, it's simple foreach which working with
reference to int from arr, parallel function return ParallelForeach!R
(ParallelForeach!int[]), but I don't know what it is.
Parallel function is part od phobos library, not D builtin function, then what
kind of magic is used for this? */
foreach (ref e;parallel(arr))
{
e = 100;
}
foreach (ref e;parallel(arr))
{
e *= e;
}
return 0;
}
第二,为什么它是慢那么简单的foreach? 最后,如果我创建自己的taskPool(并且不使用全局taskPool对象),程序永远不会结束。为什么?
感谢一个很好的答案关闭线程池的原因。我有另外一个问题。我如何使用(在我的课上重载)opApply?在文档中没有描述oppApply运算符重载。 – Seraph
请参阅http://dlang.org/statement.html#ForeachStatement和http://ddili.org/ders/d.en/foreach_opapply.html –
谢谢,这对我非常有帮助。 – Seraph