thread-pool icon indicating copy to clipboard operation
thread-pool copied to clipboard

[FEATURE] Add bulk enqueue functions

Open DeveloperPaul123 opened this issue 3 years ago • 2 comments
trafficstars

Description

Would be good to add versions of enqueue() and enqueue_detach() that would allow for submission of multiple tasks at once so that they can be batched.

DeveloperPaul123 avatar Oct 17 '22 17:10 DeveloperPaul123

Proposed API

template<std::ranges::range Range>
void batch_enqueue_detach(Range&& tasks);

// a bit simplified
template<std::ranges::range Range, RangeValue, RangeValueReturnType>
std::vector<std::future<RangeValueReturnType>> batch_equeue(Range&& tasks);

The non-detached version will prove to be challenging but should be possible. With non-C++23 code, it will be a bit memory greedy with std::shared_ptr allocations but that is a necessary tradeoff unfortunately.

The other question that remains is should we create a way to be able to pass a range of input parameters to the range of tasks? If so, how would this look?

DeveloperPaul123 avatar Oct 18 '22 19:10 DeveloperPaul123

Since std::apply exists, we could just require that a range of values that is to be used as arguments is the same length as the input batch of tasks and that the value type of the range is tuple-like. In fact, the standard specifies

The tuple need not be std::tuple, and instead may be anything that supports std::get and std::tuple_size; in particular, std::array and std::pair may be used.

DeveloperPaul123 avatar Oct 25 '22 20:10 DeveloperPaul123