- tmp/tmpm42z4c8s/{from.md → to.md} +242 -193
tmp/tmpm42z4c8s/{from.md → to.md}
RENAMED
|
@@ -2,13 +2,17 @@
|
|
| 2 |
|
| 3 |
### Overview <a id="futures.overview">[[futures.overview]]</a>
|
| 4 |
|
| 5 |
[[futures]] describes components that a C++program can use to retrieve
|
| 6 |
in one thread the result (value or exception) from a function that has
|
| 7 |
-
run in the same thread or another thread.
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
``` cpp
|
| 12 |
namespace std {
|
| 13 |
enum class future_errc {
|
| 14 |
broken_promise = implementation-defined,
|
|
@@ -53,36 +57,37 @@ namespace std {
|
|
| 53 |
|
| 54 |
template <class R> class shared_future;
|
| 55 |
template <class R> class shared_future<R&>;
|
| 56 |
template <> class shared_future<void>;
|
| 57 |
|
| 58 |
-
template <class> class packaged_task; //
|
| 59 |
template <class R, class... ArgTypes>
|
| 60 |
class packaged_task<R(ArgTypes...)>;
|
| 61 |
|
| 62 |
template <class R, class... ArgTypes>
|
| 63 |
void swap(packaged_task<R(ArgTypes...)>&, packaged_task<R(ArgTypes...)>&) noexcept;
|
| 64 |
|
| 65 |
template <class R, class Alloc>
|
| 66 |
struct uses_allocator<packaged_task<R>, Alloc>;
|
| 67 |
|
| 68 |
template <class F, class... Args>
|
| 69 |
-
future<
|
| 70 |
async(F&& f, Args&&... args);
|
| 71 |
template <class F, class... Args>
|
| 72 |
-
future<
|
| 73 |
async(launch policy, F&& f, Args&&... args);
|
| 74 |
}
|
| 75 |
```
|
| 76 |
|
| 77 |
The `enum` type `launch` is a bitmask type ([[bitmask.types]]) with
|
| 78 |
-
`launch::async` and `launch::deferred`
|
| 79 |
-
|
| 80 |
-
|
|
|
|
| 81 |
corresponding subset of available launch policies. Implementations can
|
| 82 |
extend the behavior of the first overload of `async()` by adding their
|
| 83 |
-
extensions to the launch policy under the “as if” rule.
|
| 84 |
|
| 85 |
The enum values of `future_errc` are distinct and not zero.
|
| 86 |
|
| 87 |
### Error handling <a id="futures.errors">[[futures.errors]]</a>
|
| 88 |
|
|
@@ -113,53 +118,67 @@ error_condition make_error_condition(future_errc e) noexcept;
|
|
| 113 |
|
| 114 |
``` cpp
|
| 115 |
namespace std {
|
| 116 |
class future_error : public logic_error {
|
| 117 |
public:
|
| 118 |
-
future_error(
|
| 119 |
|
| 120 |
const error_code& code() const noexcept;
|
| 121 |
const char* what() const noexcept;
|
|
|
|
|
|
|
| 122 |
};
|
| 123 |
}
|
| 124 |
```
|
| 125 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
``` cpp
|
| 127 |
const error_code& code() const noexcept;
|
| 128 |
```
|
| 129 |
|
| 130 |
-
*Returns:*
|
| 131 |
-
constructor.
|
| 132 |
|
| 133 |
``` cpp
|
| 134 |
const char* what() const noexcept;
|
| 135 |
```
|
| 136 |
|
| 137 |
*Returns:* An NTBSincorporating `code().message()`.
|
| 138 |
|
| 139 |
### Shared state <a id="futures.state">[[futures.state]]</a>
|
| 140 |
|
| 141 |
-
Many of the classes introduced in this
|
| 142 |
communicate results. This *shared state* consists of some state
|
| 143 |
information and some (possibly not yet evaluated) *result*, which can be
|
| 144 |
-
a (possibly void) value or an exception.
|
| 145 |
-
defined in this clause reference such shared state.
|
| 146 |
|
| 147 |
-
|
| 148 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
|
| 150 |
An *asynchronous return object* is an object that reads results from a
|
| 151 |
shared state. A *waiting function* of an asynchronous return object is
|
| 152 |
one that potentially blocks to wait for the shared state to be made
|
| 153 |
ready. If a waiting function can return before the state is made ready
|
| 154 |
because of a timeout ([[thread.req.lockable]]), then it is a *timed
|
| 155 |
waiting function*, otherwise it is a *non-timed waiting function*.
|
| 156 |
|
| 157 |
An *asynchronous provider* is an object that provides a result to a
|
| 158 |
shared state. The result of a shared state is set by respective
|
| 159 |
-
functions on the asynchronous provider.
|
| 160 |
-
|
|
|
|
|
|
|
|
|
|
| 161 |
description of those classes and functions that create such a state
|
| 162 |
object.
|
| 163 |
|
| 164 |
When an asynchronous return object or an asynchronous provider is said
|
| 165 |
to release its shared state, it means:
|
|
@@ -206,16 +225,18 @@ the shared state ready until the calling thread exits. The destruction
|
|
| 206 |
of each of that thread’s objects with thread storage duration (
|
| 207 |
[[basic.stc.thread]]) is sequenced before making that shared state
|
| 208 |
ready.
|
| 209 |
|
| 210 |
Access to the result of the same shared state may conflict (
|
| 211 |
-
[[intro.multithread]]).
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
|
|
|
|
|
|
| 217 |
|
| 218 |
### Class template `promise` <a id="futures.promise">[[futures.promise]]</a>
|
| 219 |
|
| 220 |
``` cpp
|
| 221 |
namespace std {
|
|
@@ -240,11 +261,10 @@ namespace std {
|
|
| 240 |
// setting the result
|
| 241 |
void set_value(see below);
|
| 242 |
void set_exception(exception_ptr p);
|
| 243 |
|
| 244 |
// setting the result with deferred notification
|
| 245 |
-
void set_value_at_thread_exit(const R& r);
|
| 246 |
void set_value_at_thread_exit(see below);
|
| 247 |
void set_exception_at_thread_exit(exception_ptr p);
|
| 248 |
};
|
| 249 |
template <class R>
|
| 250 |
void swap(promise<R>& x, promise<R>& y) noexcept;
|
|
@@ -253,12 +273,12 @@ namespace std {
|
|
| 253 |
}
|
| 254 |
```
|
| 255 |
|
| 256 |
The implementation shall provide the template `promise` and two
|
| 257 |
specializations, `promise<R&>` and `promise<{}void>`. These differ only
|
| 258 |
-
in the argument type of the member
|
| 259 |
-
|
| 260 |
|
| 261 |
The `set_value`, `set_exception`, `set_value_at_thread_exit`, and
|
| 262 |
`set_exception_at_thread_exit` member functions behave as though they
|
| 263 |
acquire a single mutex associated with the promise object while updating
|
| 264 |
the promise object.
|
|
@@ -286,11 +306,11 @@ promise(promise&& rhs) noexcept;
|
|
| 286 |
```
|
| 287 |
|
| 288 |
*Effects:* constructs a new `promise` object and transfers ownership of
|
| 289 |
the shared state of `rhs` (if any) to the newly-constructed object.
|
| 290 |
|
| 291 |
-
`rhs` has no shared state.
|
| 292 |
|
| 293 |
``` cpp
|
| 294 |
~promise();
|
| 295 |
```
|
| 296 |
|
|
@@ -309,13 +329,13 @@ promise& operator=(promise&& rhs) noexcept;
|
|
| 309 |
void swap(promise& other) noexcept;
|
| 310 |
```
|
| 311 |
|
| 312 |
*Effects:* Exchanges the shared state of `*this` and `other`.
|
| 313 |
|
| 314 |
-
`*this` has the shared state (if any) that `other` had
|
| 315 |
-
to `swap`. `other` has the shared state (if any) that
|
| 316 |
-
to the call to `swap`.
|
| 317 |
|
| 318 |
``` cpp
|
| 319 |
future<R> get_future();
|
| 320 |
```
|
| 321 |
|
|
@@ -336,11 +356,11 @@ void promise::set_value(const R& r);
|
|
| 336 |
void promise::set_value(R&& r);
|
| 337 |
void promise<R&>::set_value(R& r);
|
| 338 |
void promise<void>::set_value();
|
| 339 |
```
|
| 340 |
|
| 341 |
-
*Effects:*
|
| 342 |
that state ready ([[futures.state]]).
|
| 343 |
|
| 344 |
*Throws:*
|
| 345 |
|
| 346 |
- `future_error` if its shared state already has a stored value or
|
|
@@ -358,11 +378,13 @@ that state ready ([[futures.state]]).
|
|
| 358 |
|
| 359 |
``` cpp
|
| 360 |
void set_exception(exception_ptr p);
|
| 361 |
```
|
| 362 |
|
| 363 |
-
*
|
|
|
|
|
|
|
| 364 |
state and makes that state ready ([[futures.state]]).
|
| 365 |
|
| 366 |
*Throws:* `future_error` if its shared state already has a stored value
|
| 367 |
or exception.
|
| 368 |
|
|
@@ -401,10 +423,12 @@ associated with the current thread have been destroyed.
|
|
| 401 |
|
| 402 |
``` cpp
|
| 403 |
void set_exception_at_thread_exit(exception_ptr p);
|
| 404 |
```
|
| 405 |
|
|
|
|
|
|
|
| 406 |
*Effects:* Stores the exception pointer `p` in the shared state without
|
| 407 |
making that state ready immediately. Schedules that state to be made
|
| 408 |
ready when the current thread exits, after all objects of thread storage
|
| 409 |
duration associated with the current thread have been destroyed.
|
| 410 |
|
|
@@ -416,14 +440,14 @@ duration associated with the current thread have been destroyed.
|
|
| 416 |
value or exception.
|
| 417 |
- `no_state` if `*this` has no shared state.
|
| 418 |
|
| 419 |
``` cpp
|
| 420 |
template <class R>
|
| 421 |
-
void swap(promise<R>& x, promise<R>& y);
|
| 422 |
```
|
| 423 |
|
| 424 |
-
*Effects:* `x.swap(y)`.
|
| 425 |
|
| 426 |
### Class template `future` <a id="futures.unique_future">[[futures.unique_future]]</a>
|
| 427 |
|
| 428 |
The class template `future` defines a type for asynchronous return
|
| 429 |
objects which do not share their shared state with other asynchronous
|
|
@@ -433,18 +457,23 @@ on asynchronous providers ([[futures.state]]) or by the move
|
|
| 433 |
constructor and shares its shared state with the original asynchronous
|
| 434 |
provider. The result (value or exception) of a `future` object can be
|
| 435 |
set by calling a respective function on an object that shares the same
|
| 436 |
shared state.
|
| 437 |
|
| 438 |
-
Member functions of `future` do not synchronize with
|
| 439 |
-
member functions of `shared_future`.
|
| 440 |
|
| 441 |
The effect of calling any member function other than the destructor, the
|
| 442 |
-
move-assignment operator, or `valid` on a `future` object for
|
| 443 |
-
`valid() == false` is undefined.
|
| 444 |
-
|
| 445 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 446 |
|
| 447 |
``` cpp
|
| 448 |
namespace std {
|
| 449 |
template <class R>
|
| 450 |
class future {
|
|
@@ -453,11 +482,11 @@ namespace std {
|
|
| 453 |
future(future&&) noexcept;
|
| 454 |
future(const future& rhs) = delete;
|
| 455 |
~future();
|
| 456 |
future& operator=(const future& rhs) = delete;
|
| 457 |
future& operator=(future&&) noexcept;
|
| 458 |
-
shared_future<R> share();
|
| 459 |
|
| 460 |
// retrieving the value
|
| 461 |
see below get();
|
| 462 |
|
| 463 |
// functions to check state
|
|
@@ -479,20 +508,20 @@ out in its description, below.
|
|
| 479 |
|
| 480 |
``` cpp
|
| 481 |
future() noexcept;
|
| 482 |
```
|
| 483 |
|
| 484 |
-
*Effects:*
|
| 485 |
a shared state.
|
| 486 |
|
| 487 |
-
`valid() == false`.
|
| 488 |
|
| 489 |
``` cpp
|
| 490 |
future(future&& rhs) noexcept;
|
| 491 |
```
|
| 492 |
|
| 493 |
-
*Effects:*
|
| 494 |
state that was originally referred to by `rhs` (if any).
|
| 495 |
|
| 496 |
*Postconditions:*
|
| 497 |
|
| 498 |
- `valid()` returns the same value as `rhs.valid()` prior to the
|
|
@@ -503,48 +532,51 @@ state that was originally referred to by `rhs` (if any).
|
|
| 503 |
~future();
|
| 504 |
```
|
| 505 |
|
| 506 |
*Effects:*
|
| 507 |
|
| 508 |
-
-
|
| 509 |
- destroys `*this`.
|
| 510 |
|
| 511 |
``` cpp
|
| 512 |
future& operator=(future&& rhs) noexcept;
|
| 513 |
```
|
| 514 |
|
| 515 |
*Effects:*
|
| 516 |
|
| 517 |
-
-
|
| 518 |
- move assigns the contents of `rhs` to `*this`.
|
| 519 |
|
| 520 |
*Postconditions:*
|
| 521 |
|
| 522 |
- `valid()` returns the same value as `rhs.valid()` prior to the
|
| 523 |
assignment.
|
| 524 |
- `rhs.valid() == false`.
|
| 525 |
|
| 526 |
``` cpp
|
| 527 |
-
shared_future<R> share();
|
| 528 |
```
|
| 529 |
|
| 530 |
*Returns:* `shared_future<R>(std::move(*this))`.
|
| 531 |
|
| 532 |
-
`valid() == false`.
|
| 533 |
|
| 534 |
``` cpp
|
| 535 |
R future::get();
|
| 536 |
R& future<R&>::get();
|
| 537 |
void future<void>::get();
|
| 538 |
```
|
| 539 |
|
| 540 |
-
*Note
|
| 541 |
specializations differ only in the return type and return value of the
|
| 542 |
-
member function `get`.
|
| 543 |
|
| 544 |
-
*Effects:*
|
| 545 |
-
|
|
|
|
|
|
|
|
|
|
| 546 |
|
| 547 |
*Returns:*
|
| 548 |
|
| 549 |
- `future::get()` returns the value `v` stored in the object’s shared
|
| 550 |
state as `std::move(v)`.
|
|
@@ -553,11 +585,11 @@ value stored in the shared state.
|
|
| 553 |
- `future<void>::get()` returns nothing.
|
| 554 |
|
| 555 |
*Throws:* the stored exception, if an exception was stored in the shared
|
| 556 |
state.
|
| 557 |
|
| 558 |
-
`valid() == false`.
|
| 559 |
|
| 560 |
``` cpp
|
| 561 |
bool valid() const noexcept;
|
| 562 |
```
|
| 563 |
|
|
@@ -565,18 +597,18 @@ bool valid() const noexcept;
|
|
| 565 |
|
| 566 |
``` cpp
|
| 567 |
void wait() const;
|
| 568 |
```
|
| 569 |
|
| 570 |
-
*Effects:*
|
| 571 |
|
| 572 |
``` cpp
|
| 573 |
template <class Rep, class Period>
|
| 574 |
future_status wait_for(const chrono::duration<Rep, Period>& rel_time) const;
|
| 575 |
```
|
| 576 |
|
| 577 |
-
*Effects:*
|
| 578 |
function ([[futures.async]]), otherwise blocks until the shared state
|
| 579 |
is ready or until the relative timeout ([[thread.req.timing]])
|
| 580 |
specified by `rel_time` has expired.
|
| 581 |
|
| 582 |
*Returns:*
|
|
@@ -593,11 +625,11 @@ specified by `rel_time` has expired.
|
|
| 593 |
``` cpp
|
| 594 |
template <class Clock, class Duration>
|
| 595 |
future_status wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
|
| 596 |
```
|
| 597 |
|
| 598 |
-
*Effects:*
|
| 599 |
function ([[futures.async]]), otherwise blocks until the shared state
|
| 600 |
is ready or until the absolute timeout ([[thread.req.timing]])
|
| 601 |
specified by `abs_time` has expired.
|
| 602 |
|
| 603 |
*Returns:*
|
|
@@ -621,31 +653,35 @@ can be created by conversion from a `future` object and shares its
|
|
| 621 |
shared state with the original asynchronous provider (
|
| 622 |
[[futures.state]]) of the shared state. The result (value or exception)
|
| 623 |
of a `shared_future` object can be set by calling a respective function
|
| 624 |
on an object that shares the same shared state.
|
| 625 |
|
| 626 |
-
Member functions of `shared_future` do not synchronize with
|
| 627 |
-
but they synchronize with the shared
|
| 628 |
|
| 629 |
The effect of calling any member function other than the destructor, the
|
| 630 |
-
move-assignment operator, or `valid()` on
|
| 631 |
-
which `valid() ==
|
| 632 |
-
|
| 633 |
-
|
| 634 |
-
`
|
|
|
|
|
|
|
|
|
|
|
|
|
| 635 |
|
| 636 |
``` cpp
|
| 637 |
namespace std {
|
| 638 |
template <class R>
|
| 639 |
class shared_future {
|
| 640 |
public:
|
| 641 |
shared_future() noexcept;
|
| 642 |
-
shared_future(const shared_future& rhs);
|
| 643 |
shared_future(future<R>&&) noexcept;
|
| 644 |
shared_future(shared_future&& rhs) noexcept;
|
| 645 |
~shared_future();
|
| 646 |
-
shared_future& operator=(const shared_future& rhs);
|
| 647 |
shared_future& operator=(shared_future&& rhs) noexcept;
|
| 648 |
|
| 649 |
// retrieving the value
|
| 650 |
see below get() const;
|
| 651 |
|
|
@@ -668,30 +704,30 @@ differ only in the return type and return value of the member function
|
|
| 668 |
|
| 669 |
``` cpp
|
| 670 |
shared_future() noexcept;
|
| 671 |
```
|
| 672 |
|
| 673 |
-
*Effects:*
|
| 674 |
refer to a shared state.
|
| 675 |
|
| 676 |
-
`valid() == false`.
|
| 677 |
|
| 678 |
``` cpp
|
| 679 |
-
shared_future(const shared_future& rhs);
|
| 680 |
```
|
| 681 |
|
| 682 |
-
*Effects:*
|
| 683 |
shared state as `rhs` (if any).
|
| 684 |
|
| 685 |
-
`valid()` returns the same value as `rhs.valid()`.
|
| 686 |
|
| 687 |
``` cpp
|
| 688 |
shared_future(future<R>&& rhs) noexcept;
|
| 689 |
shared_future(shared_future&& rhs) noexcept;
|
| 690 |
```
|
| 691 |
|
| 692 |
-
*Effects:*
|
| 693 |
shared state that was originally referred to by `rhs` (if any).
|
| 694 |
|
| 695 |
*Postconditions:*
|
| 696 |
|
| 697 |
- `valid()` returns the same value as `rhs.valid()` returned prior to
|
|
@@ -702,64 +738,67 @@ shared state that was originally referred to by `rhs` (if any).
|
|
| 702 |
~shared_future();
|
| 703 |
```
|
| 704 |
|
| 705 |
*Effects:*
|
| 706 |
|
| 707 |
-
-
|
| 708 |
- destroys `*this`.
|
| 709 |
|
| 710 |
``` cpp
|
| 711 |
shared_future& operator=(shared_future&& rhs) noexcept;
|
| 712 |
```
|
| 713 |
|
| 714 |
*Effects:*
|
| 715 |
|
| 716 |
-
-
|
| 717 |
- move assigns the contents of `rhs` to `*this`.
|
| 718 |
|
| 719 |
*Postconditions:*
|
| 720 |
|
| 721 |
- `valid()` returns the same value as `rhs.valid()` returned prior to
|
| 722 |
the assignment.
|
| 723 |
- `rhs.valid() == false`.
|
| 724 |
|
| 725 |
``` cpp
|
| 726 |
-
shared_future& operator=(const shared_future& rhs);
|
| 727 |
```
|
| 728 |
|
| 729 |
*Effects:*
|
| 730 |
|
| 731 |
-
-
|
| 732 |
-
- assigns the contents of `rhs` to `*this`. As a result,
|
| 733 |
-
to the same shared state as `rhs` (if
|
|
|
|
| 734 |
|
| 735 |
*Postconditions:* `valid() == rhs.valid()`.
|
| 736 |
|
| 737 |
``` cpp
|
| 738 |
const R& shared_future::get() const;
|
| 739 |
R& shared_future<R&>::get() const;
|
| 740 |
void shared_future<void>::get() const;
|
| 741 |
```
|
| 742 |
|
| 743 |
-
*Note
|
| 744 |
specializations differ only in the return type and return value of the
|
| 745 |
-
member function `get`.
|
| 746 |
|
| 747 |
-
*Note
|
| 748 |
unsynchronized, so programmers should apply only those operations on `R`
|
| 749 |
-
that do not introduce a data
|
|
|
|
| 750 |
|
| 751 |
*Effects:* `wait()`s until the shared state is ready, then retrieves the
|
| 752 |
value stored in the shared state.
|
| 753 |
|
| 754 |
*Returns:*
|
| 755 |
|
| 756 |
- `shared_future::get()` returns a const reference to the value stored
|
| 757 |
-
in the object’s shared state. Access through that
|
| 758 |
-
shared state has been destroyed produces undefined
|
| 759 |
-
be avoided by not storing the reference in any
|
| 760 |
-
lifetime than the `shared_future` object that
|
|
|
|
| 761 |
- `shared_future<R&>::get()` returns the reference stored as value in
|
| 762 |
the object’s shared state.
|
| 763 |
- `shared_future<void>::get()` returns nothing.
|
| 764 |
|
| 765 |
*Throws:* the stored exception, if an exception was stored in the shared
|
|
@@ -773,18 +812,18 @@ bool valid() const noexcept;
|
|
| 773 |
|
| 774 |
``` cpp
|
| 775 |
void wait() const;
|
| 776 |
```
|
| 777 |
|
| 778 |
-
*Effects:*
|
| 779 |
|
| 780 |
``` cpp
|
| 781 |
template <class Rep, class Period>
|
| 782 |
future_status wait_for(const chrono::duration<Rep, Period>& rel_time) const;
|
| 783 |
```
|
| 784 |
|
| 785 |
-
*Effects:*
|
| 786 |
function ([[futures.async]]), otherwise blocks until the shared state
|
| 787 |
is ready or until the relative timeout ([[thread.req.timing]])
|
| 788 |
specified by `rel_time` has expired.
|
| 789 |
|
| 790 |
*Returns:*
|
|
@@ -801,11 +840,11 @@ specified by `rel_time` has expired.
|
|
| 801 |
``` cpp
|
| 802 |
template <class Clock, class Duration>
|
| 803 |
future_status wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
|
| 804 |
```
|
| 805 |
|
| 806 |
-
*Effects:*
|
| 807 |
function ([[futures.async]]), otherwise blocks until the shared state
|
| 808 |
is ready or until the absolute timeout ([[thread.req.timing]])
|
| 809 |
specified by `abs_time` has expired.
|
| 810 |
|
| 811 |
*Returns:*
|
|
@@ -825,81 +864,91 @@ The function template `async` provides a mechanism to launch a function
|
|
| 825 |
potentially in a new thread and provides the result of the function in a
|
| 826 |
`future` object with which it shares a shared state.
|
| 827 |
|
| 828 |
``` cpp
|
| 829 |
template <class F, class... Args>
|
| 830 |
-
future<
|
|
|
|
| 831 |
template <class F, class... Args>
|
| 832 |
-
future<
|
|
|
|
| 833 |
```
|
| 834 |
|
| 835 |
*Requires:* `F` and each `Ti` in `Args` shall satisfy the
|
| 836 |
-
`MoveConstructible` requirements
|
| 837 |
-
|
| 838 |
-
|
| 839 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 840 |
|
| 841 |
*Effects:* The first function behaves the same as a call to the second
|
| 842 |
function with a `policy` argument of `launch::async | launch::deferred`
|
| 843 |
and the same arguments for `F` and `Args`. The second function creates a
|
| 844 |
shared state that is associated with the returned `future` object. The
|
| 845 |
further behavior of the second function depends on the `policy` argument
|
| 846 |
as follows (if more than one of these conditions applies, the
|
| 847 |
implementation may choose any of the corresponding policies):
|
| 848 |
|
| 849 |
-
-
|
| 850 |
-
*
|
| 851 |
-
*
|
| 852 |
[[thread.thread.constr]]) as if in a new thread of execution
|
| 853 |
-
represented by a `thread` object with the calls to *
|
| 854 |
being evaluated in the thread that called `async`. Any return value is
|
| 855 |
stored as the result in the shared state. Any exception propagated
|
| 856 |
-
from the execution of
|
| 857 |
-
*
|
| 858 |
-
|
| 859 |
-
|
| 860 |
-
|
| 861 |
-
-
|
| 862 |
-
*
|
| 863 |
-
*
|
| 864 |
-
|
| 865 |
-
|
| 866 |
-
|
| 867 |
-
|
| 868 |
-
|
| 869 |
-
|
| 870 |
-
|
| 871 |
-
|
| 872 |
-
|
| 873 |
-
|
| 874 |
-
|
| 875 |
-
|
| 876 |
-
|
| 877 |
-
|
| 878 |
together with other policies, such as when using a `policy` value of
|
| 879 |
`launch::async | launch::deferred`, implementations should defer
|
| 880 |
invocation or the selection of the policy when no more concurrency can
|
| 881 |
-
be effectively exploited.
|
| 882 |
- If no value is set in the launch policy, or a value is set that is
|
| 883 |
-
neither specified in this International Standard
|
| 884 |
-
implementation, the
|
| 885 |
|
| 886 |
*Returns:* An object of type
|
| 887 |
-
`future<
|
| 888 |
-
shared state created by this call to `async`.
|
| 889 |
-
|
| 890 |
-
|
| 891 |
-
|
|
|
|
|
|
|
| 892 |
|
| 893 |
*Synchronization:* Regardless of the provided `policy` argument,
|
| 894 |
|
| 895 |
- the invocation of `async` synchronizes with ([[intro.multithread]])
|
| 896 |
-
the invocation of `f`. This statement applies even when
|
| 897 |
-
corresponding `future` object is moved to another
|
|
|
|
| 898 |
- the completion of the function `f` is sequenced
|
| 899 |
-
before ([[intro.multithread]]) the shared state is made ready.
|
| 900 |
-
might not be called at all, so its completion might
|
|
|
|
| 901 |
|
| 902 |
If the implementation chooses the `launch::async` policy,
|
| 903 |
|
| 904 |
- a call to a waiting function on an asynchronous return object that
|
| 905 |
shares the shared state created by this `async` call shall block until
|
|
@@ -910,32 +959,23 @@ If the implementation chooses the `launch::async` policy,
|
|
| 910 |
successfully detects the ready status of the shared state or with the
|
| 911 |
return from the last function that releases the shared state,
|
| 912 |
whichever happens first.
|
| 913 |
|
| 914 |
*Throws:* `system_error` if `policy == launch::async` and the
|
| 915 |
-
implementation is unable to start a new thread
|
|
|
|
| 916 |
|
| 917 |
*Error conditions:*
|
| 918 |
|
| 919 |
- `resource_unavailable_try_again` — if `policy == launch::async` and
|
| 920 |
the system is unable to start a new thread.
|
| 921 |
|
| 922 |
-
``
|
| 923 |
-
|
| 924 |
-
|
| 925 |
-
|
| 926 |
-
|
| 927 |
-
int tmp = work1(value);
|
| 928 |
-
return tmp + handle.get(); // #1
|
| 929 |
-
}
|
| 930 |
-
```
|
| 931 |
-
|
| 932 |
-
Line \#1 might not result in concurrency because the `async` call uses
|
| 933 |
-
the default policy, which may use `launch::deferred`, in which case the
|
| 934 |
-
lambda might not be invoked until the `get()` call; in that case,
|
| 935 |
-
`work1` and `work2` are called on the same thread and there is no
|
| 936 |
-
concurrency.
|
| 937 |
|
| 938 |
### Class template `packaged_task` <a id="futures.task">[[futures.task]]</a>
|
| 939 |
|
| 940 |
The class template `packaged_task` defines a type for wrapping a
|
| 941 |
function or callable object so that the return value of the function or
|
|
@@ -946,21 +986,19 @@ and the result (whether normal or exceptional) stored in the shared
|
|
| 946 |
state. Any futures that share the shared state will then be able to
|
| 947 |
access the stored result.
|
| 948 |
|
| 949 |
``` cpp
|
| 950 |
namespace std {
|
| 951 |
-
template<class> class packaged_task; //
|
| 952 |
|
| 953 |
template<class R, class... ArgTypes>
|
| 954 |
class packaged_task<R(ArgTypes...)> {
|
| 955 |
public:
|
| 956 |
// construction and destruction
|
| 957 |
packaged_task() noexcept;
|
| 958 |
template <class F>
|
| 959 |
explicit packaged_task(F&& f);
|
| 960 |
-
template <class F, class Allocator>
|
| 961 |
-
explicit packaged_task(allocator_arg_t, const Allocator& a, F&& f);
|
| 962 |
~packaged_task();
|
| 963 |
|
| 964 |
// no copy
|
| 965 |
packaged_task(const packaged_task&) = delete;
|
| 966 |
packaged_task& operator=(const packaged_task&) = delete;
|
|
@@ -992,73 +1030,73 @@ namespace std {
|
|
| 992 |
|
| 993 |
``` cpp
|
| 994 |
packaged_task() noexcept;
|
| 995 |
```
|
| 996 |
|
| 997 |
-
*Effects:*
|
| 998 |
no stored task.
|
| 999 |
|
| 1000 |
``` cpp
|
| 1001 |
template <class F>
|
| 1002 |
packaged_task(F&& f);
|
| 1003 |
-
template <class F, class Allocator>
|
| 1004 |
-
explicit packaged_task(allocator_arg_t, const Allocator& a, F&& f);
|
| 1005 |
```
|
| 1006 |
|
| 1007 |
-
*Requires:* *
|
| 1008 |
are values of the corresponding types in `ArgTypes...`, shall be a valid
|
| 1009 |
expression. Invoking a copy of `f` shall behave the same as invoking
|
| 1010 |
`f`.
|
| 1011 |
|
| 1012 |
-
*Remarks:*
|
| 1013 |
-
|
| 1014 |
-
`std::packaged_task<R(ArgTypes...)>`.
|
| 1015 |
|
| 1016 |
-
*Effects:*
|
| 1017 |
-
and initializes the object’s stored task with `std::forward<F>(f)`.
|
| 1018 |
-
constructors that take an `Allocator` argument use it to allocate memory
|
| 1019 |
-
needed to store the internal data structures.
|
| 1020 |
|
| 1021 |
-
*Throws:*
|
| 1022 |
-
|
| 1023 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1024 |
|
| 1025 |
``` cpp
|
| 1026 |
packaged_task(packaged_task&& rhs) noexcept;
|
| 1027 |
```
|
| 1028 |
|
| 1029 |
-
*Effects:*
|
| 1030 |
ownership of `rhs`’s shared state to `*this`, leaving `rhs` with no
|
| 1031 |
shared state. Moves the stored task from `rhs` to `*this`.
|
| 1032 |
|
| 1033 |
-
`rhs` has no shared state.
|
| 1034 |
|
| 1035 |
``` cpp
|
| 1036 |
packaged_task& operator=(packaged_task&& rhs) noexcept;
|
| 1037 |
```
|
| 1038 |
|
| 1039 |
*Effects:*
|
| 1040 |
|
| 1041 |
-
-
|
| 1042 |
-
- `packaged_task(std::move(rhs)).swap(*this)`.
|
| 1043 |
|
| 1044 |
``` cpp
|
| 1045 |
~packaged_task();
|
| 1046 |
```
|
| 1047 |
|
| 1048 |
-
*Effects:* Abandons any shared state
|
| 1049 |
|
| 1050 |
``` cpp
|
| 1051 |
void swap(packaged_task& other) noexcept;
|
| 1052 |
```
|
| 1053 |
|
| 1054 |
-
*Effects:*
|
| 1055 |
`other`.
|
| 1056 |
|
| 1057 |
-
`*this` has the same shared state and stored task (if
|
| 1058 |
-
prior to the call to `swap`. `other` has the same shared
|
| 1059 |
-
stored task (if any) as `*this` prior to the call to `swap`.
|
| 1060 |
|
| 1061 |
``` cpp
|
| 1062 |
bool valid() const noexcept;
|
| 1063 |
```
|
| 1064 |
|
|
@@ -1081,14 +1119,14 @@ future<R> get_future();
|
|
| 1081 |
|
| 1082 |
``` cpp
|
| 1083 |
void operator()(ArgTypes... args);
|
| 1084 |
```
|
| 1085 |
|
| 1086 |
-
*Effects:* *
|
| 1087 |
-
task of `*this` and `t1, t2, ..., tN` are the values in
|
| 1088 |
-
the task returns normally, the return value is stored as
|
| 1089 |
-
asynchronous result in the shared state of `*this`, otherwise the
|
| 1090 |
exception thrown by the task is stored. The shared state of `*this` is
|
| 1091 |
made ready, and any threads blocked in a function waiting for the shared
|
| 1092 |
state of `*this` to become ready are unblocked.
|
| 1093 |
|
| 1094 |
*Throws:* a `future_error` exception object if there is no shared state
|
|
@@ -1102,18 +1140,18 @@ or the stored task has already been invoked.
|
|
| 1102 |
|
| 1103 |
``` cpp
|
| 1104 |
void make_ready_at_thread_exit(ArgTypes... args);
|
| 1105 |
```
|
| 1106 |
|
| 1107 |
-
*Effects:* *
|
| 1108 |
-
task and `t1, t2, ..., tN` are the values in `args...`. If the
|
| 1109 |
-
returns normally, the return value is stored as the asynchronous
|
| 1110 |
-
in the shared state of `*this`, otherwise the exception thrown by
|
| 1111 |
-
task is stored. In either case, this shall be done without making
|
| 1112 |
-
state ready ([[futures.state]]) immediately. Schedules the shared
|
| 1113 |
-
to be made ready when the current thread exits, after all objects
|
| 1114 |
-
thread storage duration associated with the current thread have been
|
| 1115 |
destroyed.
|
| 1116 |
|
| 1117 |
*Throws:* `future_error` if an error condition occurs.
|
| 1118 |
|
| 1119 |
*Error conditions:*
|
|
@@ -1124,13 +1162,15 @@ destroyed.
|
|
| 1124 |
|
| 1125 |
``` cpp
|
| 1126 |
void reset();
|
| 1127 |
```
|
| 1128 |
|
| 1129 |
-
*Effects:*
|
| 1130 |
-
task stored in `*this`.
|
| 1131 |
-
|
|
|
|
|
|
|
| 1132 |
|
| 1133 |
*Throws:*
|
| 1134 |
|
| 1135 |
- `bad_alloc` if memory for the new shared state could not be allocated.
|
| 1136 |
- any exception thrown by the move constructor of the task stored in the
|
|
@@ -1143,11 +1183,11 @@ The old state is abandoned ([[futures.state]]).
|
|
| 1143 |
``` cpp
|
| 1144 |
template <class R, class... ArgTypes>
|
| 1145 |
void swap(packaged_task<R(ArgTypes...)>& x, packaged_task<R(ArgTypes...)>& y) noexcept;
|
| 1146 |
```
|
| 1147 |
|
| 1148 |
-
*Effects:* `x.swap(y)`
|
| 1149 |
|
| 1150 |
``` cpp
|
| 1151 |
template <class R, class Alloc>
|
| 1152 |
struct uses_allocator<packaged_task<R>, Alloc>
|
| 1153 |
: true_type { };
|
|
@@ -1161,12 +1201,14 @@ template <class R, class Alloc>
|
|
| 1161 |
[atomics]: atomics.md#atomics
|
| 1162 |
[basic.life]: basic.md#basic.life
|
| 1163 |
[basic.stc.thread]: basic.md#basic.stc.thread
|
| 1164 |
[bitmask.types]: library.md#bitmask.types
|
| 1165 |
[class]: class.md#class
|
|
|
|
| 1166 |
[except.terminate]: except.md#except.terminate
|
| 1167 |
[func.require]: utilities.md#func.require
|
|
|
|
| 1168 |
[futures]: #futures
|
| 1169 |
[futures.async]: #futures.async
|
| 1170 |
[futures.errors]: #futures.errors
|
| 1171 |
[futures.future_error]: #futures.future_error
|
| 1172 |
[futures.overview]: #futures.overview
|
|
@@ -1176,24 +1218,28 @@ template <class R, class Alloc>
|
|
| 1176 |
[futures.task]: #futures.task
|
| 1177 |
[futures.task.members]: #futures.task.members
|
| 1178 |
[futures.task.nonmembers]: #futures.task.nonmembers
|
| 1179 |
[futures.unique_future]: #futures.unique_future
|
| 1180 |
[intro.multithread]: intro.md#intro.multithread
|
|
|
|
| 1181 |
[res.on.data.races]: library.md#res.on.data.races
|
| 1182 |
[res.on.exception.handling]: library.md#res.on.exception.handling
|
|
|
|
| 1183 |
[syserr]: diagnostics.md#syserr
|
| 1184 |
[syserr.syserr]: diagnostics.md#syserr.syserr
|
| 1185 |
[tab:thread.lib.summary]: #tab:thread.lib.summary
|
| 1186 |
[thread]: #thread
|
| 1187 |
[thread.condition]: #thread.condition
|
| 1188 |
[thread.condition.condvar]: #thread.condition.condvar
|
| 1189 |
[thread.condition.condvarany]: #thread.condition.condvarany
|
|
|
|
| 1190 |
[thread.decaycopy]: #thread.decaycopy
|
| 1191 |
[thread.general]: #thread.general
|
| 1192 |
[thread.lock]: #thread.lock
|
| 1193 |
[thread.lock.algorithm]: #thread.lock.algorithm
|
| 1194 |
[thread.lock.guard]: #thread.lock.guard
|
|
|
|
| 1195 |
[thread.lock.shared]: #thread.lock.shared
|
| 1196 |
[thread.lock.shared.cons]: #thread.lock.shared.cons
|
| 1197 |
[thread.lock.shared.locking]: #thread.lock.shared.locking
|
| 1198 |
[thread.lock.shared.mod]: #thread.lock.shared.mod
|
| 1199 |
[thread.lock.shared.obs]: #thread.lock.shared.obs
|
|
@@ -1219,12 +1265,15 @@ template <class R, class Alloc>
|
|
| 1219 |
[thread.req.lockable.req]: #thread.req.lockable.req
|
| 1220 |
[thread.req.lockable.timed]: #thread.req.lockable.timed
|
| 1221 |
[thread.req.native]: #thread.req.native
|
| 1222 |
[thread.req.paramname]: #thread.req.paramname
|
| 1223 |
[thread.req.timing]: #thread.req.timing
|
|
|
|
|
|
|
| 1224 |
[thread.sharedtimedmutex.class]: #thread.sharedtimedmutex.class
|
| 1225 |
[thread.sharedtimedmutex.requirements]: #thread.sharedtimedmutex.requirements
|
|
|
|
| 1226 |
[thread.thread.algorithm]: #thread.thread.algorithm
|
| 1227 |
[thread.thread.assign]: #thread.thread.assign
|
| 1228 |
[thread.thread.class]: #thread.thread.class
|
| 1229 |
[thread.thread.constr]: #thread.thread.constr
|
| 1230 |
[thread.thread.destr]: #thread.thread.destr
|
|
|
|
| 2 |
|
| 3 |
### Overview <a id="futures.overview">[[futures.overview]]</a>
|
| 4 |
|
| 5 |
[[futures]] describes components that a C++program can use to retrieve
|
| 6 |
in one thread the result (value or exception) from a function that has
|
| 7 |
+
run in the same thread or another thread.
|
| 8 |
+
|
| 9 |
+
[*Note 1*: These components are not restricted to multi-threaded
|
| 10 |
+
programs but can be useful in single-threaded programs as
|
| 11 |
+
well. — *end note*]
|
| 12 |
+
|
| 13 |
+
### Header `<future>` synopsis <a id="future.syn">[[future.syn]]</a>
|
| 14 |
|
| 15 |
``` cpp
|
| 16 |
namespace std {
|
| 17 |
enum class future_errc {
|
| 18 |
broken_promise = implementation-defined,
|
|
|
|
| 57 |
|
| 58 |
template <class R> class shared_future;
|
| 59 |
template <class R> class shared_future<R&>;
|
| 60 |
template <> class shared_future<void>;
|
| 61 |
|
| 62 |
+
template <class> class packaged_task; // not defined
|
| 63 |
template <class R, class... ArgTypes>
|
| 64 |
class packaged_task<R(ArgTypes...)>;
|
| 65 |
|
| 66 |
template <class R, class... ArgTypes>
|
| 67 |
void swap(packaged_task<R(ArgTypes...)>&, packaged_task<R(ArgTypes...)>&) noexcept;
|
| 68 |
|
| 69 |
template <class R, class Alloc>
|
| 70 |
struct uses_allocator<packaged_task<R>, Alloc>;
|
| 71 |
|
| 72 |
template <class F, class... Args>
|
| 73 |
+
future<invoke_result_t<decay_t<F>, decay_t<Args>...>>
|
| 74 |
async(F&& f, Args&&... args);
|
| 75 |
template <class F, class... Args>
|
| 76 |
+
future<invoke_result_t<decay_t<F>, decay_t<Args>...>>
|
| 77 |
async(launch policy, F&& f, Args&&... args);
|
| 78 |
}
|
| 79 |
```
|
| 80 |
|
| 81 |
The `enum` type `launch` is a bitmask type ([[bitmask.types]]) with
|
| 82 |
+
elements `launch::async` and `launch::deferred`.
|
| 83 |
+
|
| 84 |
+
[*Note 1*: Implementations can provide bitmasks to specify restrictions
|
| 85 |
+
on task interaction by functions launched by `async()` applicable to a
|
| 86 |
corresponding subset of available launch policies. Implementations can
|
| 87 |
extend the behavior of the first overload of `async()` by adding their
|
| 88 |
+
extensions to the launch policy under the “as if” rule. — *end note*]
|
| 89 |
|
| 90 |
The enum values of `future_errc` are distinct and not zero.
|
| 91 |
|
| 92 |
### Error handling <a id="futures.errors">[[futures.errors]]</a>
|
| 93 |
|
|
|
|
| 118 |
|
| 119 |
``` cpp
|
| 120 |
namespace std {
|
| 121 |
class future_error : public logic_error {
|
| 122 |
public:
|
| 123 |
+
explicit future_error(future_errc e);
|
| 124 |
|
| 125 |
const error_code& code() const noexcept;
|
| 126 |
const char* what() const noexcept;
|
| 127 |
+
private:
|
| 128 |
+
error_code ec_; // exposition only
|
| 129 |
};
|
| 130 |
}
|
| 131 |
```
|
| 132 |
|
| 133 |
+
``` cpp
|
| 134 |
+
explicit future_error(future_errc e);
|
| 135 |
+
```
|
| 136 |
+
|
| 137 |
+
*Effects:* Constructs an object of class `future_error` and initializes
|
| 138 |
+
`ec_` with `make_error_code(e)`.
|
| 139 |
+
|
| 140 |
``` cpp
|
| 141 |
const error_code& code() const noexcept;
|
| 142 |
```
|
| 143 |
|
| 144 |
+
*Returns:* `ec_`.
|
|
|
|
| 145 |
|
| 146 |
``` cpp
|
| 147 |
const char* what() const noexcept;
|
| 148 |
```
|
| 149 |
|
| 150 |
*Returns:* An NTBSincorporating `code().message()`.
|
| 151 |
|
| 152 |
### Shared state <a id="futures.state">[[futures.state]]</a>
|
| 153 |
|
| 154 |
+
Many of the classes introduced in this subclause use some state to
|
| 155 |
communicate results. This *shared state* consists of some state
|
| 156 |
information and some (possibly not yet evaluated) *result*, which can be
|
| 157 |
+
a (possibly void) value or an exception.
|
|
|
|
| 158 |
|
| 159 |
+
[*Note 1*: Futures, promises, and tasks defined in this clause
|
| 160 |
+
reference such shared state. — *end note*]
|
| 161 |
+
|
| 162 |
+
[*Note 2*: The result can be any kind of object including a function to
|
| 163 |
+
compute that result, as used by `async` when `policy` is
|
| 164 |
+
`launch::deferred`. — *end note*]
|
| 165 |
|
| 166 |
An *asynchronous return object* is an object that reads results from a
|
| 167 |
shared state. A *waiting function* of an asynchronous return object is
|
| 168 |
one that potentially blocks to wait for the shared state to be made
|
| 169 |
ready. If a waiting function can return before the state is made ready
|
| 170 |
because of a timeout ([[thread.req.lockable]]), then it is a *timed
|
| 171 |
waiting function*, otherwise it is a *non-timed waiting function*.
|
| 172 |
|
| 173 |
An *asynchronous provider* is an object that provides a result to a
|
| 174 |
shared state. The result of a shared state is set by respective
|
| 175 |
+
functions on the asynchronous provider.
|
| 176 |
+
|
| 177 |
+
[*Note 3*: Such as promises or tasks. — *end note*]
|
| 178 |
+
|
| 179 |
+
The means of setting the result of a shared state is specified in the
|
| 180 |
description of those classes and functions that create such a state
|
| 181 |
object.
|
| 182 |
|
| 183 |
When an asynchronous return object or an asynchronous provider is said
|
| 184 |
to release its shared state, it means:
|
|
|
|
| 225 |
of each of that thread’s objects with thread storage duration (
|
| 226 |
[[basic.stc.thread]]) is sequenced before making that shared state
|
| 227 |
ready.
|
| 228 |
|
| 229 |
Access to the result of the same shared state may conflict (
|
| 230 |
+
[[intro.multithread]]).
|
| 231 |
+
|
| 232 |
+
[*Note 4*: This explicitly specifies that the result of the shared
|
| 233 |
+
state is visible in the objects that reference this state in the sense
|
| 234 |
+
of data race avoidance ([[res.on.data.races]]). For example, concurrent
|
| 235 |
+
accesses through references returned by `shared_future::get()` (
|
| 236 |
+
[[futures.shared_future]]) must either use read-only operations or
|
| 237 |
+
provide additional synchronization. — *end note*]
|
| 238 |
|
| 239 |
### Class template `promise` <a id="futures.promise">[[futures.promise]]</a>
|
| 240 |
|
| 241 |
``` cpp
|
| 242 |
namespace std {
|
|
|
|
| 261 |
// setting the result
|
| 262 |
void set_value(see below);
|
| 263 |
void set_exception(exception_ptr p);
|
| 264 |
|
| 265 |
// setting the result with deferred notification
|
|
|
|
| 266 |
void set_value_at_thread_exit(see below);
|
| 267 |
void set_exception_at_thread_exit(exception_ptr p);
|
| 268 |
};
|
| 269 |
template <class R>
|
| 270 |
void swap(promise<R>& x, promise<R>& y) noexcept;
|
|
|
|
| 273 |
}
|
| 274 |
```
|
| 275 |
|
| 276 |
The implementation shall provide the template `promise` and two
|
| 277 |
specializations, `promise<R&>` and `promise<{}void>`. These differ only
|
| 278 |
+
in the argument type of the member functions `set_value` and
|
| 279 |
+
`set_value_at_thread_exit`, as set out in their descriptions, below.
|
| 280 |
|
| 281 |
The `set_value`, `set_exception`, `set_value_at_thread_exit`, and
|
| 282 |
`set_exception_at_thread_exit` member functions behave as though they
|
| 283 |
acquire a single mutex associated with the promise object while updating
|
| 284 |
the promise object.
|
|
|
|
| 306 |
```
|
| 307 |
|
| 308 |
*Effects:* constructs a new `promise` object and transfers ownership of
|
| 309 |
the shared state of `rhs` (if any) to the newly-constructed object.
|
| 310 |
|
| 311 |
+
*Postconditions:* `rhs` has no shared state.
|
| 312 |
|
| 313 |
``` cpp
|
| 314 |
~promise();
|
| 315 |
```
|
| 316 |
|
|
|
|
| 329 |
void swap(promise& other) noexcept;
|
| 330 |
```
|
| 331 |
|
| 332 |
*Effects:* Exchanges the shared state of `*this` and `other`.
|
| 333 |
|
| 334 |
+
*Postconditions:* `*this` has the shared state (if any) that `other` had
|
| 335 |
+
prior to the call to `swap`. `other` has the shared state (if any) that
|
| 336 |
+
`*this` had prior to the call to `swap`.
|
| 337 |
|
| 338 |
``` cpp
|
| 339 |
future<R> get_future();
|
| 340 |
```
|
| 341 |
|
|
|
|
| 356 |
void promise::set_value(R&& r);
|
| 357 |
void promise<R&>::set_value(R& r);
|
| 358 |
void promise<void>::set_value();
|
| 359 |
```
|
| 360 |
|
| 361 |
+
*Effects:* Atomically stores the value `r` in the shared state and makes
|
| 362 |
that state ready ([[futures.state]]).
|
| 363 |
|
| 364 |
*Throws:*
|
| 365 |
|
| 366 |
- `future_error` if its shared state already has a stored value or
|
|
|
|
| 378 |
|
| 379 |
``` cpp
|
| 380 |
void set_exception(exception_ptr p);
|
| 381 |
```
|
| 382 |
|
| 383 |
+
*Requires:* `p` is not null.
|
| 384 |
+
|
| 385 |
+
*Effects:* Atomically stores the exception pointer `p` in the shared
|
| 386 |
state and makes that state ready ([[futures.state]]).
|
| 387 |
|
| 388 |
*Throws:* `future_error` if its shared state already has a stored value
|
| 389 |
or exception.
|
| 390 |
|
|
|
|
| 423 |
|
| 424 |
``` cpp
|
| 425 |
void set_exception_at_thread_exit(exception_ptr p);
|
| 426 |
```
|
| 427 |
|
| 428 |
+
*Requires:* `p` is not null.
|
| 429 |
+
|
| 430 |
*Effects:* Stores the exception pointer `p` in the shared state without
|
| 431 |
making that state ready immediately. Schedules that state to be made
|
| 432 |
ready when the current thread exits, after all objects of thread storage
|
| 433 |
duration associated with the current thread have been destroyed.
|
| 434 |
|
|
|
|
| 440 |
value or exception.
|
| 441 |
- `no_state` if `*this` has no shared state.
|
| 442 |
|
| 443 |
``` cpp
|
| 444 |
template <class R>
|
| 445 |
+
void swap(promise<R>& x, promise<R>& y) noexcept;
|
| 446 |
```
|
| 447 |
|
| 448 |
+
*Effects:* As if by `x.swap(y)`.
|
| 449 |
|
| 450 |
### Class template `future` <a id="futures.unique_future">[[futures.unique_future]]</a>
|
| 451 |
|
| 452 |
The class template `future` defines a type for asynchronous return
|
| 453 |
objects which do not share their shared state with other asynchronous
|
|
|
|
| 457 |
constructor and shares its shared state with the original asynchronous
|
| 458 |
provider. The result (value or exception) of a `future` object can be
|
| 459 |
set by calling a respective function on an object that shares the same
|
| 460 |
shared state.
|
| 461 |
|
| 462 |
+
[*Note 1*: Member functions of `future` do not synchronize with
|
| 463 |
+
themselves or with member functions of `shared_future`. — *end note*]
|
| 464 |
|
| 465 |
The effect of calling any member function other than the destructor, the
|
| 466 |
+
move-assignment operator, `share`, or `valid` on a `future` object for
|
| 467 |
+
which `valid() == false` is undefined.
|
| 468 |
+
|
| 469 |
+
[*Note 2*: It is valid to move from a future object for which
|
| 470 |
+
`valid() == false`. — *end note*]
|
| 471 |
+
|
| 472 |
+
[*Note 3*: Implementations are encouraged to detect this case and throw
|
| 473 |
+
an object of type `future_error` with an error condition of
|
| 474 |
+
`future_errc::no_state`. — *end note*]
|
| 475 |
|
| 476 |
``` cpp
|
| 477 |
namespace std {
|
| 478 |
template <class R>
|
| 479 |
class future {
|
|
|
|
| 482 |
future(future&&) noexcept;
|
| 483 |
future(const future& rhs) = delete;
|
| 484 |
~future();
|
| 485 |
future& operator=(const future& rhs) = delete;
|
| 486 |
future& operator=(future&&) noexcept;
|
| 487 |
+
shared_future<R> share() noexcept;
|
| 488 |
|
| 489 |
// retrieving the value
|
| 490 |
see below get();
|
| 491 |
|
| 492 |
// functions to check state
|
|
|
|
| 508 |
|
| 509 |
``` cpp
|
| 510 |
future() noexcept;
|
| 511 |
```
|
| 512 |
|
| 513 |
+
*Effects:* Constructs an *empty* `future` object that does not refer to
|
| 514 |
a shared state.
|
| 515 |
|
| 516 |
+
*Postconditions:* `valid() == false`.
|
| 517 |
|
| 518 |
``` cpp
|
| 519 |
future(future&& rhs) noexcept;
|
| 520 |
```
|
| 521 |
|
| 522 |
+
*Effects:* Move constructs a `future` object that refers to the shared
|
| 523 |
state that was originally referred to by `rhs` (if any).
|
| 524 |
|
| 525 |
*Postconditions:*
|
| 526 |
|
| 527 |
- `valid()` returns the same value as `rhs.valid()` prior to the
|
|
|
|
| 532 |
~future();
|
| 533 |
```
|
| 534 |
|
| 535 |
*Effects:*
|
| 536 |
|
| 537 |
+
- Releases any shared state ([[futures.state]]);
|
| 538 |
- destroys `*this`.
|
| 539 |
|
| 540 |
``` cpp
|
| 541 |
future& operator=(future&& rhs) noexcept;
|
| 542 |
```
|
| 543 |
|
| 544 |
*Effects:*
|
| 545 |
|
| 546 |
+
- Releases any shared state ([[futures.state]]).
|
| 547 |
- move assigns the contents of `rhs` to `*this`.
|
| 548 |
|
| 549 |
*Postconditions:*
|
| 550 |
|
| 551 |
- `valid()` returns the same value as `rhs.valid()` prior to the
|
| 552 |
assignment.
|
| 553 |
- `rhs.valid() == false`.
|
| 554 |
|
| 555 |
``` cpp
|
| 556 |
+
shared_future<R> share() noexcept;
|
| 557 |
```
|
| 558 |
|
| 559 |
*Returns:* `shared_future<R>(std::move(*this))`.
|
| 560 |
|
| 561 |
+
*Postconditions:* `valid() == false`.
|
| 562 |
|
| 563 |
``` cpp
|
| 564 |
R future::get();
|
| 565 |
R& future<R&>::get();
|
| 566 |
void future<void>::get();
|
| 567 |
```
|
| 568 |
|
| 569 |
+
[*Note 1*: As described above, the template and its two required
|
| 570 |
specializations differ only in the return type and return value of the
|
| 571 |
+
member function `get`. — *end note*]
|
| 572 |
|
| 573 |
+
*Effects:*
|
| 574 |
+
|
| 575 |
+
- `wait()`s until the shared state is ready, then retrieves the value
|
| 576 |
+
stored in the shared state;
|
| 577 |
+
- releases any shared state ([[futures.state]]).
|
| 578 |
|
| 579 |
*Returns:*
|
| 580 |
|
| 581 |
- `future::get()` returns the value `v` stored in the object’s shared
|
| 582 |
state as `std::move(v)`.
|
|
|
|
| 585 |
- `future<void>::get()` returns nothing.
|
| 586 |
|
| 587 |
*Throws:* the stored exception, if an exception was stored in the shared
|
| 588 |
state.
|
| 589 |
|
| 590 |
+
*Postconditions:* `valid() == false`.
|
| 591 |
|
| 592 |
``` cpp
|
| 593 |
bool valid() const noexcept;
|
| 594 |
```
|
| 595 |
|
|
|
|
| 597 |
|
| 598 |
``` cpp
|
| 599 |
void wait() const;
|
| 600 |
```
|
| 601 |
|
| 602 |
+
*Effects:* Blocks until the shared state is ready.
|
| 603 |
|
| 604 |
``` cpp
|
| 605 |
template <class Rep, class Period>
|
| 606 |
future_status wait_for(const chrono::duration<Rep, Period>& rel_time) const;
|
| 607 |
```
|
| 608 |
|
| 609 |
+
*Effects:* None if the shared state contains a deferred
|
| 610 |
function ([[futures.async]]), otherwise blocks until the shared state
|
| 611 |
is ready or until the relative timeout ([[thread.req.timing]])
|
| 612 |
specified by `rel_time` has expired.
|
| 613 |
|
| 614 |
*Returns:*
|
|
|
|
| 625 |
``` cpp
|
| 626 |
template <class Clock, class Duration>
|
| 627 |
future_status wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
|
| 628 |
```
|
| 629 |
|
| 630 |
+
*Effects:* None if the shared state contains a deferred
|
| 631 |
function ([[futures.async]]), otherwise blocks until the shared state
|
| 632 |
is ready or until the absolute timeout ([[thread.req.timing]])
|
| 633 |
specified by `abs_time` has expired.
|
| 634 |
|
| 635 |
*Returns:*
|
|
|
|
| 653 |
shared state with the original asynchronous provider (
|
| 654 |
[[futures.state]]) of the shared state. The result (value or exception)
|
| 655 |
of a `shared_future` object can be set by calling a respective function
|
| 656 |
on an object that shares the same shared state.
|
| 657 |
|
| 658 |
+
[*Note 1*: Member functions of `shared_future` do not synchronize with
|
| 659 |
+
themselves, but they synchronize with the shared state. — *end note*]
|
| 660 |
|
| 661 |
The effect of calling any member function other than the destructor, the
|
| 662 |
+
move-assignment operator, the copy-assignment operator, or `valid()` on
|
| 663 |
+
a `shared_future` object for which `valid() == false` is undefined.
|
| 664 |
+
|
| 665 |
+
[*Note 2*: It is valid to copy or move from a `shared_future` object
|
| 666 |
+
for which `valid()` is `false`. — *end note*]
|
| 667 |
+
|
| 668 |
+
[*Note 3*: Implementations are encouraged to detect this case and throw
|
| 669 |
+
an object of type `future_error` with an error condition of
|
| 670 |
+
`future_errc::no_state`. — *end note*]
|
| 671 |
|
| 672 |
``` cpp
|
| 673 |
namespace std {
|
| 674 |
template <class R>
|
| 675 |
class shared_future {
|
| 676 |
public:
|
| 677 |
shared_future() noexcept;
|
| 678 |
+
shared_future(const shared_future& rhs) noexcept;
|
| 679 |
shared_future(future<R>&&) noexcept;
|
| 680 |
shared_future(shared_future&& rhs) noexcept;
|
| 681 |
~shared_future();
|
| 682 |
+
shared_future& operator=(const shared_future& rhs) noexcept;
|
| 683 |
shared_future& operator=(shared_future&& rhs) noexcept;
|
| 684 |
|
| 685 |
// retrieving the value
|
| 686 |
see below get() const;
|
| 687 |
|
|
|
|
| 704 |
|
| 705 |
``` cpp
|
| 706 |
shared_future() noexcept;
|
| 707 |
```
|
| 708 |
|
| 709 |
+
*Effects:* Constructs an *empty* `shared_future` object that does not
|
| 710 |
refer to a shared state.
|
| 711 |
|
| 712 |
+
*Postconditions:* `valid() == false`.
|
| 713 |
|
| 714 |
``` cpp
|
| 715 |
+
shared_future(const shared_future& rhs) noexcept;
|
| 716 |
```
|
| 717 |
|
| 718 |
+
*Effects:* Constructs a `shared_future` object that refers to the same
|
| 719 |
shared state as `rhs` (if any).
|
| 720 |
|
| 721 |
+
*Postconditions:* `valid()` returns the same value as `rhs.valid()`.
|
| 722 |
|
| 723 |
``` cpp
|
| 724 |
shared_future(future<R>&& rhs) noexcept;
|
| 725 |
shared_future(shared_future&& rhs) noexcept;
|
| 726 |
```
|
| 727 |
|
| 728 |
+
*Effects:* Move constructs a `shared_future` object that refers to the
|
| 729 |
shared state that was originally referred to by `rhs` (if any).
|
| 730 |
|
| 731 |
*Postconditions:*
|
| 732 |
|
| 733 |
- `valid()` returns the same value as `rhs.valid()` returned prior to
|
|
|
|
| 738 |
~shared_future();
|
| 739 |
```
|
| 740 |
|
| 741 |
*Effects:*
|
| 742 |
|
| 743 |
+
- Releases any shared state ([[futures.state]]);
|
| 744 |
- destroys `*this`.
|
| 745 |
|
| 746 |
``` cpp
|
| 747 |
shared_future& operator=(shared_future&& rhs) noexcept;
|
| 748 |
```
|
| 749 |
|
| 750 |
*Effects:*
|
| 751 |
|
| 752 |
+
- Releases any shared state ([[futures.state]]);
|
| 753 |
- move assigns the contents of `rhs` to `*this`.
|
| 754 |
|
| 755 |
*Postconditions:*
|
| 756 |
|
| 757 |
- `valid()` returns the same value as `rhs.valid()` returned prior to
|
| 758 |
the assignment.
|
| 759 |
- `rhs.valid() == false`.
|
| 760 |
|
| 761 |
``` cpp
|
| 762 |
+
shared_future& operator=(const shared_future& rhs) noexcept;
|
| 763 |
```
|
| 764 |
|
| 765 |
*Effects:*
|
| 766 |
|
| 767 |
+
- Releases any shared state ([[futures.state]]);
|
| 768 |
+
- assigns the contents of `rhs` to `*this`. \[*Note 4*: As a result,
|
| 769 |
+
`*this` refers to the same shared state as `rhs` (if
|
| 770 |
+
any). — *end note*]
|
| 771 |
|
| 772 |
*Postconditions:* `valid() == rhs.valid()`.
|
| 773 |
|
| 774 |
``` cpp
|
| 775 |
const R& shared_future::get() const;
|
| 776 |
R& shared_future<R&>::get() const;
|
| 777 |
void shared_future<void>::get() const;
|
| 778 |
```
|
| 779 |
|
| 780 |
+
[*Note 1*: As described above, the template and its two required
|
| 781 |
specializations differ only in the return type and return value of the
|
| 782 |
+
member function `get`. — *end note*]
|
| 783 |
|
| 784 |
+
[*Note 2*: Access to a value object stored in the shared state is
|
| 785 |
unsynchronized, so programmers should apply only those operations on `R`
|
| 786 |
+
that do not introduce a data
|
| 787 |
+
race ([[intro.multithread]]). — *end note*]
|
| 788 |
|
| 789 |
*Effects:* `wait()`s until the shared state is ready, then retrieves the
|
| 790 |
value stored in the shared state.
|
| 791 |
|
| 792 |
*Returns:*
|
| 793 |
|
| 794 |
- `shared_future::get()` returns a const reference to the value stored
|
| 795 |
+
in the object’s shared state. \[*Note 5*: Access through that
|
| 796 |
+
reference after the shared state has been destroyed produces undefined
|
| 797 |
+
behavior; this can be avoided by not storing the reference in any
|
| 798 |
+
storage with a greater lifetime than the `shared_future` object that
|
| 799 |
+
returned the reference. — *end note*]
|
| 800 |
- `shared_future<R&>::get()` returns the reference stored as value in
|
| 801 |
the object’s shared state.
|
| 802 |
- `shared_future<void>::get()` returns nothing.
|
| 803 |
|
| 804 |
*Throws:* the stored exception, if an exception was stored in the shared
|
|
|
|
| 812 |
|
| 813 |
``` cpp
|
| 814 |
void wait() const;
|
| 815 |
```
|
| 816 |
|
| 817 |
+
*Effects:* Blocks until the shared state is ready.
|
| 818 |
|
| 819 |
``` cpp
|
| 820 |
template <class Rep, class Period>
|
| 821 |
future_status wait_for(const chrono::duration<Rep, Period>& rel_time) const;
|
| 822 |
```
|
| 823 |
|
| 824 |
+
*Effects:* None if the shared state contains a deferred
|
| 825 |
function ([[futures.async]]), otherwise blocks until the shared state
|
| 826 |
is ready or until the relative timeout ([[thread.req.timing]])
|
| 827 |
specified by `rel_time` has expired.
|
| 828 |
|
| 829 |
*Returns:*
|
|
|
|
| 840 |
``` cpp
|
| 841 |
template <class Clock, class Duration>
|
| 842 |
future_status wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
|
| 843 |
```
|
| 844 |
|
| 845 |
+
*Effects:* None if the shared state contains a deferred
|
| 846 |
function ([[futures.async]]), otherwise blocks until the shared state
|
| 847 |
is ready or until the absolute timeout ([[thread.req.timing]])
|
| 848 |
specified by `abs_time` has expired.
|
| 849 |
|
| 850 |
*Returns:*
|
|
|
|
| 864 |
potentially in a new thread and provides the result of the function in a
|
| 865 |
`future` object with which it shares a shared state.
|
| 866 |
|
| 867 |
``` cpp
|
| 868 |
template <class F, class... Args>
|
| 869 |
+
future<invoke_result_t<decay_t<F>, decay_t<Args>...>>
|
| 870 |
+
async(F&& f, Args&&... args);
|
| 871 |
template <class F, class... Args>
|
| 872 |
+
future<invoke_result_t<decay_t<F>, decay_t<Args>...>>
|
| 873 |
+
async(launch policy, F&& f, Args&&... args);
|
| 874 |
```
|
| 875 |
|
| 876 |
*Requires:* `F` and each `Ti` in `Args` shall satisfy the
|
| 877 |
+
`MoveConstructible` requirements, and
|
| 878 |
+
|
| 879 |
+
``` cpp
|
| 880 |
+
INVOKE(DECAY_COPY(std::forward<F>(f)),
|
| 881 |
+
DECAY_COPY(std::forward<Args>(args))...) // see [func.require] [thread.thread.constr]
|
| 882 |
+
```
|
| 883 |
+
|
| 884 |
+
shall be a valid expression.
|
| 885 |
|
| 886 |
*Effects:* The first function behaves the same as a call to the second
|
| 887 |
function with a `policy` argument of `launch::async | launch::deferred`
|
| 888 |
and the same arguments for `F` and `Args`. The second function creates a
|
| 889 |
shared state that is associated with the returned `future` object. The
|
| 890 |
further behavior of the second function depends on the `policy` argument
|
| 891 |
as follows (if more than one of these conditions applies, the
|
| 892 |
implementation may choose any of the corresponding policies):
|
| 893 |
|
| 894 |
+
- If `launch::async` is set in `policy`, calls
|
| 895 |
+
*INVOKE*(*DECAY_COPY*(std::forward\<F\>(f)),
|
| 896 |
+
*DECAY_COPY*(std::forward\<Args\>(args))...) ([[func.require]],
|
| 897 |
[[thread.thread.constr]]) as if in a new thread of execution
|
| 898 |
+
represented by a `thread` object with the calls to *DECAY_COPY*()
|
| 899 |
being evaluated in the thread that called `async`. Any return value is
|
| 900 |
stored as the result in the shared state. Any exception propagated
|
| 901 |
+
from the execution of *INVOKE*(*DECAY_COPY*(std::forward\<F\>(f)),
|
| 902 |
+
*DECAY_COPY*(std::forward\<Args\>(args))...) is stored as the
|
| 903 |
+
exceptional result in the shared state. The `thread` object is stored
|
| 904 |
+
in the shared state and affects the behavior of any asynchronous
|
| 905 |
+
return objects that reference that state.
|
| 906 |
+
- If `launch::deferred` is set in `policy`, stores
|
| 907 |
+
*DECAY_COPY*(std::forward\<F\>(f)) and
|
| 908 |
+
*DECAY_COPY*(std::forward\<Args\>(args))... in the shared state. These
|
| 909 |
+
copies of `f` and `args` constitute a *deferred function*. Invocation
|
| 910 |
+
of the deferred function evaluates *INVOKE*(std::move(g),
|
| 911 |
+
std::move(xyz)) where `g` is the stored value of
|
| 912 |
+
*DECAY_COPY*(std::forward\<F\>(f)) and `xyz` is the stored copy of
|
| 913 |
+
*DECAY_COPY*(std::forward\<Args\>(args)).... Any return value is
|
| 914 |
+
stored as the result in the shared state. Any exception propagated
|
| 915 |
+
from the execution of the deferred function is stored as the
|
| 916 |
+
exceptional result in the shared state. The shared state is not made
|
| 917 |
+
ready until the function has completed. The first call to a non-timed
|
| 918 |
+
waiting function ([[futures.state]]) on an asynchronous return object
|
| 919 |
+
referring to this shared state shall invoke the deferred function in
|
| 920 |
+
the thread that called the waiting function. Once evaluation of
|
| 921 |
+
*INVOKE*(std::move(g), std::move(xyz)) begins, the function is no
|
| 922 |
+
longer considered deferred. \[*Note 1*: If this policy is specified
|
| 923 |
together with other policies, such as when using a `policy` value of
|
| 924 |
`launch::async | launch::deferred`, implementations should defer
|
| 925 |
invocation or the selection of the policy when no more concurrency can
|
| 926 |
+
be effectively exploited. — *end note*]
|
| 927 |
- If no value is set in the launch policy, or a value is set that is
|
| 928 |
+
neither specified in this International Standard nor by the
|
| 929 |
+
implementation, the behavior is undefined.
|
| 930 |
|
| 931 |
*Returns:* An object of type
|
| 932 |
+
`future<invoke_result_t<decay_t<F>, decay_t<Args>...>``>` that refers to
|
| 933 |
+
the shared state created by this call to `async`.
|
| 934 |
+
|
| 935 |
+
[*Note 1*: If a future obtained from `async` is moved outside the local
|
| 936 |
+
scope, other code that uses the future must be aware that the future’s
|
| 937 |
+
destructor may block for the shared state to become
|
| 938 |
+
ready. — *end note*]
|
| 939 |
|
| 940 |
*Synchronization:* Regardless of the provided `policy` argument,
|
| 941 |
|
| 942 |
- the invocation of `async` synchronizes with ([[intro.multithread]])
|
| 943 |
+
the invocation of `f`. \[*Note 2*: This statement applies even when
|
| 944 |
+
the corresponding `future` object is moved to another
|
| 945 |
+
thread. — *end note*] ; and
|
| 946 |
- the completion of the function `f` is sequenced
|
| 947 |
+
before ([[intro.multithread]]) the shared state is made ready.
|
| 948 |
+
\[*Note 3*: `f` might not be called at all, so its completion might
|
| 949 |
+
never happen. — *end note*]
|
| 950 |
|
| 951 |
If the implementation chooses the `launch::async` policy,
|
| 952 |
|
| 953 |
- a call to a waiting function on an asynchronous return object that
|
| 954 |
shares the shared state created by this `async` call shall block until
|
|
|
|
| 959 |
successfully detects the ready status of the shared state or with the
|
| 960 |
return from the last function that releases the shared state,
|
| 961 |
whichever happens first.
|
| 962 |
|
| 963 |
*Throws:* `system_error` if `policy == launch::async` and the
|
| 964 |
+
implementation is unable to start a new thread, or `std::bad_alloc` if
|
| 965 |
+
memory for the internal data structures could not be allocated.
|
| 966 |
|
| 967 |
*Error conditions:*
|
| 968 |
|
| 969 |
- `resource_unavailable_try_again` — if `policy == launch::async` and
|
| 970 |
the system is unable to start a new thread.
|
| 971 |
|
| 972 |
+
[*Note 4*: Line \#1 might not result in concurrency because the `async`
|
| 973 |
+
call uses the default policy, which may use `launch::deferred`, in which
|
| 974 |
+
case the lambda might not be invoked until the `get()` call; in that
|
| 975 |
+
case, `work1` and `work2` are called on the same thread and there is no
|
| 976 |
+
concurrency. — *end note*]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 977 |
|
| 978 |
### Class template `packaged_task` <a id="futures.task">[[futures.task]]</a>
|
| 979 |
|
| 980 |
The class template `packaged_task` defines a type for wrapping a
|
| 981 |
function or callable object so that the return value of the function or
|
|
|
|
| 986 |
state. Any futures that share the shared state will then be able to
|
| 987 |
access the stored result.
|
| 988 |
|
| 989 |
``` cpp
|
| 990 |
namespace std {
|
| 991 |
+
template<class> class packaged_task; // not defined
|
| 992 |
|
| 993 |
template<class R, class... ArgTypes>
|
| 994 |
class packaged_task<R(ArgTypes...)> {
|
| 995 |
public:
|
| 996 |
// construction and destruction
|
| 997 |
packaged_task() noexcept;
|
| 998 |
template <class F>
|
| 999 |
explicit packaged_task(F&& f);
|
|
|
|
|
|
|
| 1000 |
~packaged_task();
|
| 1001 |
|
| 1002 |
// no copy
|
| 1003 |
packaged_task(const packaged_task&) = delete;
|
| 1004 |
packaged_task& operator=(const packaged_task&) = delete;
|
|
|
|
| 1030 |
|
| 1031 |
``` cpp
|
| 1032 |
packaged_task() noexcept;
|
| 1033 |
```
|
| 1034 |
|
| 1035 |
+
*Effects:* Constructs a `packaged_task` object with no shared state and
|
| 1036 |
no stored task.
|
| 1037 |
|
| 1038 |
``` cpp
|
| 1039 |
template <class F>
|
| 1040 |
packaged_task(F&& f);
|
|
|
|
|
|
|
| 1041 |
```
|
| 1042 |
|
| 1043 |
+
*Requires:* *INVOKE*\<R\>(f, t1, t2, ..., tN), where `t1, t2, ..., tN`
|
| 1044 |
are values of the corresponding types in `ArgTypes...`, shall be a valid
|
| 1045 |
expression. Invoking a copy of `f` shall behave the same as invoking
|
| 1046 |
`f`.
|
| 1047 |
|
| 1048 |
+
*Remarks:* This constructor shall not participate in overload resolution
|
| 1049 |
+
if `decay_t<F>` is the same type as `packaged_task<R(ArgTypes...)>`.
|
|
|
|
| 1050 |
|
| 1051 |
+
*Effects:* Constructs a new `packaged_task` object with a shared state
|
| 1052 |
+
and initializes the object’s stored task with `std::forward<F>(f)`.
|
|
|
|
|
|
|
| 1053 |
|
| 1054 |
+
*Throws:*
|
| 1055 |
+
|
| 1056 |
+
- Any exceptions thrown by the copy or move constructor of `f`.
|
| 1057 |
+
- For the first version, `bad_alloc` if memory for the internal data
|
| 1058 |
+
structures could not be allocated.
|
| 1059 |
+
- For the second version, any exceptions thrown by
|
| 1060 |
+
`allocator_traits<Allocator>::template`
|
| 1061 |
+
`rebind_traits<`*`unspecified`*`>::allocate`.
|
| 1062 |
|
| 1063 |
``` cpp
|
| 1064 |
packaged_task(packaged_task&& rhs) noexcept;
|
| 1065 |
```
|
| 1066 |
|
| 1067 |
+
*Effects:* Constructs a new `packaged_task` object and transfers
|
| 1068 |
ownership of `rhs`’s shared state to `*this`, leaving `rhs` with no
|
| 1069 |
shared state. Moves the stored task from `rhs` to `*this`.
|
| 1070 |
|
| 1071 |
+
*Postconditions:* `rhs` has no shared state.
|
| 1072 |
|
| 1073 |
``` cpp
|
| 1074 |
packaged_task& operator=(packaged_task&& rhs) noexcept;
|
| 1075 |
```
|
| 1076 |
|
| 1077 |
*Effects:*
|
| 1078 |
|
| 1079 |
+
- Releases any shared state ([[futures.state]]);
|
| 1080 |
+
- calls `packaged_task(std::move(rhs)).swap(*this)`.
|
| 1081 |
|
| 1082 |
``` cpp
|
| 1083 |
~packaged_task();
|
| 1084 |
```
|
| 1085 |
|
| 1086 |
+
*Effects:* Abandons any shared state ([[futures.state]]).
|
| 1087 |
|
| 1088 |
``` cpp
|
| 1089 |
void swap(packaged_task& other) noexcept;
|
| 1090 |
```
|
| 1091 |
|
| 1092 |
+
*Effects:* Exchanges the shared states and stored tasks of `*this` and
|
| 1093 |
`other`.
|
| 1094 |
|
| 1095 |
+
*Postconditions:* `*this` has the same shared state and stored task (if
|
| 1096 |
+
any) as `other` prior to the call to `swap`. `other` has the same shared
|
| 1097 |
+
state and stored task (if any) as `*this` prior to the call to `swap`.
|
| 1098 |
|
| 1099 |
``` cpp
|
| 1100 |
bool valid() const noexcept;
|
| 1101 |
```
|
| 1102 |
|
|
|
|
| 1119 |
|
| 1120 |
``` cpp
|
| 1121 |
void operator()(ArgTypes... args);
|
| 1122 |
```
|
| 1123 |
|
| 1124 |
+
*Effects:* As if by *INVOKE*\<R\>(f, t1, t2, ..., tN), where `f` is the
|
| 1125 |
+
stored task of `*this` and `t1, t2, ..., tN` are the values in
|
| 1126 |
+
`args...`. If the task returns normally, the return value is stored as
|
| 1127 |
+
the asynchronous result in the shared state of `*this`, otherwise the
|
| 1128 |
exception thrown by the task is stored. The shared state of `*this` is
|
| 1129 |
made ready, and any threads blocked in a function waiting for the shared
|
| 1130 |
state of `*this` to become ready are unblocked.
|
| 1131 |
|
| 1132 |
*Throws:* a `future_error` exception object if there is no shared state
|
|
|
|
| 1140 |
|
| 1141 |
``` cpp
|
| 1142 |
void make_ready_at_thread_exit(ArgTypes... args);
|
| 1143 |
```
|
| 1144 |
|
| 1145 |
+
*Effects:* As if by *INVOKE*\<R\>(f, t1, t2, ..., tN), where `f` is the
|
| 1146 |
+
stored task and `t1, t2, ..., tN` are the values in `args...`. If the
|
| 1147 |
+
task returns normally, the return value is stored as the asynchronous
|
| 1148 |
+
result in the shared state of `*this`, otherwise the exception thrown by
|
| 1149 |
+
the task is stored. In either case, this shall be done without making
|
| 1150 |
+
that state ready ([[futures.state]]) immediately. Schedules the shared
|
| 1151 |
+
state to be made ready when the current thread exits, after all objects
|
| 1152 |
+
of thread storage duration associated with the current thread have been
|
| 1153 |
destroyed.
|
| 1154 |
|
| 1155 |
*Throws:* `future_error` if an error condition occurs.
|
| 1156 |
|
| 1157 |
*Error conditions:*
|
|
|
|
| 1162 |
|
| 1163 |
``` cpp
|
| 1164 |
void reset();
|
| 1165 |
```
|
| 1166 |
|
| 1167 |
+
*Effects:* As if `*this = packaged_task(std::move(f))`, where `f` is the
|
| 1168 |
+
task stored in `*this`.
|
| 1169 |
+
|
| 1170 |
+
[*Note 1*: This constructs a new shared state for `*this`. The old
|
| 1171 |
+
state is abandoned ([[futures.state]]). — *end note*]
|
| 1172 |
|
| 1173 |
*Throws:*
|
| 1174 |
|
| 1175 |
- `bad_alloc` if memory for the new shared state could not be allocated.
|
| 1176 |
- any exception thrown by the move constructor of the task stored in the
|
|
|
|
| 1183 |
``` cpp
|
| 1184 |
template <class R, class... ArgTypes>
|
| 1185 |
void swap(packaged_task<R(ArgTypes...)>& x, packaged_task<R(ArgTypes...)>& y) noexcept;
|
| 1186 |
```
|
| 1187 |
|
| 1188 |
+
*Effects:* As if by `x.swap(y)`.
|
| 1189 |
|
| 1190 |
``` cpp
|
| 1191 |
template <class R, class Alloc>
|
| 1192 |
struct uses_allocator<packaged_task<R>, Alloc>
|
| 1193 |
: true_type { };
|
|
|
|
| 1201 |
[atomics]: atomics.md#atomics
|
| 1202 |
[basic.life]: basic.md#basic.life
|
| 1203 |
[basic.stc.thread]: basic.md#basic.stc.thread
|
| 1204 |
[bitmask.types]: library.md#bitmask.types
|
| 1205 |
[class]: class.md#class
|
| 1206 |
+
[condition_variable.syn]: #condition_variable.syn
|
| 1207 |
[except.terminate]: except.md#except.terminate
|
| 1208 |
[func.require]: utilities.md#func.require
|
| 1209 |
+
[future.syn]: #future.syn
|
| 1210 |
[futures]: #futures
|
| 1211 |
[futures.async]: #futures.async
|
| 1212 |
[futures.errors]: #futures.errors
|
| 1213 |
[futures.future_error]: #futures.future_error
|
| 1214 |
[futures.overview]: #futures.overview
|
|
|
|
| 1218 |
[futures.task]: #futures.task
|
| 1219 |
[futures.task.members]: #futures.task.members
|
| 1220 |
[futures.task.nonmembers]: #futures.task.nonmembers
|
| 1221 |
[futures.unique_future]: #futures.unique_future
|
| 1222 |
[intro.multithread]: intro.md#intro.multithread
|
| 1223 |
+
[mutex.syn]: #mutex.syn
|
| 1224 |
[res.on.data.races]: library.md#res.on.data.races
|
| 1225 |
[res.on.exception.handling]: library.md#res.on.exception.handling
|
| 1226 |
+
[shared_mutex.syn]: #shared_mutex.syn
|
| 1227 |
[syserr]: diagnostics.md#syserr
|
| 1228 |
[syserr.syserr]: diagnostics.md#syserr.syserr
|
| 1229 |
[tab:thread.lib.summary]: #tab:thread.lib.summary
|
| 1230 |
[thread]: #thread
|
| 1231 |
[thread.condition]: #thread.condition
|
| 1232 |
[thread.condition.condvar]: #thread.condition.condvar
|
| 1233 |
[thread.condition.condvarany]: #thread.condition.condvarany
|
| 1234 |
+
[thread.condition.nonmember]: #thread.condition.nonmember
|
| 1235 |
[thread.decaycopy]: #thread.decaycopy
|
| 1236 |
[thread.general]: #thread.general
|
| 1237 |
[thread.lock]: #thread.lock
|
| 1238 |
[thread.lock.algorithm]: #thread.lock.algorithm
|
| 1239 |
[thread.lock.guard]: #thread.lock.guard
|
| 1240 |
+
[thread.lock.scoped]: #thread.lock.scoped
|
| 1241 |
[thread.lock.shared]: #thread.lock.shared
|
| 1242 |
[thread.lock.shared.cons]: #thread.lock.shared.cons
|
| 1243 |
[thread.lock.shared.locking]: #thread.lock.shared.locking
|
| 1244 |
[thread.lock.shared.mod]: #thread.lock.shared.mod
|
| 1245 |
[thread.lock.shared.obs]: #thread.lock.shared.obs
|
|
|
|
| 1265 |
[thread.req.lockable.req]: #thread.req.lockable.req
|
| 1266 |
[thread.req.lockable.timed]: #thread.req.lockable.timed
|
| 1267 |
[thread.req.native]: #thread.req.native
|
| 1268 |
[thread.req.paramname]: #thread.req.paramname
|
| 1269 |
[thread.req.timing]: #thread.req.timing
|
| 1270 |
+
[thread.sharedmutex.class]: #thread.sharedmutex.class
|
| 1271 |
+
[thread.sharedmutex.requirements]: #thread.sharedmutex.requirements
|
| 1272 |
[thread.sharedtimedmutex.class]: #thread.sharedtimedmutex.class
|
| 1273 |
[thread.sharedtimedmutex.requirements]: #thread.sharedtimedmutex.requirements
|
| 1274 |
+
[thread.syn]: #thread.syn
|
| 1275 |
[thread.thread.algorithm]: #thread.thread.algorithm
|
| 1276 |
[thread.thread.assign]: #thread.thread.assign
|
| 1277 |
[thread.thread.class]: #thread.thread.class
|
| 1278 |
[thread.thread.constr]: #thread.thread.constr
|
| 1279 |
[thread.thread.destr]: #thread.thread.destr
|