The OpenD Programming Language

TaskPool.asyncBuf

Given a callable object next that writes to a user-provided buffer and a second callable object empty that determines whether more data is available to write via next, returns an input range that asynchronously calls next with a set of size nBuffers of buffers and makes the results available in the order they were obtained via the input range interface of the returned object. Similarly to the input range overload of asyncBuf, the first half of the buffers are made available via the range interface while the second half are filled and vice-versa.

  1. auto asyncBuf(S source, size_t bufSize)
  2. auto asyncBuf(C1 next, C2 empty, size_t initialBufSize, size_t nBuffers)
    class TaskPool
    asyncBuf
    (
    C1
    C2
    )
    (
    C1 next
    ,,
    size_t initialBufSize = 0
    ,
    size_t nBuffers = 100
    )
    if (
    is(typeof(C2.init()) : bool) &&
    Parameters!C1.length == 1
    &&
    Parameters!C2.length == 0
    &&
    )

Parameters

next C1

A callable object that takes a single argument that must be an array with mutable elements. When called, next writes data to the array provided by the caller.

empty C2

A callable object that takes no arguments and returns a type implicitly convertible to bool. This is used to signify that no more data is available to be obtained by calling next.

initialBufSize size_t

The initial size of each buffer. If next takes its array by reference, it may resize the buffers.

nBuffers size_t

The number of buffers to cycle through when calling next.

Examples

// Fetch lines of a file in a background
// thread while processing previously fetched
// lines, without duplicating any lines.
auto file = File("foo.txt");

void next(ref char[] buf)
{
    file.readln(buf);
}

// Fetch more lines in the background while we
// process the lines already read into memory
// into a matrix of doubles.
double[][] matrix;
auto asyncReader = taskPool.asyncBuf(&next, &file.eof);

foreach (line; asyncReader)
{
    auto ls = line.split("\t");
    matrix ~= to!(double[])(ls);
}

Exception Handling:

Any exceptions thrown while iterating over range are re-thrown on a call to popFront.

Warning:

Using the range returned by this function in a parallel foreach loop will not work because buffers may be overwritten while the task that processes them is in queue. This is checked for at compile time and will result in a static assertion failure.

Meta