How is the storage associated with std::future allocated?
asynchronous, c++, c++11, future, std
Solution
Just judging from the mere arguments of `std::async` there seems to be no way to control the allocation of the internal `std::promise` and it can therefore just use anything, though likely the `std::allocator`. Though I guess in theory it is unspecified, it is likely the shared state is allocated inside the calling thread. I haven't found any explicit information in the standard about this matter. In the end `std::async` is a very specialized facility for easy asynchronous invocation, so you don't have to think if there actually is a `std::promise` anywhere.
For more direct control about the behaviour of an asynchronous call there is also `std::packaged_task`, which indeed has an allocator argument. But from the mere standard quote it is not perfectly clear if this allocator is just used to allocate storage for the function (since `std::packaged_task` is kind of a special `std::function`) or if it is also used to allocate the shared state of the internal `std::promise`, though it seems likely:
30.6.9.1 [futures.task.members]:
Effects: constructs a new `packaged_task` object with a shared state and initializes the object’s stored task with `std::forward<F>(f)`. The constructors that take an `Allocator` argument use it to allocate memory needed to store the internal data structures.
Well, it doesn't even say there is a `std::promise` underneath (likewise for `std::async`), it could just be an undefined type connectable to a `std::future`.
So if it is indeed not specified how `std::packaged_task` allocates its internal shared state, your best bet might be to implement your own facilities for asynchronous function invocation. Considering that, simply spoken, a `std::packaged_task` is just a `std::function` bundled with a `std::promise` and `std::async` just starts a `std::packaged_task` in a new thread (well, except when it doesn't), this shouldn't be too much of a problem.
But indeed this might be an oversight in the specification. Whereas allocation control doesn't really fit to `std::async`, the explanation of `std::packaged_task` and its use of allocators might be a bit clearer. But this may also be intentional, so the `std::packaged_task` is free to use whatever it wants and doesn't even need a `std::promise` internally.
EDIT: Reading it again, I think the above standard quote indeed says, that the `std::packaged_task`'s shared state is allocated using the provided allocator, since it is part of the "internal data structures", whatever those are (there doesn't need to be an actual `std::promise`, though). So I think `std::packaged_task` should be enough for having explicit control of the shared state of an asynchronous task's `std::future`.
Problem
One way to get a `std::future` is through `std::async`: ``` int foo() { return 42; } ... std::future<int> x = std::async(foo); ``` In this example, how is the storage for `x`'s asynchronous state allocated, and which thread (if more than one thread is involved) is responsible for performing the allocation? Moreover, does a client of `std::async` have any control over the allocation? For context, I see that one of the constructors of `std::promise` may receive an allocator, but it's not clear to me if it is possible to customize the allocation of the `std::future` at the level of `std::async`.