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