Parallel array append using std.parallelism?

H. S. Teoh hsteoh at quickfur.ath.cx
Fri Jun 19 15:39:18 UTC 2020


On Fri, Jun 19, 2020 at 06:48:18AM +0000, Simen Kjærås via Digitalmars-d-learn wrote:
[...]
> There's an example of exactly this in std.parallelism:
> https://dlang.org/phobos/std_parallelism.html#.TaskPool.workerIndex
> 
> In short:
> 
>     Item[] targetArray = ...; // already contains data
>     // Get thread count from taskPool
>     Item[][] tmp = new Item[][taskPool.size+1];
>     foreach (elem; input.parallel) {
>         if (condition(elem)) {
>             auto output = expensiveComputation(elem);
>             // Use workerIndex as index
>             tmp[taskPool.workerIndex] ~= output;
>         }
>     }
>     foreach (a; tmp)
>         targetArray ~= a;
[...]

Yes, that's exactly what I was looking for. Thanks!!


T

-- 
The best way to destroy a cause is to defend it poorly.


More information about the Digitalmars-d-learn mailing list