Source: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4123.html
Timestamp: 2019-04-25 22:03:46+00:00

Document:
This document proposes improvements to N4107, the current Working Draft of the Concurrency TS.
The document is motivated by N4032 and N4048, which were presented to SG1 at the Rapperswil meeting. This document collects the parts of these papers that received positive feedback from SG1 and presents them as a combined set of proposed changes to the Concurrency TS.
In addition, the paper also addresses numerous issues discovered and reported to the authors by various individuals, namely Agustín Bergé, Hartmut Kaiser and Anthony Williams.
The authors seek feedback from SG1 on this document. If directed by SG1, all or some of the changes proposed in this document will be incorporated into the next Working Paper of the Concurrency TS.
The proposed changes are presented as "diffs" to N4107 marked as insertions and deletions.
Some of the extensions described in this Technical Specification represent types and functions that are currently not part of the C++ Standards Library, and because these extensions are experimental, they should not be declared directly within namespace std. Instead, such extensions are declared in namespace std::experimental.
Unless otherwise specified, references to such entities described in this Technical Specification are assumed to be qualified with std::experimental, and references to entities described in the C++ Standard Library are assumed to be qualified with std::.
Editor's note: This section reflects the consensus between the LWG and LEWG at the Chicago 2013 and Issaquah 2014 meetings.
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.
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::.
New headers are also provided in the <experimental/> directory, but without such an #include.
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.
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.
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.
The specification of all declarations within this sub-clause 3.3 and its sub-clauses are the same as the corresponding declarations, as specified in C++14 §30.6.6, unless explicitly specified otherwise.
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.
The member function template then provides a mechanism for attaching a continuation to a future object, that will be executed as specified below.
INVOKE(func, *this) shall be a valid expression.
The two functions differ only by input parameters. The first only 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.
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.
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>)>>.
Unlike futures returned from std::async (see C++14 §30.6.8 paragraph 4), the destructor of the future object returned from then will not block.
The future object is moved to the parameter of the continuation function.
valid() == false on the original future; valid() == true on the future returned from then. object immediately after it returns.
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.
true if the shared state is ready, false if it isn't.
The specification of all declarations within this sub-clause 3.4 and its sub-clauses are the same as the corresponding declarations, as specified in C++14 §30.6.7, unless explicitly specified otherwise.
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.
The member function template then provides a mechanism for attaching a continuation to a shared_future object, that will be executed as specified below.
The two functions differ only by input parameters. The first only 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.
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.
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.
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>)>>.
The shared_future passed to the continuation function is a copy of the original shared_future.
valid() == true on the original shared_future object. valid() == true on the shared_future returned from then.
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.
If the function has returned true, all subsequent invocations on the same shared_future object will also return true.
The specification of all declarations within this sub-clause 3.5 and its sub-clauses are the same as the corresponding declarations, as specified in C++14 §30.6.5, unless explicitly specified otherwise.
The future returned by the function get_future is the one defined in the experimental namespace (3.3).
The specification of all declarations within this sub-clause 3.6 and its sub-clauses are the same as the corresponding declarations, as specified in C++14 §30.6.9, unless explicitly specified otherwise.
The specification of all declarations within this sub-clause 3.7 and its sub-clauses are the same as the corresponding declarations, as specified in C++14 §30.6.8, unless explicitly specified otherwise.
The future returned by the function async is the one defined in the experimental namespace (3.3).
A new section 30.6.10 shall be inserted at the end of C++14 §30.6. Below is the content of that section.
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.
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, T is of type future<R> or shared_future<R> 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.
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_allwhen_any with no arguments returns a future<tuple<>> that is immediately ready.
Each future and shared_future is waited upon and then copied into the collection of the output (returned) future, maintaining the order of the futures in the input collection.
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.
All input future<T>s valid() == false.
All inputoutput shared_future<T> valid() == true.
A future object that becomes ready when all the input futures/shared_futures are ready.
future<tuple<>> if when_all is called with zero arguments.
future<vector<future<R>>> if the input cardinality is unknown at compile and the iterator pair yields future<R>. R may be void. The order of the future in the output vector will be the same as given by the input iterator.
future<vector<shared_future<R>>> if the input cardinality is unknown at compile time and the iterator pair yields shared_future<R>. R may be void. The order of the future in the output vector will be the same as given by the input iterator.
future<tuple<future<R0>, future<R1>, future<R2>...>> if inputs are fixed in number. The inputs can be any arbitrary number of future and shared_future objects. The type of the element at each position of the tuple corresponds to the type of the argument at the same position. Any of R0, R1, R2, etc. maybe void.
A new section 30.6.11 shall be inserted at the end of C++14 §30.6. Below is the content of that section.
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.
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.
Each future and shared_future is waited upon. When at least one is ready, all the futures are copied into the collection of the output (returned) future, maintaining the order of the futures in the input collection.
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.
The future returned by when_any will not throw an exception, but the futures and shared_futures held in the output collection may.
A future object that becomes ready when any of the input futures/shared_futures are ready.
future<tuple<>> if when_any is called with zero arguments.
future<vector<future<R>>> if the input cardinality is unknown at compile time and the iterator pair yields future<R>. R may be void. The order of the future in the output vector will be the same as given by the input iterator.
A new section 30.6.12 shall be inserted at the end of C++14 §30.6. 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.
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.
Each future and shared_future is waited upon. When at least one is ready, all the future are copied into the collection of the output (returned) future.
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.
After the copy, the future or shared_future that was first detected as being ready swaps its position with that of the last element of the result collection, so that the ready future or shared_future may be identified in constant time. Only one future or shared_future is thus moved.
future<vector<future<R>>> if the input cardinality is unknown at compile time and the iterator pair yields future<R>. R may be void.
future<vector<shared_future<R>>> if the input cardinality is unknown at compile time and the iterator pair yields shared_future<R>. R may be void.
A new section 30.6.13 shall be inserted at the end of C++14 §30.6. Below is the content of that section.
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.
future<decay_t<T>>, if function is given a value of type T.
future<void>, if the function is not given any inputs.
Returned future<decay_t<T>>, valid() == true.
Returned future<decay_t<T>>, is_ready() == true.
The authors are grateful to Agustín Bergé, Hartmut Kaiser and Anthony Williams for their contributions.

References: §30
 §30
 §30
 §30
 §30
 §30
 §30
 §30
 §30
 §30