vibe.d beta banner
get vibe.d
0.10.1

Asynchronous I/O that doesn’t get in your way, written in D

Function makeIsolatedArray

Creates a new isolated array.

Isolated!(T[]) makeIsolatedArray(T) (
  size_t size
) pure;

Example

import vibe.core.concurrency;
import vibe.core.core;

static void compute(Tid tid, Isolated!(double[]) array, size_t start_index)
nothrow {
	foreach( i; 0 .. array.length )
		array[i] = (start_index + i) * 0.5;

	//send(tid, array.move()); // Isolated!T isn't recognized by std.concurrency
}

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
	Tid[] tids;
	foreach (i, idx; indices)
		tids ~= runWorkerTaskH(&compute, thisTid, subarrays[i].move(), idx).tid;

	// collect results
	auto resultarrays = new Isolated!(double[])[tids.length];
	//foreach( i, tid; tids )
	//	resultarrays[i] = receiveOnly!(Isolated!(double[])).move(); // Isolated!T isn't recognized by std.concurrency

	// 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 Sönke Ludwig

License

Subject to the terms of the MIT license, as written in the included LICENSE.txt file.