Document Number:N4256
Date:
Revises:N4107
Editor: Artur Laksberg
Microsoft Corp.
arturl@microsoft.com

Working Draft, Technical Specification for C++ Extensions for Concurrency

Note: this is an early draft. It’s known to be incomplet and incorrekt, and it has lots of bad formatting.

1

General

[general]
1.1

Namespaces, headers, and modifications to standard classes

[general.namespaces]

Since the extensions described in this technical specification are experimental and not part of the C++ standard library, they should not be declared directly within namespace std. Unless otherwise specified, all components described in this technical specification either:

  • modify an existing interface in the C++ Standard Library in-place,
  • are declared in a namespace whose name appends ::experimental::concurrency_v1 to a namespace defined in the C++ Standard Library, such as std, or
  • are declared in a subnamespace of a namespace described in the previous bullet, whose name is not the same as an existing subnamespace of namespace std.

Each header described in this technical specification shall import the contents of std::experimental::concurrency_v1 into std::experimental as if by

namespace std {
  namespace experimental {
    inline namespace concurrency_v1 {}
  }
}

Unless otherwise specified, references to other entities described in this technical specification are assumed to be qualified with std::experimental::concurrency_v1::, and references to entities described in the standard are assumed to be qualified with std::.

Extensions that are expected to eventually be added to an existing header <meow> are provided inside the <experimental/meow> header, which shall include the standard contents of <meow> as if by

#include <meow>

New headers are also provided in the <experimental/> directory, but without such an #include.

Table 1 — C++ library headers
  • <experimental/future>
1.2

Future plans (Informative)

[general.plans]

This section describes tentative plans for future versions of this technical specification and plans for moving content into future versions of the C++ Standard.

The C++ committee intends to release a new version of this technical specification approximately every year, containing the library extensions we hope to add to a near-future version of the C++ Standard. Future versions will define their contents in std::experimental::concurrency_v2, std::experimental::concurrency_v3, etc., with the most recent implemented version inlined into std::experimental.

When an extension defined in this or a future version of this technical specification represents enough existing practice, it will be moved into the next version of the C++ Standard by removing the experimental::concurrency_vN segment of its namespace and by removing the experimental/ prefix from its header's path.

1.3

Feature-testing recommendations (Informative)

[general.feature.test]

