Boost logo

Boost :

Subject: [boost] Non-allocating future-promise
From: Niall Douglas (s_sourceforge_at_[hidden])
Date: 2014-10-17 16:50:11


Dear Boost,

I have been working on non-allocating future-promise which when
combined with proposed expected<T, E> (i.e. basic_promise<expected<T,
E>>) will provide a close substitute to std::future/promise, except
that it doesn't use an allocator and can reduce to pure constexpr
when possible e.g. this code:

__attribute__((noinline)) int test1()
{
  promise<int> p;
  auto f(p.get_future());
  p.set_value(5);
  return f.get();
}

... reduces to exactly:

_Z5test1v: # @_Z5test1v
        .cfi_startproc
# BB#0: # %_ZN7promiseIiJEED2Ev.exit2
        movl $5, %eax
        ret

The non-allocating future-promise is intended to be compatible with
the Concurrency TS extensions
(http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4123.html)
where futures can have continuations scheduled against them via a
future.then(callable). However, this involves an unavoidable memory
allocation, which while fine for dynamically assigned continuations
is overkill for statically assigned continuations.

There is no reason why one could not also have a method of statically
adding continuations which uses type extension to store the
additions. Let us say we have a template<class T, class...
Continuations> class promise and that this promise also provides a
.then(callable) like this:

template<class F> promise<T, Continuations..., F> then(F &&c)
{
  return promise<T, Continuations..., F>(std::move(*this),
std::forward<F>(c));
}

... and therefore to statically add continuations to the future
promise, one simply iterates .then(callable) on the promise like
this:

auto p=promise<int>().then([]).then([]) ...;
auto f=p.get_future();
p.set_value(5); // executes all the continuations

My first question to the Boost community is this: is there some less
ugly way? I'm thinking maybe the promise's constructor could take a
set of continuations, and then a make_promise(callable, callable,
...) could construct a statically continued future-promise?

My second question to the Boost community is this: should static
continuations be able to see the value being set? Or indeed, to
modify it before it gets sent to future.get()?

My thanks in advance.

Niall

-- 
ned Productions Limited Consulting
http://www.nedproductions.biz/ 
http://ie.linkedin.com/in/nialldouglas/



Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk