Function makeIsolatedArray
Creates a new isolated array.
Prototype
Isolated!(T[]) makeIsolatedArray(T)(
size_t size
) pure;
Example
import vibe .core .concurrency;
import vibe .core .core;
static void compute(Task tid, Isolated!(double[]) array, size_t start_index)
{
foreach( i; 0 .. array .length )
array[i] = (start_index + i) * 0.5;
sendCompat(tid, array .move());
}
void test()
{
import std .stdio;
// compute contents of an array using multiple threads
auto arr = makeIsolatedArray!double(256);
// partition the array (no copying takes place)
size_t[] indices = [64, 128, 192, 256];
Isolated!(double[])[] subarrays = arr .splice(indices);
// start processing in threads
Task[] tids;
foreach (i, idx; indices)
tids ~= runWorkerTaskH(&compute, Task .getThis(), subarrays[i] .move(), idx);
// collect results
auto resultarrays = new Isolated!(double[])[tids .length];
foreach( i, tid; tids )
resultarrays[i] = receiveOnlyCompat!(Isolated!(double[])) .move();
// BUG: the arrays must be sorted here, but since there is no way to tell
// from where something was received, this is difficult here.
// merge results (no copying takes place again)
foreach( i; 1 .. resultarrays .length )
resultarrays[0] .merge(resultarrays[i]);
// convert the final result to immutable
auto result = resultarrays[0] .freeze();
writefln("Result: %s", result);
}
Authors
Sönke Ludwig
Copyright
© 2013-2014 RejectedSoftware e.K.
License
Subject to the terms of the MIT license, as written in the included LICENSE.txt file.