For the sake of improved portability between partial implementations of various C++ standards, WG21 (the ISO technical committee for the C++ programming language) recommends that implementers and programmers follow the guidelines in this section concerning feature-test macros. [ Note: WG21's SD-6 makes similar recommendations for the C++ Standard itself. end note ]

Implementers who provide a new standard feature should define a macro with the recommended name, in the same circumstances under which the feature is available (for example, taking into account relevant command-line options), to indicate the presence of support for that feature. Implementers should define that macro with the value specified in the most recent version of this technical specification that they have implemented. The recommended macro name is "__cpp_lib_experimental_" followed by the string in the "Macro Name Suffix" column.

Programmers who wish to determine whether a feature is available in an implementation should base that determination on the presence of the header (determined with __has_include(<header/name>)) and the state of the macro with the recommended name. (The absence of a tested feature may result in a program with decreased functionality, or the relevant functionality may be provided in a different way. A program that strictly depends on support for a feature can just try to use the feature unconditionally; presumably, on an implementation lacking necessary support, translation will fail.)

Table 2 — Significant features in this technical specification
Doc. No. Title Primary Section Macro Name Suffix Value Header
N3875 Improvements to std::future<T> and Related APIs 2 future_continuations 201410 <experimental/future>
2

Improvements to std::future<T> and Related APIs

[futures]
2.1

General

[futures.general]

The extensions proposed here are an evolution of the functionality of std::future and std::shared_future. The extensions enable wait free composition of asynchronous operations. Class templates std::promise, std::packaged_task and function template std::async are also updated to be compatible with the updated std::future.

2.2

Header <experimental/future> synopsis

[header.future.synop]
#include <future>

namespace std {
  namespace experimental {
  inline namespace concurrency_v1 {

    template <class R> class promise;
    template <class R> class promise<R&>;
    template <> class promise<void>;

    template <class R>
    void swap(promise<R>& x, promise<R>& y) noexcept;

    template <class R> class future;
    template <class R> class future<R&>;
    template <> class future<void>;
    template <class R> class shared_future;
    template <class R> class shared_future<R&>;
    template <> class shared_future<void>;

    template <class> class packaged_task; // undefined
    template <class R, class... ArgTypes>
    class packaged_task<R(ArgTypes...)>;

    template <class R, class... ArgTypes>
    void swap(packaged_task<R(ArgTypes...)>&, packaged_task<R(ArgTypes...)>&) noexcept;

    template <class F, class... Args>
      future<result_of_t<decay_t<F>(decay_t<Args>...)>>
      async(F&& f, Args&&... args);
    template <class F, class... Args>
      future<result_of_t<decay_t<F>(decay_t<Args>...)>>
      async(launch policy, F&& f, Args&&... args);

    template <class T>
    future<decay_t<T>> make_ready_future(T&& value);
    future<void> make_ready_future();

    future<T> make_exceptional_future(exception_ptr value);
    template <class T, class E>
    future<T> make_exceptional_future(E ex);

    template <class InputIterator>
      see below when_all(InputIterator first, InputIterator last);
    template <class... Futures>
      see below when_all(Futures&&... futures);

    template <class InputIterator>
      see below when_any(InputIterator first, InputIterator last);
    template <class... Futures>
      see below when_any(Futures&&... futures);

    template <class InputIterator>
      see below when_any_back(InputIterator first, InputIterator last);

  } // namespace concurrency_v1
  } // namespace experimental

  template <class R, class Alloc>
  struct uses_allocator<experimental::promise<R>, Alloc>;

  template <class R, class Alloc>
  struct uses_allocator<experimental::packaged_task<R>, Alloc>;

} // namespace std
2.3

Class template future

[futures.unique_future]

The specification of all declarations within this sub-clause 2.3 and its sub-clauses are the same as the corresponding declarations, as specified in , unless explicitly specified otherwise.


namespace std {
  namespace experimental {
  inline namespace concurrency_v1 {

    template <class R>
    class future {
    public:
      future() noexcept;
      future(future &&) noexcept;
      future(const future& rhs) = delete;
      future(future<future<R>>&& rhs) noexcept;
      ~future();
      future& operator=(const future& rhs) = delete;
      future& operator=(future&&) noexcept;
      shared_future&<R> share();

      // retrieving the value
      see below get();

      // functions to check state
      bool valid() const noexcept;
      bool is_ready() const;

      void wait() const;
      template <class Rep, class Period>
        future_status wait_for(const chrono::duration<Rep, Period>& rel_time) const;
      template <class Clock, class Duration>
        future_status wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;

      // continuations
      template<class F>
        see below then(F&& func);
      template<class F>
        see below then(launch policy, F&& func);

    };

  } // namespace concurrency_v1
  } // namespace experimental
  } // namespace std

In between paragraphs 9 and 10, add the following:

future(future<future<R>>&& rhs) noexcept;
Effects:
Constructs a future object from the shared state referred to by rhs. The future becomes ready when one of the following occurs:
  • Both the outer and the inner futures are ready. The future inherits the value or the exception from the inner future.
  • The outer future is ready but the inner future is invalid. The future gets an exception of type std::future_error, with an error code of std::future_errc::broken_promise.
Postconditions:
  • valid() == true.
  • rhs.valid() == false.

After paragraph 26, add the following:

The member function template then provides a mechanism for attaching a continuation to a future object, that will be executed as specified below.


template<class F>
see below then(F&& func);

template<class F>
see below then(launch policy, F&& func);
Requires:
INVOKE(func, *this) shall be a valid expression.
Notes:
The two functions differ only by input parameters. The first takes a callable object which accepts a future object as a parameter. The second function takes a launch policy as the first parameter and a callable object as the second parameter.
Effects:
Both functions create a shared state that is associated with the returned future object. The further behavior of the functions is as follows.
  • The continuation INVOKE(DECAY_COPY (std::forward<F>(func))) is called when the object's shared state is ready (has a value or exception stored).
  • The continuation launches according to the specified launch policy, if it is provided.
  • When the launch policy is not provided the continuation inherits the parent's launch policy.
  • Any value returned from the continuation is stored as the result in the shared state of the resulting future. Any exception propagated from the execution of the continuation is stored as the exceptional result in the shared state of the resulting future.
  • If the parent was created with std::promise or with a packaged_task (has no associated launch policy), the continuation behaves the same as in the second overload with a policy argument of launch::async | launch::deferred and the same argument for func.
  • If the parent has a policy of launch::deferred, then it is filled by calling wait() or get() on the resulting future. [ Example:
    auto f1 = async(launch::deferred, [] { return 1; });
    
    auto f2 = f1.then([](future<int> n) { return 2; });
    
    f2.wait(); // execution of f1 starts here, followed by f2
          
    end example ]
Returns:
The return type of then depends on the return type of the closure func as defined below:
  • When result_of_t<decay_t<F>(future<R>)> is future<R2>, the function returns future<R2>.
  • Otherwise, the function returns future<result_of_t<decay_t<F>(future<R>)>>.

    [ Note: The first rule above is called the implicit unwrapping. Without this rule, the return type of then taking a closure returning a future<R> would have been future<future<R>>. This rule avoids such nested future objects. [ Example: The type of f2 below is future<int> and not future<future<int>>: end example ] end note ]

    future<int> f1 = g();
    future<int> f2 = f1.then([](future<int> f) {
                        future<int> f3 = h();
                        return f3;
                     });
    
  • Unlike futures returned from std::async (see paragraph 4), the destructor of the future object returned from then will not block.
Postconditions:
  • valid() == false on the original future; valid() == true on the future returned from then.
    Notes:
    In case of implicit unwrapping, the validity of the future returned from then cannot be established until after the completion of the continuation. If it is not valid, the resulting future becomes ready with an exception of type std::future_error, with an error code of std::future_errc::broken_promise.
bool is_ready() const;
Returns:
true if the shared state is ready, false if it isn't.
2.4

Class template shared_future

[futures.shared_future]

The specification of all declarations within this sub-clause 2.4 and its sub-clauses are the same as the corresponding declarations, as specified in , unless explicitly specified otherwise.

  namespace std {
  namespace experimental {
  inline namespace concurrency_v1 {

    template <class R>
    class shared_future {
    public:
      shared_future() noexcept;
      shared_future(const shared_future &) noexcept;
      shared_future(future &&) noexcept;
      shared_future(shared_future &&) noexcept;
      ~shared_future();
      shared_future& operator=(const shared_future& rhs);
      shared_future& operator=(shared_future&&) noexcept;

      // retrieving the value
      see below get();

      // functions to check state
      bool valid() const noexcept;
      bool is_ready() const;

      void wait() const;
      template <class Rep, class Period>
        future_status wait_for(const chrono::duration<Rep, Period>& rel_time) const;
      template <class Clock, class Duration>
        future_status wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;

      // continuations
      template<class F>
        see below then(F&& func) const;
      template<class F>
        see below then(launch policy, F&& func) const;

    };

  } // namespace concurrency_v1
  } // namespace experimental
  } // namespace std

After paragraph 10, add the following:

shared_future(future<shared_future<R>>&& rhs) noexcept;
Effects:
Constructs a shared_future object from the shared state referred to by rhs. The shared_future becomes ready when one of the following occurs:
  • Both the outer future and the inner shared_future are ready. The shared_future inherits the value or the exception from the inner shared_future.
  • The outer future is ready but the inner shared_future is invalid. The shared_future gets an exception of type std::future_error, with an error code of std::future_errc::broken_promise.
Postconditions:
  • valid() == true.
  • rhs.valid() == false.

After paragraph 28, add the following:

The member function template then provides a mechanism for attaching a continuation to a shared_future object, that will be executed as specified below.


template<class F>
see below then(F&& func) const;

template<class F>
see below then(launch policy, F&& func) const;
Requires:
INVOKE(func, *this) shall be a valid expression.
Notes:
The two functions differ only by input parameters. The first takes a callable object which accepts a shared_future object as a parameter. The second function takes a launch policy as the first parameter and a callable object which accepts a shared_future object as a parameter, as the second parameter.
Effects:
Both functions create a shared state that is associated with the returned future object. The further behavior of the functions is as follows.
  • The continuation INVOKE(DECAY_COPY (std::forward<F>(func)), *this) is called when the object's shared state is ready (has a value or exception stored).
  • The continuation launches according to the specified policy, if it is provided.
  • When the launch policy is not provided the continuation inherits the parent's launch policy.
  • Any value returned from the continuation is stored as the result in the shared state of the resulting future. Any exception propagated from the execution of the continuation is stored as the exceptional result in the shared state of the resulting future.
  • If the parent was created with std::promise (has no associated launch policy), the continuation behaves the same as in the second function with a policy argument of launch::async | launch::deferred and the same argument for func.
  • If the parent has a policy of launch::deferred, then it is filled by calling wait() or get() on the resulting future. [ Note: This is similar to future. See example in 2.3. end note ]
Returns:
The return type of then depends on the return type of the closure func as defined below:
  • When result_of_t<decay_t<F>(shared_future<R>)> is future<R2>, the function returns future<R2>.
  • Otherwise, the function returns future<result_of_t<decay_t<F>(shared_future<R>)>>.

    [ Note: This is the same as in future. See the notes on future::then return type in 2.3. end note ]

  • Unlike futures returned from std::async (see paragraph 4), the destructor of the future object returned from then will not block.
Postconditions:
  • valid() == true on the original shared_future object. valid() == true on the future returned from then.
    Notes:
    In case of implicit unwrapping, the validity of the future returned from then cannot be established until after the completion of the continuation. In such case, the resulting future becomes ready with an exception of type std::future_error, with an error code of std::future_errc::broken_promise.
bool is_ready() const;
Returns:
true if the shared state is ready, false if it isn't.
Remarks:
If the function has returned true, all subsequent invocations on the same shared_future object will also return true.
2.5

Class template promise

[futures.promise]

The specification of all declarations within this sub-clause 2.5 and its sub-clauses are the same as the corresponding declarations, as specified in , unless explicitly specified otherwise.

The future returned by the function get_future is the one defined in the experimental namespace (2.3).

2.6

Class template packaged_task

[futures.task]

The specification of all declarations within this sub-clause 2.6 and its sub-clauses are the same as the corresponding declarations, as specified in , unless explicitly specified otherwise.

The future returned by the function get_future is the one defined in the experimental namespace (2.3).

2.7

Function template async

[futures.async]

The specification of all declarations within this sub-clause 2.7 and its sub-clauses are the same as the corresponding declarations, as specified in , unless explicitly specified otherwise.

The future returned by the function async is the one defined in the experimental namespace (2.3).

2.8

Function template when_all

[futures.when_all]

The function template when_all creates a future object that becomes ready when all elements in a set of future and shared_future objects become ready.


template <class InputIterator>
see below
future<vector<typename iterator_traits<InputIterator>::value_type>>
when_all(InputIterator first, InputIterator last);

template <class... Futures>
future<tuple<decay_t<Futures>...>> when_all(Futures&&... futures);
Requires:
  • For the first overload, iterator_traits<InputIterator>::value_type shall be convertible to future<R> or shared_future<R>, but not both. If any of the future<R> or shared_future<R> objects are in invalid state (i.e. valid() == false), the behavior is undefined.
  • For the second overload, let Fi be the ith type in Futures, Ui be remove_reference_t<Fi>, and fi be the ith parameter in the function parameter pack futures, where all indexing is zero-based. Then each Ui shall be the type future<Ri> or (possibly const) shared_future<Ri>; and fi.valid() shall be true for all i.
Notes:
  • There are two variations of when_all. The first version takes a pair of InputIterators. The second takes any arbitrary number of future<R0> and shared_future<R1> objects, where R0 and R1 need not be the same type.
  • Calling the first signature of when_all where InputIterator first equals last, returns a future with an empty vector that is immediately ready.
  • Calling the second signature of when_all with no arguments returns a future<tuple<>> that is immediately ready.
Effects:
  • If any of the futures or shared_futures supplied to a call to when_all refer to deferred tasks that have not started execution, those tasks are executed before the call to when_all returns.
  • The call to when_all does not wait for non-deferred tasks, or deferred tasks that have already started executing elsewhere, to complete before returning.
  • Once all the futures and shared_futures supplied to the call to when_all are ready, the futures are moved, and the shared_futures are copied, into, correspondingly, futures or shared_futures of a new collection, which can be either tuple or a vector as described below. The order of the objects in the collection matches the order of the arguments supplied to when_all.
  • The collection is then stored as the result in a newly created shared state.
  • A new future object that refers to the shared state is created. The exact type of the future is further described below.
  • The future returned by when_all will not throw an exception, but the futures and shared_futures held in the output collection may.
Postconditions:
  • valid() == true.
  • All input future<T>s valid() == false.
  • All inputshared_future<T> valid() == true.
Returns:
  • A future object that becomes ready when all the input futures/shared_futures are ready.
2.9

Function template when_any

[futures.when_any]

The function template when_any creates a future object that becomes ready when at least one element in a set of future and shared_future objects becomes ready.


template <class InputIterator>
future<vector<typename iterator_traits<InputIterator>::value_type>>
when_any(InputIterator first, InputIterator last);

template <class... Futures>
future<tuple<decay_t<Futures>...>> when_any(Futures&&... futures);
Requires:
  • For the first overload, iterator_traits<InputIterator>::value_type shall be convertible to future<R> or shared_future<R>, but not both. If any of the future<R> or shared_future<R> objects are in invalid state (i.e. valid() == false), the behavior is undefined.
  • For the second overload, let Fi be the ith type in Futures, Ui be remove_reference_t<Fi>, and fi be the ith parameter in the function parameter pack futures, where all indexing is zero-based. Then each Ui shall be the type future<Ri> or (possibly const) shared_future<Ri>; and fi.valid() shall be true for all i.
Notes:
  • There are two variations of when_any. The first version takes a pair of InputIterators. The second takes any arbitrary number of future<R> and shared_future<R> objects, where R need not be the same type.
  • Calling the first signature of when_any where InputIterator first equals last, returns a future with an empty vector that is immediately ready.
  • Calling the second signature of when_any with no arguments returns a future<tuple<>> that is immediately ready.
Effects:
  • Each of the futures and shared_futures supplied to when_any is checked in the order supplied. If a given future or shared_future refers to a deferred task that has not yet started execution, then no further futures or shared_futures are checked, and that task is executed.
  • The call to when_any does not wait for non-deferred tasks, or deferred tasks that have already started executing elsewhere, to complete before returning.
  • Once at least one of the futures or shared_futures supplied to the call to when_any are ready, the futures are moved, and the shared_futures are copied into, correspondingly, futures or shared_futures of a new collection, which can be either tuple or a vector as described below.
  • The order of the objects in the collection matches the order of the arguments supplied to when_any.
  • The collection is then stored as the result in the newly created shared state.
  • A new future object that refers to the shared state is created. The exact type of the future is further described below.
  • The future returned by when_any will not throw an exception, but the futures and shared_futures held in the output collection may.
Postconditions:
  • valid() == true.
  • All input future<T>s valid() == false.
  • All inputshared_future<T> valid() == true.
Returns:
  • A future object that becomes ready when any of the input futures/shared_futures are ready.
2.10

Function template when_any_back

[futures.when_any_back]

A new section 30.6.12 shall be inserted at the end of . Below is the content of that section.

The function template when_any_back creates a future object that becomes ready when at least one element in a set of future and shared_future objects becomes ready. The ready future or shared_future may be identified in constant time.


template <class InputIterator>
future<vector<typename iterator_traits<InputIterator>::value_type>>
when_any_back(InputIterator first, InputIterator last);
Requires:
iterator_traits<InputIterator>::value_type shall be convertible to future<R> or shared_future<R>. If any of the future<R> or shared_future<R> objects are in invalid state (i.e. valid() == false), the behavior is undefined.
Effects:
  • Each of the futures and shared_futures supplied to when_any_back is checked in the order supplied. If a given future or shared_future refers to a deferred task that has not yet started execution, then no further futures or shared_futures are checked, and that task is executed.
  • The call to when_any_back does not wait for non-deferred tasks, or deferred tasks that have already started executing elsewhere, to complete before returning.
  • Once at least one of the futures or shared_futures supplied to the call to when_any_back are ready, the futures are moved, and the shared_futures are copied into, correspondingly, futures or shared_futures of a new collection, which can be either tuple or a vector as described below.
  • If the collection passed into when_any_back is non-empty (i.e., first != last), the last future or shared_future in the output collection is guaranteed to be in the ready state. The order of other elements in the collection is unspecified.
  • The collection is then stored as the result in the shared state.
  • A new future object that refers to the newly created shared state is created. The exact type of the future is further described below.
  • The future returned by when_any_back will not throw an exception, but the futures and shared_futures held in the output collection may.
Postconditions:
  • valid() == true.
  • All input future<T>s valid() == false.
  • All inputshared_future<T> valid() == true.
Returns:
  • A future object that becomes ready when any of the input futures/shared_futures are ready.
2.11

Function template make_ready_future

[futures.make_ready_future]

A new section 30.6.13 shall be inserted at the end of . Below is the content of that section.


template <class T>
future<decay_t<T>> make_ready_future(T&& value);

future<void> make_ready_future();
  
Effects:
The value that is passed in to the function is moved to the shared state of the returned future if it is an rvalue. Otherwise the value is copied to the shared state of the returned future.
Returns:
  • future<decay_t<T>>, if function is given a value of type T.
  • future<void>, if the function is not given any inputs.
Postconditions:
  • Returned future<decay_t<T>>, valid() == true.
  • Returned future<decay_t<T>>, is_ready() == true.
2.12

Function template make_exceptional_future

[futures.make_exceptional_future]

A new section 30.6.13 shall be inserted at the end of . Below is the content of that section.


template <class T>
future<T> make_exceptional_future(exception_ptr ex);
Effects:
As if
promise<T> p;
p.set_exception(ex);
return p.get_future();
template <class T, class E>
future<T> make_exceptional_future(E ex);
Effects:
As if
promise<T> p;
p.set_exception(make_exception_ptr(ex));
return p.get_future();