Will `std::future::then` allocate memory? Custom allocator possible?

Viewed 233

std::promise has a constructor which allocates the memory used to store the shared state. In implementations of .then, which I have seen so far, one stores a function in a type erased manner similar to std::function in the shared state. The Concurrency library of stlab even goes so far as to put a

using then_t = std::vector<std::pair<executor_t, task<void()>>>;

into the shared state (to enable splittable futures).

Is there an intent to specify if an allocator could be specified for continuations?

In P0443R3 they say in 1.2.9 Properties for customizing memory allocation

Executor implementations shall use the supplied allocator to allocate any memory required to store the submitted function object.

I think this only means the storage for agents submitted via the execute functions not the pointer in std::future's shared state.

I've looked into the recent proposals for futures or executors and I could not find anything. Recent papers seem to favour putting executors in the shared state of a future anyway. If an executor would have an associated custom allocator should this be used?

I feel like I miss something.

--

Edit: I thought about this more and I think I can grasp why one would like to leave this unspecified. If futures are ever parametrised by executors their shared state might schematically look like this

template <typename Executor, typename T>
struct shared_state_t {
    // some prevention from race condition, for example
    std::atomic<unsigned> flags;
    // some sort of exception or value representation, for example
    std::variant<std::exception_ptr, T> maybe_value;
    // some handle to continuation
    continuation_t<Executor, T> continuation;
};

where continuation_t<Executor, T> does not need to allocate memory. It could be a pointer to an internal execution agent of one executor Executor or something like std::function<void(T&&)> for another executor.

0 Answers
Related