taskPool.asyncBuf()

You will be familiarized with the use of taskPool.asyncBuf() function in this lesson.

We'll cover the following

Use of asyncBuf()

Similar to parallel(), asyncBuf() iterates InputRange ranges in parallel. It stores the elements in a buffer as they are produced by the range, and serves the elements from that buffer to its users.

In order to avoid making a potentially fully-lazy input range a fully-eager range, it iterates the elements in waves. Once it prepares a certain number of elements in parallel, it waits until those elements are consumed by popFront() before producing the elements of the next wave.

asyncBuf() takes a range and an optional buffer size that determines how many elements should be made available during each wave:

auto elements = taskPool.asyncBuf(range, buffer_size);

To see the effects of asyncBuf(), let’s use a range that takes half a second to iterate and half a second to process each element. This range simply produces integers up to the specified limit:

Get hands-on with 1000+ tech skills courses.