- tmp/tmplhakgs7a/{from.md → to.md} +246 -217
tmp/tmplhakgs7a/{from.md → to.md}
RENAMED
|
@@ -64,23 +64,20 @@ namespace std {
|
|
| 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
|
| 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
|
|
@@ -93,16 +90,16 @@ The enum values of `future_errc` are distinct and not zero.
|
|
| 93 |
|
| 94 |
``` cpp
|
| 95 |
const error_category& future_category() noexcept;
|
| 96 |
```
|
| 97 |
|
| 98 |
-
*Returns:*
|
| 99 |
`error_category`.
|
| 100 |
|
| 101 |
The object’s `default_error_condition` and equivalent virtual functions
|
| 102 |
shall behave as specified for the class `error_category`. The object’s
|
| 103 |
-
`name` virtual function
|
| 104 |
|
| 105 |
``` cpp
|
| 106 |
error_code make_error_code(future_errc e) noexcept;
|
| 107 |
```
|
| 108 |
|
|
@@ -112,32 +109,32 @@ error_code make_error_code(future_errc e) noexcept;
|
|
| 112 |
error_condition make_error_condition(future_errc e) noexcept;
|
| 113 |
```
|
| 114 |
|
| 115 |
*Returns:* `error_condition(static_cast<int>(e), future_category())`.
|
| 116 |
|
| 117 |
-
### Class `future_error` <a id="futures.
|
| 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:*
|
| 138 |
-
`ec_` with `make_error_code(e)`.
|
| 139 |
|
| 140 |
``` cpp
|
| 141 |
const error_code& code() const noexcept;
|
| 142 |
```
|
| 143 |
|
|
@@ -149,12 +146,12 @@ const char* what() const noexcept;
|
|
| 149 |
|
| 150 |
*Returns:* An NTBS incorporating `code().message()`.
|
| 151 |
|
| 152 |
### Shared state <a id="futures.state">[[futures.state]]</a>
|
| 153 |
|
| 154 |
-
Many of the classes introduced in
|
| 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*]
|
|
@@ -165,11 +162,11 @@ compute that result, as used by `async` when `policy` is
|
|
| 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
|
| 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.
|
|
@@ -212,30 +209,29 @@ A shared state is *ready* only if it holds a value or an exception ready
|
|
| 212 |
for retrieval. Waiting for a shared state to become ready may invoke
|
| 213 |
code to compute the result on the waiting thread if so specified in the
|
| 214 |
description of the class or function that creates the state object.
|
| 215 |
|
| 216 |
Calls to functions that successfully set the stored result of a shared
|
| 217 |
-
state synchronize with
|
| 218 |
successfully detecting the ready state resulting from that setting. The
|
| 219 |
storage of the result (whether normal or exceptional) into the shared
|
| 220 |
-
state synchronizes with
|
| 221 |
-
|
| 222 |
|
| 223 |
Some functions (e.g., `promise::set_value_at_thread_exit`) delay making
|
| 224 |
the shared state ready until the calling thread exits. The destruction
|
| 225 |
-
of each of that thread’s objects with thread storage duration
|
| 226 |
-
[[basic.stc.thread]]
|
| 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
|
| 235 |
-
accesses through references returned by `shared_future::get()`
|
| 236 |
-
[[futures.
|
| 237 |
provide additional synchronization. — *end note*]
|
| 238 |
|
| 239 |
### Class template `promise` <a id="futures.promise">[[futures.promise]]</a>
|
| 240 |
|
| 241 |
``` cpp
|
|
@@ -245,16 +241,16 @@ namespace std {
|
|
| 245 |
public:
|
| 246 |
promise();
|
| 247 |
template<class Allocator>
|
| 248 |
promise(allocator_arg_t, const Allocator& a);
|
| 249 |
promise(promise&& rhs) noexcept;
|
| 250 |
-
promise(const promise&
|
| 251 |
~promise();
|
| 252 |
|
| 253 |
// assignment
|
| 254 |
promise& operator=(promise&& rhs) noexcept;
|
| 255 |
-
promise& operator=(const promise&
|
| 256 |
void swap(promise& other) noexcept;
|
| 257 |
|
| 258 |
// retrieving the result
|
| 259 |
future<R> get_future();
|
| 260 |
|
|
@@ -264,18 +260,20 @@ namespace std {
|
|
| 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;
|
|
|
|
| 271 |
template<class R, class Alloc>
|
| 272 |
struct uses_allocator<promise<R>, Alloc>;
|
| 273 |
}
|
| 274 |
```
|
| 275 |
|
| 276 |
-
The implementation
|
| 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
|
|
@@ -287,62 +285,69 @@ the promise object.
|
|
| 287 |
template<class R, class Alloc>
|
| 288 |
struct uses_allocator<promise<R>, Alloc>
|
| 289 |
: true_type { };
|
| 290 |
```
|
| 291 |
|
| 292 |
-
*
|
|
|
|
| 293 |
|
| 294 |
``` cpp
|
| 295 |
promise();
|
| 296 |
template<class Allocator>
|
| 297 |
promise(allocator_arg_t, const Allocator& a);
|
| 298 |
```
|
| 299 |
|
| 300 |
-
*Effects:*
|
| 301 |
-
|
| 302 |
-
state.
|
| 303 |
|
| 304 |
``` cpp
|
| 305 |
promise(promise&& rhs) noexcept;
|
| 306 |
```
|
| 307 |
|
| 308 |
-
*Effects:*
|
| 309 |
-
the
|
| 310 |
|
| 311 |
-
*
|
| 312 |
|
| 313 |
``` cpp
|
| 314 |
~promise();
|
| 315 |
```
|
| 316 |
|
| 317 |
-
*Effects:* Abandons any shared state
|
| 318 |
|
| 319 |
``` cpp
|
| 320 |
promise& operator=(promise&& rhs) noexcept;
|
| 321 |
```
|
| 322 |
|
| 323 |
-
*Effects:* Abandons any shared state
|
| 324 |
`promise(std::move(rhs)).swap(*this)`.
|
| 325 |
|
| 326 |
*Returns:* `*this`.
|
| 327 |
|
| 328 |
``` cpp
|
| 329 |
void swap(promise& other) noexcept;
|
| 330 |
```
|
| 331 |
|
| 332 |
*Effects:* Exchanges the shared state of `*this` and `other`.
|
| 333 |
|
| 334 |
-
*
|
| 335 |
-
|
| 336 |
`*this` had prior to the call to `swap`.
|
| 337 |
|
| 338 |
``` cpp
|
| 339 |
future<R> get_future();
|
| 340 |
```
|
| 341 |
|
| 342 |
*Returns:* A `future<R>` object with the same shared state as `*this`.
|
| 343 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 344 |
*Throws:* `future_error` if `*this` has no shared state or if
|
| 345 |
`get_future` has already been called on a `promise` with the same shared
|
| 346 |
state as `*this`.
|
| 347 |
|
| 348 |
*Error conditions:*
|
|
@@ -357,11 +362,11 @@ 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
|
| 363 |
|
| 364 |
*Throws:*
|
| 365 |
|
| 366 |
- `future_error` if its shared state already has a stored value or
|
| 367 |
exception, or
|
|
@@ -378,14 +383,14 @@ that state ready ([[futures.state]]).
|
|
| 378 |
|
| 379 |
``` cpp
|
| 380 |
void set_exception(exception_ptr p);
|
| 381 |
```
|
| 382 |
|
| 383 |
-
*
|
| 384 |
|
| 385 |
*Effects:* Atomically stores the exception pointer `p` in the shared
|
| 386 |
-
state and makes that state ready
|
| 387 |
|
| 388 |
*Throws:* `future_error` if its shared state already has a stored value
|
| 389 |
or exception.
|
| 390 |
|
| 391 |
*Error conditions:*
|
|
@@ -423,11 +428,11 @@ associated with the current thread have been destroyed.
|
|
| 423 |
|
| 424 |
``` cpp
|
| 425 |
void set_exception_at_thread_exit(exception_ptr p);
|
| 426 |
```
|
| 427 |
|
| 428 |
-
*
|
| 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.
|
|
@@ -445,21 +450,20 @@ 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.
|
| 451 |
|
| 452 |
The class template `future` defines a type for asynchronous return
|
| 453 |
objects which do not share their shared state with other asynchronous
|
| 454 |
return objects. A default-constructed `future` object has no shared
|
| 455 |
state. A `future` object with shared state can be created by functions
|
| 456 |
-
on asynchronous providers
|
| 457 |
-
|
| 458 |
-
|
| 459 |
-
|
| 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
|
|
@@ -467,24 +471,24 @@ 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
|
| 473 |
-
|
| 474 |
`future_errc::no_state`. — *end note*]
|
| 475 |
|
| 476 |
``` cpp
|
| 477 |
namespace std {
|
| 478 |
template<class R>
|
| 479 |
class future {
|
| 480 |
public:
|
| 481 |
future() noexcept;
|
| 482 |
future(future&&) noexcept;
|
| 483 |
-
future(const future&
|
| 484 |
~future();
|
| 485 |
-
future& operator=(const future&
|
| 486 |
future& operator=(future&&) noexcept;
|
| 487 |
shared_future<R> share() noexcept;
|
| 488 |
|
| 489 |
// retrieving the value
|
| 490 |
see below get();
|
|
@@ -499,32 +503,31 @@ namespace std {
|
|
| 499 |
future_status wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
|
| 500 |
};
|
| 501 |
}
|
| 502 |
```
|
| 503 |
|
| 504 |
-
The implementation
|
| 505 |
specializations, `future<R&>` and `future<{}void>`. These differ only in
|
| 506 |
the return type and return value of the member function `get`, as set
|
| 507 |
out in its description, below.
|
| 508 |
|
| 509 |
``` cpp
|
| 510 |
future() noexcept;
|
| 511 |
```
|
| 512 |
|
| 513 |
-
*Effects:*
|
| 514 |
-
a shared state.
|
| 515 |
|
| 516 |
-
*
|
| 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 |
-
*
|
| 526 |
|
| 527 |
- `valid()` returns the same value as `rhs.valid()` prior to the
|
| 528 |
constructor invocation.
|
| 529 |
- `rhs.valid() == false`.
|
| 530 |
|
|
@@ -532,23 +535,23 @@ state that was originally referred to by `rhs` (if any).
|
|
| 532 |
~future();
|
| 533 |
```
|
| 534 |
|
| 535 |
*Effects:*
|
| 536 |
|
| 537 |
-
- Releases any shared state
|
| 538 |
- destroys `*this`.
|
| 539 |
|
| 540 |
``` cpp
|
| 541 |
future& operator=(future&& rhs) noexcept;
|
| 542 |
```
|
| 543 |
|
| 544 |
*Effects:*
|
| 545 |
|
| 546 |
-
- Releases any shared state
|
| 547 |
- move assigns the contents of `rhs` to `*this`.
|
| 548 |
|
| 549 |
-
*
|
| 550 |
|
| 551 |
- `valid()` returns the same value as `rhs.valid()` prior to the
|
| 552 |
assignment.
|
| 553 |
- `rhs.valid() == false`.
|
| 554 |
|
|
@@ -556,11 +559,11 @@ future& operator=(future&& rhs) noexcept;
|
|
| 556 |
shared_future<R> share() noexcept;
|
| 557 |
```
|
| 558 |
|
| 559 |
*Returns:* `shared_future<R>(std::move(*this))`.
|
| 560 |
|
| 561 |
-
*
|
| 562 |
|
| 563 |
``` cpp
|
| 564 |
R future::get();
|
| 565 |
R& future<R&>::get();
|
| 566 |
void future<void>::get();
|
|
@@ -572,24 +575,24 @@ 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
|
| 578 |
|
| 579 |
*Returns:*
|
| 580 |
|
| 581 |
- `future::get()` returns the value `v` stored in the object’s shared
|
| 582 |
state as `std::move(v)`.
|
| 583 |
- `future<R&>::get()` returns the reference stored as value in the
|
| 584 |
object’s shared state.
|
| 585 |
- `future<void>::get()` returns nothing.
|
| 586 |
|
| 587 |
-
*Throws:*
|
| 588 |
state.
|
| 589 |
|
| 590 |
-
*
|
| 591 |
|
| 592 |
``` cpp
|
| 593 |
bool valid() const noexcept;
|
| 594 |
```
|
| 595 |
|
|
@@ -605,57 +608,57 @@ void wait() const;
|
|
| 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
|
| 611 |
-
|
| 612 |
-
|
| 613 |
|
| 614 |
*Returns:*
|
| 615 |
|
| 616 |
- `future_status::deferred` if the shared state contains a deferred
|
| 617 |
function.
|
| 618 |
- `future_status::ready` if the shared state is ready.
|
| 619 |
- `future_status::timeout` if the function is returning because the
|
| 620 |
-
relative timeout
|
| 621 |
expired.
|
| 622 |
|
| 623 |
-
*Throws:* timeout-related exceptions
|
| 624 |
|
| 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
|
| 632 |
-
|
| 633 |
-
|
| 634 |
|
| 635 |
*Returns:*
|
| 636 |
|
| 637 |
- `future_status::deferred` if the shared state contains a deferred
|
| 638 |
function.
|
| 639 |
- `future_status::ready` if the shared state is ready.
|
| 640 |
- `future_status::timeout` if the function is returning because the
|
| 641 |
-
absolute timeout
|
| 642 |
expired.
|
| 643 |
|
| 644 |
-
*Throws:* timeout-related exceptions
|
| 645 |
|
| 646 |
-
### Class template `shared_future` <a id="futures.
|
| 647 |
|
| 648 |
The class template `shared_future` defines a type for asynchronous
|
| 649 |
return objects which may share their shared state with other
|
| 650 |
asynchronous return objects. A default-constructed `shared_future`
|
| 651 |
object has no shared state. A `shared_future` object with shared state
|
| 652 |
can be created by conversion from a `future` object and shares its
|
| 653 |
-
shared state with the original asynchronous provider
|
| 654 |
-
|
| 655 |
-
|
| 656 |
-
|
| 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
|
|
@@ -663,12 +666,12 @@ 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
|
| 669 |
-
|
| 670 |
`future_errc::no_state`. — *end note*]
|
| 671 |
|
| 672 |
``` cpp
|
| 673 |
namespace std {
|
| 674 |
template<class R>
|
|
@@ -695,42 +698,40 @@ namespace std {
|
|
| 695 |
future_status wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
|
| 696 |
};
|
| 697 |
}
|
| 698 |
```
|
| 699 |
|
| 700 |
-
The implementation
|
| 701 |
specializations, `shared_future<R&>` and `shared_future<void>`. These
|
| 702 |
differ only in the return type and return value of the member function
|
| 703 |
`get`, as set out in its description, below.
|
| 704 |
|
| 705 |
``` cpp
|
| 706 |
shared_future() noexcept;
|
| 707 |
```
|
| 708 |
|
| 709 |
-
*Effects:*
|
| 710 |
-
refer to a shared state.
|
| 711 |
|
| 712 |
-
*
|
| 713 |
|
| 714 |
``` cpp
|
| 715 |
shared_future(const shared_future& rhs) noexcept;
|
| 716 |
```
|
| 717 |
|
| 718 |
-
*Effects:*
|
| 719 |
-
shared state as `rhs` (if any).
|
| 720 |
|
| 721 |
-
*
|
| 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 |
-
*
|
| 732 |
|
| 733 |
- `valid()` returns the same value as `rhs.valid()` returned prior to
|
| 734 |
the constructor invocation.
|
| 735 |
- `rhs.valid() == false`.
|
| 736 |
|
|
@@ -738,23 +739,23 @@ shared state that was originally referred to by `rhs` (if any).
|
|
| 738 |
~shared_future();
|
| 739 |
```
|
| 740 |
|
| 741 |
*Effects:*
|
| 742 |
|
| 743 |
-
- Releases any shared 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
|
| 753 |
- move assigns the contents of `rhs` to `*this`.
|
| 754 |
|
| 755 |
-
*
|
| 756 |
|
| 757 |
- `valid()` returns the same value as `rhs.valid()` returned prior to
|
| 758 |
the assignment.
|
| 759 |
- `rhs.valid() == false`.
|
| 760 |
|
|
@@ -762,16 +763,16 @@ shared_future& operator=(shared_future&& rhs) noexcept;
|
|
| 762 |
shared_future& operator=(const shared_future& rhs) noexcept;
|
| 763 |
```
|
| 764 |
|
| 765 |
*Effects:*
|
| 766 |
|
| 767 |
-
- Releases any shared 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 |
-
*
|
| 773 |
|
| 774 |
``` cpp
|
| 775 |
const R& shared_future::get() const;
|
| 776 |
R& shared_future<R&>::get() const;
|
| 777 |
void shared_future<void>::get() const;
|
|
@@ -781,12 +782,11 @@ void shared_future<void>::get() const;
|
|
| 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:*
|
|
@@ -799,11 +799,11 @@ value stored in the shared state.
|
|
| 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:*
|
| 805 |
state.
|
| 806 |
|
| 807 |
``` cpp
|
| 808 |
bool valid() const noexcept;
|
| 809 |
```
|
|
@@ -820,144 +820,145 @@ void wait() const;
|
|
| 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
|
| 826 |
-
|
| 827 |
-
|
| 828 |
|
| 829 |
*Returns:*
|
| 830 |
|
| 831 |
- `future_status::deferred` if the shared state contains a deferred
|
| 832 |
function.
|
| 833 |
- `future_status::ready` if the shared state is ready.
|
| 834 |
- `future_status::timeout` if the function is returning because the
|
| 835 |
-
relative timeout
|
| 836 |
expired.
|
| 837 |
|
| 838 |
-
*Throws:* timeout-related exceptions
|
| 839 |
|
| 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
|
| 847 |
-
|
| 848 |
-
|
| 849 |
|
| 850 |
*Returns:*
|
| 851 |
|
| 852 |
- `future_status::deferred` if the shared state contains a deferred
|
| 853 |
function.
|
| 854 |
- `future_status::ready` if the shared state is ready.
|
| 855 |
- `future_status::timeout` if the function is returning because the
|
| 856 |
-
absolute timeout
|
| 857 |
expired.
|
| 858 |
|
| 859 |
-
*Throws:* timeout-related exceptions
|
| 860 |
|
| 861 |
### Function template `async` <a id="futures.async">[[futures.async]]</a>
|
| 862 |
|
| 863 |
The function template `async` provides a mechanism to launch a function
|
| 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 |
-
*
|
| 877 |
-
`MoveConstructible` requirements, and
|
| 878 |
|
| 879 |
-
`
|
| 880 |
-
|
| 881 |
-
|
| 882 |
-
``
|
|
|
|
| 883 |
|
| 884 |
-
|
|
|
|
| 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 |
-
|
| 896 |
-
*
|
| 897 |
[[thread.thread.constr]]) as if in a new thread of execution
|
| 898 |
-
represented by a `thread` object with the calls to *
|
| 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
|
| 902 |
-
*
|
| 903 |
-
exceptional result in the shared state. The `thread`
|
| 904 |
-
in the shared state and affects the behavior of any
|
| 905 |
-
return objects that reference that state.
|
| 906 |
- If `launch::deferred` is set in `policy`, stores
|
| 907 |
-
*
|
| 908 |
-
*
|
| 909 |
copies of `f` and `args` constitute a *deferred function*. Invocation
|
| 910 |
-
of the deferred function evaluates
|
| 911 |
-
std::move(xyz)) where `g` is the stored value
|
| 912 |
-
*
|
| 913 |
-
*
|
| 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
|
| 919 |
-
referring to this shared state
|
| 920 |
-
|
| 921 |
-
|
| 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
|
| 929 |
-
|
| 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
|
| 937 |
-
destructor
|
| 938 |
ready. — *end note*]
|
| 939 |
|
| 940 |
*Synchronization:* Regardless of the provided `policy` argument,
|
| 941 |
|
| 942 |
-
- the invocation of `async` synchronizes with
|
| 943 |
-
|
| 944 |
-
|
| 945 |
thread. — *end note*] ; and
|
| 946 |
- the completion of the function `f` is sequenced
|
| 947 |
-
before
|
| 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
|
| 955 |
the associated thread has completed, as if joined, or else time
|
| 956 |
-
out
|
| 957 |
- the associated thread completion synchronizes
|
| 958 |
-
with
|
| 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
|
|
@@ -1017,86 +1018,79 @@ namespace std {
|
|
| 1017 |
void operator()(ArgTypes... );
|
| 1018 |
void make_ready_at_thread_exit(ArgTypes...);
|
| 1019 |
|
| 1020 |
void reset();
|
| 1021 |
};
|
|
|
|
| 1022 |
template<class R, class... ArgTypes>
|
| 1023 |
void swap(packaged_task<R(ArgTypes...)>& x, packaged_task<R(ArgTypes...)>& y) noexcept;
|
| 1024 |
-
template <class R, class Alloc>
|
| 1025 |
-
struct uses_allocator<packaged_task<R>, Alloc>;
|
| 1026 |
}
|
| 1027 |
```
|
| 1028 |
|
| 1029 |
-
####
|
| 1030 |
|
| 1031 |
``` cpp
|
| 1032 |
packaged_task() noexcept;
|
| 1033 |
```
|
| 1034 |
|
| 1035 |
-
*Effects:*
|
| 1036 |
-
no stored task.
|
| 1037 |
|
| 1038 |
``` cpp
|
| 1039 |
template<class F>
|
| 1040 |
packaged_task(F&& f);
|
| 1041 |
```
|
| 1042 |
|
| 1043 |
-
*
|
| 1044 |
-
|
| 1045 |
-
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 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:*
|
| 1068 |
-
|
| 1069 |
-
|
| 1070 |
|
| 1071 |
-
*
|
| 1072 |
|
| 1073 |
``` cpp
|
| 1074 |
packaged_task& operator=(packaged_task&& rhs) noexcept;
|
| 1075 |
```
|
| 1076 |
|
| 1077 |
*Effects:*
|
| 1078 |
|
| 1079 |
-
- Releases any shared state
|
| 1080 |
- calls `packaged_task(std::move(rhs)).swap(*this)`.
|
| 1081 |
|
| 1082 |
``` cpp
|
| 1083 |
~packaged_task();
|
| 1084 |
```
|
| 1085 |
|
| 1086 |
-
*Effects:* Abandons any shared 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 |
-
*
|
| 1096 |
-
|
| 1097 |
-
|
| 1098 |
|
| 1099 |
``` cpp
|
| 1100 |
bool valid() const noexcept;
|
| 1101 |
```
|
| 1102 |
|
|
@@ -1107,11 +1101,18 @@ future<R> get_future();
|
|
| 1107 |
```
|
| 1108 |
|
| 1109 |
*Returns:* A `future` object that shares the same shared state as
|
| 1110 |
`*this`.
|
| 1111 |
|
| 1112 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1113 |
|
| 1114 |
*Error conditions:*
|
| 1115 |
|
| 1116 |
- `future_already_retrieved` if `get_future` has already been called on
|
| 1117 |
a `packaged_task` object with the same shared state as `*this`.
|
|
@@ -1119,19 +1120,19 @@ future<R> get_future();
|
|
| 1119 |
|
| 1120 |
``` cpp
|
| 1121 |
void operator()(ArgTypes... args);
|
| 1122 |
```
|
| 1123 |
|
| 1124 |
-
*Effects:* As if by *INVOKE*\<R\>(f,
|
| 1125 |
-
stored task of `*this` and `
|
| 1126 |
-
`args...`. If the task returns normally, the return value
|
| 1127 |
-
the asynchronous result in the shared state of `*this`,
|
| 1128 |
-
exception thrown by the task is stored. The shared state
|
| 1129 |
-
made ready, and any threads blocked in a function waiting
|
| 1130 |
-
state of `*this` to become ready are unblocked.
|
| 1131 |
|
| 1132 |
-
*Throws:*
|
| 1133 |
or the stored task has already been invoked.
|
| 1134 |
|
| 1135 |
*Error conditions:*
|
| 1136 |
|
| 1137 |
- `promise_already_satisfied` if the stored task has already been
|
|
@@ -1140,19 +1141,19 @@ or the stored task has already been invoked.
|
|
| 1140 |
|
| 1141 |
``` cpp
|
| 1142 |
void make_ready_at_thread_exit(ArgTypes... args);
|
| 1143 |
```
|
| 1144 |
|
| 1145 |
-
*Effects:* As if by *INVOKE*\<R\>(f,
|
| 1146 |
-
stored task and `
|
| 1147 |
-
task returns normally, the return value is stored
|
| 1148 |
-
result in the shared state of `*this`, otherwise the
|
| 1149 |
-
the task is stored. In either case, this
|
| 1150 |
-
that state ready
|
| 1151 |
-
state to be made ready when the current thread exits, after
|
| 1152 |
-
of thread storage duration associated with the current
|
| 1153 |
-
destroyed.
|
| 1154 |
|
| 1155 |
*Throws:* `future_error` if an error condition occurs.
|
| 1156 |
|
| 1157 |
*Error conditions:*
|
| 1158 |
|
|
@@ -1165,77 +1166,99 @@ 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
|
| 1171 |
-
state is abandoned
|
| 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
|
| 1177 |
shared state.
|
| 1178 |
- `future_error` with an error condition of `no_state` if `*this` has no
|
| 1179 |
shared state.
|
| 1180 |
|
| 1181 |
-
####
|
| 1182 |
|
| 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 { };
|
| 1194 |
-
```
|
| 1195 |
-
|
| 1196 |
-
*Requires:* `Alloc` shall be an Allocator ([[allocator.requirements]]).
|
| 1197 |
-
|
| 1198 |
<!-- Link reference definitions -->
|
| 1199 |
[alg.sorting]: algorithms.md#alg.sorting
|
| 1200 |
-
[allocator.requirements]: library.md#allocator.requirements
|
| 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 |
-
[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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.
|
| 1214 |
[futures.overview]: #futures.overview
|
| 1215 |
[futures.promise]: #futures.promise
|
| 1216 |
-
[futures.
|
| 1217 |
[futures.state]: #futures.state
|
| 1218 |
[futures.task]: #futures.task
|
| 1219 |
[futures.task.members]: #futures.task.members
|
| 1220 |
[futures.task.nonmembers]: #futures.task.nonmembers
|
| 1221 |
-
[futures.
|
| 1222 |
-
[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 |
-
[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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.
|
|
|
|
|
|
|
| 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
|
|
@@ -1265,14 +1288,20 @@ template <class R, class Alloc>
|
|
| 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
|
|
@@ -1283,15 +1312,15 @@ template <class R, class Alloc>
|
|
| 1283 |
[thread.thread.this]: #thread.thread.this
|
| 1284 |
[thread.threads]: #thread.threads
|
| 1285 |
[thread.timedmutex.class]: #thread.timedmutex.class
|
| 1286 |
[thread.timedmutex.recursive]: #thread.timedmutex.recursive
|
| 1287 |
[thread.timedmutex.requirements]: #thread.timedmutex.requirements
|
| 1288 |
-
[time]:
|
| 1289 |
-
[time.clock]:
|
| 1290 |
-
[time.clock.req]:
|
| 1291 |
-
[time.duration]:
|
| 1292 |
-
[time.point]:
|
| 1293 |
[unord.hash]: utilities.md#unord.hash
|
| 1294 |
|
| 1295 |
[^1]: All implementations for which standard time units are meaningful
|
| 1296 |
must necessarily have a steady clock within their hardware
|
| 1297 |
implementation.
|
|
|
|
| 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 F, class... Args>
|
| 70 |
+
[[nodiscard]] future<invoke_result_t<decay_t<F>, decay_t<Args>...>>
|
| 71 |
async(F&& f, Args&&... args);
|
| 72 |
template<class F, class... Args>
|
| 73 |
+
[[nodiscard]] future<invoke_result_t<decay_t<F>, decay_t<Args>...>>
|
| 74 |
async(launch policy, F&& f, Args&&... args);
|
| 75 |
}
|
| 76 |
```
|
| 77 |
|
| 78 |
+
The `enum` type `launch` is a bitmask type [[bitmask.types]] with
|
| 79 |
elements `launch::async` and `launch::deferred`.
|
| 80 |
|
| 81 |
[*Note 1*: Implementations can provide bitmasks to specify restrictions
|
| 82 |
on task interaction by functions launched by `async()` applicable to a
|
| 83 |
corresponding subset of available launch policies. Implementations can
|
|
|
|
| 90 |
|
| 91 |
``` cpp
|
| 92 |
const error_category& future_category() noexcept;
|
| 93 |
```
|
| 94 |
|
| 95 |
+
*Returns:* A reference to an object of a type derived from class
|
| 96 |
`error_category`.
|
| 97 |
|
| 98 |
The object’s `default_error_condition` and equivalent virtual functions
|
| 99 |
shall behave as specified for the class `error_category`. The object’s
|
| 100 |
+
`name` virtual function returns a pointer to the string `"future"`.
|
| 101 |
|
| 102 |
``` cpp
|
| 103 |
error_code make_error_code(future_errc e) noexcept;
|
| 104 |
```
|
| 105 |
|
|
|
|
| 109 |
error_condition make_error_condition(future_errc e) noexcept;
|
| 110 |
```
|
| 111 |
|
| 112 |
*Returns:* `error_condition(static_cast<int>(e), future_category())`.
|
| 113 |
|
| 114 |
+
### Class `future_error` <a id="futures.future.error">[[futures.future.error]]</a>
|
| 115 |
|
| 116 |
``` cpp
|
| 117 |
namespace std {
|
| 118 |
class future_error : public logic_error {
|
| 119 |
public:
|
| 120 |
explicit future_error(future_errc e);
|
| 121 |
|
| 122 |
const error_code& code() const noexcept;
|
| 123 |
const char* what() const noexcept;
|
| 124 |
+
|
| 125 |
private:
|
| 126 |
error_code ec_; // exposition only
|
| 127 |
};
|
| 128 |
}
|
| 129 |
```
|
| 130 |
|
| 131 |
``` cpp
|
| 132 |
explicit future_error(future_errc e);
|
| 133 |
```
|
| 134 |
|
| 135 |
+
*Effects:* Initializes `ec_` with `make_error_code(e)`.
|
|
|
|
| 136 |
|
| 137 |
``` cpp
|
| 138 |
const error_code& code() const noexcept;
|
| 139 |
```
|
| 140 |
|
|
|
|
| 146 |
|
| 147 |
*Returns:* An NTBS incorporating `code().message()`.
|
| 148 |
|
| 149 |
### Shared state <a id="futures.state">[[futures.state]]</a>
|
| 150 |
|
| 151 |
+
Many of the classes introduced in subclause [[futures]] use some state
|
| 152 |
+
to communicate results. This *shared state* consists of some state
|
| 153 |
information and some (possibly not yet evaluated) *result*, which can be
|
| 154 |
a (possibly void) value or an exception.
|
| 155 |
|
| 156 |
[*Note 1*: Futures, promises, and tasks defined in this clause
|
| 157 |
reference such shared state. — *end note*]
|
|
|
|
| 162 |
|
| 163 |
An *asynchronous return object* is an object that reads results from a
|
| 164 |
shared state. A *waiting function* of an asynchronous return object is
|
| 165 |
one that potentially blocks to wait for the shared state to be made
|
| 166 |
ready. If a waiting function can return before the state is made ready
|
| 167 |
+
because of a timeout [[thread.req.lockable]], then it is a *timed
|
| 168 |
waiting function*, otherwise it is a *non-timed waiting function*.
|
| 169 |
|
| 170 |
An *asynchronous provider* is an object that provides a result to a
|
| 171 |
shared state. The result of a shared state is set by respective
|
| 172 |
functions on the asynchronous provider.
|
|
|
|
| 209 |
for retrieval. Waiting for a shared state to become ready may invoke
|
| 210 |
code to compute the result on the waiting thread if so specified in the
|
| 211 |
description of the class or function that creates the state object.
|
| 212 |
|
| 213 |
Calls to functions that successfully set the stored result of a shared
|
| 214 |
+
state synchronize with [[intro.multithread]] calls to functions
|
| 215 |
successfully detecting the ready state resulting from that setting. The
|
| 216 |
storage of the result (whether normal or exceptional) into the shared
|
| 217 |
+
state synchronizes with [[intro.multithread]] the successful return from
|
| 218 |
+
a call to a waiting function on the shared state.
|
| 219 |
|
| 220 |
Some functions (e.g., `promise::set_value_at_thread_exit`) delay making
|
| 221 |
the shared state ready until the calling thread exits. The destruction
|
| 222 |
+
of each of that thread’s objects with thread storage duration
|
| 223 |
+
[[basic.stc.thread]] is sequenced before making that shared state ready.
|
|
|
|
| 224 |
|
| 225 |
+
Access to the result of the same shared state may conflict
|
| 226 |
+
[[intro.multithread]].
|
| 227 |
|
| 228 |
[*Note 4*: This explicitly specifies that the result of the shared
|
| 229 |
state is visible in the objects that reference this state in the sense
|
| 230 |
+
of data race avoidance [[res.on.data.races]]. For example, concurrent
|
| 231 |
+
accesses through references returned by `shared_future::get()`
|
| 232 |
+
[[futures.shared.future]] must either use read-only operations or
|
| 233 |
provide additional synchronization. — *end note*]
|
| 234 |
|
| 235 |
### Class template `promise` <a id="futures.promise">[[futures.promise]]</a>
|
| 236 |
|
| 237 |
``` cpp
|
|
|
|
| 241 |
public:
|
| 242 |
promise();
|
| 243 |
template<class Allocator>
|
| 244 |
promise(allocator_arg_t, const Allocator& a);
|
| 245 |
promise(promise&& rhs) noexcept;
|
| 246 |
+
promise(const promise&) = delete;
|
| 247 |
~promise();
|
| 248 |
|
| 249 |
// assignment
|
| 250 |
promise& operator=(promise&& rhs) noexcept;
|
| 251 |
+
promise& operator=(const promise&) = delete;
|
| 252 |
void swap(promise& other) noexcept;
|
| 253 |
|
| 254 |
// retrieving the result
|
| 255 |
future<R> get_future();
|
| 256 |
|
|
|
|
| 260 |
|
| 261 |
// setting the result with deferred notification
|
| 262 |
void set_value_at_thread_exit(see below);
|
| 263 |
void set_exception_at_thread_exit(exception_ptr p);
|
| 264 |
};
|
| 265 |
+
|
| 266 |
template<class R>
|
| 267 |
void swap(promise<R>& x, promise<R>& y) noexcept;
|
| 268 |
+
|
| 269 |
template<class R, class Alloc>
|
| 270 |
struct uses_allocator<promise<R>, Alloc>;
|
| 271 |
}
|
| 272 |
```
|
| 273 |
|
| 274 |
+
The implementation provides the template `promise` and two
|
| 275 |
specializations, `promise<R&>` and `promise<{}void>`. These differ only
|
| 276 |
in the argument type of the member functions `set_value` and
|
| 277 |
`set_value_at_thread_exit`, as set out in their descriptions, below.
|
| 278 |
|
| 279 |
The `set_value`, `set_exception`, `set_value_at_thread_exit`, and
|
|
|
|
| 285 |
template<class R, class Alloc>
|
| 286 |
struct uses_allocator<promise<R>, Alloc>
|
| 287 |
: true_type { };
|
| 288 |
```
|
| 289 |
|
| 290 |
+
*Preconditions:* `Alloc` meets the *Cpp17Allocator* requirements
|
| 291 |
+
([[cpp17.allocator]]).
|
| 292 |
|
| 293 |
``` cpp
|
| 294 |
promise();
|
| 295 |
template<class Allocator>
|
| 296 |
promise(allocator_arg_t, const Allocator& a);
|
| 297 |
```
|
| 298 |
|
| 299 |
+
*Effects:* Creates a shared state. The second constructor uses the
|
| 300 |
+
allocator `a` to allocate memory for the shared state.
|
|
|
|
| 301 |
|
| 302 |
``` cpp
|
| 303 |
promise(promise&& rhs) noexcept;
|
| 304 |
```
|
| 305 |
|
| 306 |
+
*Effects:* Transfers ownership of the shared state of `rhs` (if any) to
|
| 307 |
+
the newly-constructed object.
|
| 308 |
|
| 309 |
+
*Ensures:* `rhs` has no shared state.
|
| 310 |
|
| 311 |
``` cpp
|
| 312 |
~promise();
|
| 313 |
```
|
| 314 |
|
| 315 |
+
*Effects:* Abandons any shared state [[futures.state]].
|
| 316 |
|
| 317 |
``` cpp
|
| 318 |
promise& operator=(promise&& rhs) noexcept;
|
| 319 |
```
|
| 320 |
|
| 321 |
+
*Effects:* Abandons any shared state [[futures.state]] and then as if
|
| 322 |
`promise(std::move(rhs)).swap(*this)`.
|
| 323 |
|
| 324 |
*Returns:* `*this`.
|
| 325 |
|
| 326 |
``` cpp
|
| 327 |
void swap(promise& other) noexcept;
|
| 328 |
```
|
| 329 |
|
| 330 |
*Effects:* Exchanges the shared state of `*this` and `other`.
|
| 331 |
|
| 332 |
+
*Ensures:* `*this` has the shared state (if any) that `other` had prior
|
| 333 |
+
to the call to `swap`. `other` has the shared state (if any) that
|
| 334 |
`*this` had prior to the call to `swap`.
|
| 335 |
|
| 336 |
``` cpp
|
| 337 |
future<R> get_future();
|
| 338 |
```
|
| 339 |
|
| 340 |
*Returns:* A `future<R>` object with the same shared state as `*this`.
|
| 341 |
|
| 342 |
+
*Synchronization:* Calls to this function do not introduce data
|
| 343 |
+
races [[intro.multithread]] with calls to `set_value`, `set_exception`,
|
| 344 |
+
`set_value_at_thread_exit`, or `set_exception_at_thread_exit`.
|
| 345 |
+
|
| 346 |
+
[*Note 1*: Such calls need not synchronize with each
|
| 347 |
+
other. — *end note*]
|
| 348 |
+
|
| 349 |
*Throws:* `future_error` if `*this` has no shared state or if
|
| 350 |
`get_future` has already been called on a `promise` with the same shared
|
| 351 |
state as `*this`.
|
| 352 |
|
| 353 |
*Error conditions:*
|
|
|
|
| 362 |
void promise<R&>::set_value(R& r);
|
| 363 |
void promise<void>::set_value();
|
| 364 |
```
|
| 365 |
|
| 366 |
*Effects:* Atomically stores the value `r` in the shared state and makes
|
| 367 |
+
that state ready [[futures.state]].
|
| 368 |
|
| 369 |
*Throws:*
|
| 370 |
|
| 371 |
- `future_error` if its shared state already has a stored value or
|
| 372 |
exception, or
|
|
|
|
| 383 |
|
| 384 |
``` cpp
|
| 385 |
void set_exception(exception_ptr p);
|
| 386 |
```
|
| 387 |
|
| 388 |
+
*Preconditions:* `p` is not null.
|
| 389 |
|
| 390 |
*Effects:* Atomically stores the exception pointer `p` in the shared
|
| 391 |
+
state and makes that state ready [[futures.state]].
|
| 392 |
|
| 393 |
*Throws:* `future_error` if its shared state already has a stored value
|
| 394 |
or exception.
|
| 395 |
|
| 396 |
*Error conditions:*
|
|
|
|
| 428 |
|
| 429 |
``` cpp
|
| 430 |
void set_exception_at_thread_exit(exception_ptr p);
|
| 431 |
```
|
| 432 |
|
| 433 |
+
*Preconditions:* `p` is not null.
|
| 434 |
|
| 435 |
*Effects:* Stores the exception pointer `p` in the shared state without
|
| 436 |
making that state ready immediately. Schedules that state to be made
|
| 437 |
ready when the current thread exits, after all objects of thread storage
|
| 438 |
duration associated with the current thread have been destroyed.
|
|
|
|
| 450 |
void swap(promise<R>& x, promise<R>& y) noexcept;
|
| 451 |
```
|
| 452 |
|
| 453 |
*Effects:* As if by `x.swap(y)`.
|
| 454 |
|
| 455 |
+
### Class template `future` <a id="futures.unique.future">[[futures.unique.future]]</a>
|
| 456 |
|
| 457 |
The class template `future` defines a type for asynchronous return
|
| 458 |
objects which do not share their shared state with other asynchronous
|
| 459 |
return objects. A default-constructed `future` object has no shared
|
| 460 |
state. A `future` object with shared state can be created by functions
|
| 461 |
+
on asynchronous providers [[futures.state]] or by the move constructor
|
| 462 |
+
and shares its shared state with the original asynchronous provider. The
|
| 463 |
+
result (value or exception) of a `future` object can be set by calling a
|
| 464 |
+
respective function on an object that shares the same shared state.
|
|
|
|
| 465 |
|
| 466 |
[*Note 1*: Member functions of `future` do not synchronize with
|
| 467 |
themselves or with member functions of `shared_future`. — *end note*]
|
| 468 |
|
| 469 |
The effect of calling any member function other than the destructor, the
|
|
|
|
| 471 |
which `valid() == false` is undefined.
|
| 472 |
|
| 473 |
[*Note 2*: It is valid to move from a future object for which
|
| 474 |
`valid() == false`. — *end note*]
|
| 475 |
|
| 476 |
+
[*Note 3*: Implementations should detect this case and throw an object
|
| 477 |
+
of type `future_error` with an error condition of
|
| 478 |
`future_errc::no_state`. — *end note*]
|
| 479 |
|
| 480 |
``` cpp
|
| 481 |
namespace std {
|
| 482 |
template<class R>
|
| 483 |
class future {
|
| 484 |
public:
|
| 485 |
future() noexcept;
|
| 486 |
future(future&&) noexcept;
|
| 487 |
+
future(const future&) = delete;
|
| 488 |
~future();
|
| 489 |
+
future& operator=(const future&) = delete;
|
| 490 |
future& operator=(future&&) noexcept;
|
| 491 |
shared_future<R> share() noexcept;
|
| 492 |
|
| 493 |
// retrieving the value
|
| 494 |
see below get();
|
|
|
|
| 503 |
future_status wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
|
| 504 |
};
|
| 505 |
}
|
| 506 |
```
|
| 507 |
|
| 508 |
+
The implementation provides the template `future` and two
|
| 509 |
specializations, `future<R&>` and `future<{}void>`. These differ only in
|
| 510 |
the return type and return value of the member function `get`, as set
|
| 511 |
out in its description, below.
|
| 512 |
|
| 513 |
``` cpp
|
| 514 |
future() noexcept;
|
| 515 |
```
|
| 516 |
|
| 517 |
+
*Effects:* The object does not refer to a shared state.
|
|
|
|
| 518 |
|
| 519 |
+
*Ensures:* `valid() == false`.
|
| 520 |
|
| 521 |
``` cpp
|
| 522 |
future(future&& rhs) noexcept;
|
| 523 |
```
|
| 524 |
|
| 525 |
*Effects:* Move constructs a `future` object that refers to the shared
|
| 526 |
state that was originally referred to by `rhs` (if any).
|
| 527 |
|
| 528 |
+
*Ensures:*
|
| 529 |
|
| 530 |
- `valid()` returns the same value as `rhs.valid()` prior to the
|
| 531 |
constructor invocation.
|
| 532 |
- `rhs.valid() == false`.
|
| 533 |
|
|
|
|
| 535 |
~future();
|
| 536 |
```
|
| 537 |
|
| 538 |
*Effects:*
|
| 539 |
|
| 540 |
+
- Releases any shared state [[futures.state]];
|
| 541 |
- destroys `*this`.
|
| 542 |
|
| 543 |
``` cpp
|
| 544 |
future& operator=(future&& rhs) noexcept;
|
| 545 |
```
|
| 546 |
|
| 547 |
*Effects:*
|
| 548 |
|
| 549 |
+
- Releases any shared state [[futures.state]].
|
| 550 |
- move assigns the contents of `rhs` to `*this`.
|
| 551 |
|
| 552 |
+
*Ensures:*
|
| 553 |
|
| 554 |
- `valid()` returns the same value as `rhs.valid()` prior to the
|
| 555 |
assignment.
|
| 556 |
- `rhs.valid() == false`.
|
| 557 |
|
|
|
|
| 559 |
shared_future<R> share() noexcept;
|
| 560 |
```
|
| 561 |
|
| 562 |
*Returns:* `shared_future<R>(std::move(*this))`.
|
| 563 |
|
| 564 |
+
*Ensures:* `valid() == false`.
|
| 565 |
|
| 566 |
``` cpp
|
| 567 |
R future::get();
|
| 568 |
R& future<R&>::get();
|
| 569 |
void future<void>::get();
|
|
|
|
| 575 |
|
| 576 |
*Effects:*
|
| 577 |
|
| 578 |
- `wait()`s until the shared state is ready, then retrieves the value
|
| 579 |
stored in the shared state;
|
| 580 |
+
- releases any shared state [[futures.state]].
|
| 581 |
|
| 582 |
*Returns:*
|
| 583 |
|
| 584 |
- `future::get()` returns the value `v` stored in the object’s shared
|
| 585 |
state as `std::move(v)`.
|
| 586 |
- `future<R&>::get()` returns the reference stored as value in the
|
| 587 |
object’s shared state.
|
| 588 |
- `future<void>::get()` returns nothing.
|
| 589 |
|
| 590 |
+
*Throws:* The stored exception, if an exception was stored in the shared
|
| 591 |
state.
|
| 592 |
|
| 593 |
+
*Ensures:* `valid() == false`.
|
| 594 |
|
| 595 |
``` cpp
|
| 596 |
bool valid() const noexcept;
|
| 597 |
```
|
| 598 |
|
|
|
|
| 608 |
template<class Rep, class Period>
|
| 609 |
future_status wait_for(const chrono::duration<Rep, Period>& rel_time) const;
|
| 610 |
```
|
| 611 |
|
| 612 |
*Effects:* None if the shared state contains a deferred
|
| 613 |
+
function [[futures.async]], otherwise blocks until the shared state is
|
| 614 |
+
ready or until the relative timeout [[thread.req.timing]] specified by
|
| 615 |
+
`rel_time` has expired.
|
| 616 |
|
| 617 |
*Returns:*
|
| 618 |
|
| 619 |
- `future_status::deferred` if the shared state contains a deferred
|
| 620 |
function.
|
| 621 |
- `future_status::ready` if the shared state is ready.
|
| 622 |
- `future_status::timeout` if the function is returning because the
|
| 623 |
+
relative timeout [[thread.req.timing]] specified by `rel_time` has
|
| 624 |
expired.
|
| 625 |
|
| 626 |
+
*Throws:* timeout-related exceptions [[thread.req.timing]].
|
| 627 |
|
| 628 |
``` cpp
|
| 629 |
template<class Clock, class Duration>
|
| 630 |
future_status wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
|
| 631 |
```
|
| 632 |
|
| 633 |
*Effects:* None if the shared state contains a deferred
|
| 634 |
+
function [[futures.async]], otherwise blocks until the shared state is
|
| 635 |
+
ready or until the absolute timeout [[thread.req.timing]] specified by
|
| 636 |
+
`abs_time` has expired.
|
| 637 |
|
| 638 |
*Returns:*
|
| 639 |
|
| 640 |
- `future_status::deferred` if the shared state contains a deferred
|
| 641 |
function.
|
| 642 |
- `future_status::ready` if the shared state is ready.
|
| 643 |
- `future_status::timeout` if the function is returning because the
|
| 644 |
+
absolute timeout [[thread.req.timing]] specified by `abs_time` has
|
| 645 |
expired.
|
| 646 |
|
| 647 |
+
*Throws:* timeout-related exceptions [[thread.req.timing]].
|
| 648 |
|
| 649 |
+
### Class template `shared_future` <a id="futures.shared.future">[[futures.shared.future]]</a>
|
| 650 |
|
| 651 |
The class template `shared_future` defines a type for asynchronous
|
| 652 |
return objects which may share their shared state with other
|
| 653 |
asynchronous return objects. A default-constructed `shared_future`
|
| 654 |
object has no shared state. A `shared_future` object with shared state
|
| 655 |
can be created by conversion from a `future` object and shares its
|
| 656 |
+
shared state with the original asynchronous provider [[futures.state]]
|
| 657 |
+
of the shared state. The result (value or exception) of a
|
| 658 |
+
`shared_future` object can be set by calling a respective function on an
|
| 659 |
+
object that shares the same shared state.
|
| 660 |
|
| 661 |
[*Note 1*: Member functions of `shared_future` do not synchronize with
|
| 662 |
themselves, but they synchronize with the shared state. — *end note*]
|
| 663 |
|
| 664 |
The effect of calling any member function other than the destructor, the
|
|
|
|
| 666 |
a `shared_future` object for which `valid() == false` is undefined.
|
| 667 |
|
| 668 |
[*Note 2*: It is valid to copy or move from a `shared_future` object
|
| 669 |
for which `valid()` is `false`. — *end note*]
|
| 670 |
|
| 671 |
+
[*Note 3*: Implementations should detect this case and throw an object
|
| 672 |
+
of type `future_error` with an error condition of
|
| 673 |
`future_errc::no_state`. — *end note*]
|
| 674 |
|
| 675 |
``` cpp
|
| 676 |
namespace std {
|
| 677 |
template<class R>
|
|
|
|
| 698 |
future_status wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
|
| 699 |
};
|
| 700 |
}
|
| 701 |
```
|
| 702 |
|
| 703 |
+
The implementation provides the template `shared_future` and two
|
| 704 |
specializations, `shared_future<R&>` and `shared_future<void>`. These
|
| 705 |
differ only in the return type and return value of the member function
|
| 706 |
`get`, as set out in its description, below.
|
| 707 |
|
| 708 |
``` cpp
|
| 709 |
shared_future() noexcept;
|
| 710 |
```
|
| 711 |
|
| 712 |
+
*Effects:* The object does not refer to a shared state.
|
|
|
|
| 713 |
|
| 714 |
+
*Ensures:* `valid() == false`.
|
| 715 |
|
| 716 |
``` cpp
|
| 717 |
shared_future(const shared_future& rhs) noexcept;
|
| 718 |
```
|
| 719 |
|
| 720 |
+
*Effects:* The object refers to the same shared state as `rhs` (if any).
|
|
|
|
| 721 |
|
| 722 |
+
*Ensures:* `valid()` returns the same value as `rhs.valid()`.
|
| 723 |
|
| 724 |
``` cpp
|
| 725 |
shared_future(future<R>&& rhs) noexcept;
|
| 726 |
shared_future(shared_future&& rhs) noexcept;
|
| 727 |
```
|
| 728 |
|
| 729 |
*Effects:* Move constructs a `shared_future` object that refers to the
|
| 730 |
shared state that was originally referred to by `rhs` (if any).
|
| 731 |
|
| 732 |
+
*Ensures:*
|
| 733 |
|
| 734 |
- `valid()` returns the same value as `rhs.valid()` returned prior to
|
| 735 |
the constructor invocation.
|
| 736 |
- `rhs.valid() == false`.
|
| 737 |
|
|
|
|
| 739 |
~shared_future();
|
| 740 |
```
|
| 741 |
|
| 742 |
*Effects:*
|
| 743 |
|
| 744 |
+
- Releases any shared state [[futures.state]];
|
| 745 |
- destroys `*this`.
|
| 746 |
|
| 747 |
``` cpp
|
| 748 |
shared_future& operator=(shared_future&& rhs) noexcept;
|
| 749 |
```
|
| 750 |
|
| 751 |
*Effects:*
|
| 752 |
|
| 753 |
+
- Releases any shared state [[futures.state]];
|
| 754 |
- move assigns the contents of `rhs` to `*this`.
|
| 755 |
|
| 756 |
+
*Ensures:*
|
| 757 |
|
| 758 |
- `valid()` returns the same value as `rhs.valid()` returned prior to
|
| 759 |
the assignment.
|
| 760 |
- `rhs.valid() == false`.
|
| 761 |
|
|
|
|
| 763 |
shared_future& operator=(const shared_future& rhs) noexcept;
|
| 764 |
```
|
| 765 |
|
| 766 |
*Effects:*
|
| 767 |
|
| 768 |
+
- Releases any shared state [[futures.state]];
|
| 769 |
- assigns the contents of `rhs` to `*this`. \[*Note 4*: As a result,
|
| 770 |
`*this` refers to the same shared state as `rhs` (if
|
| 771 |
any). — *end note*]
|
| 772 |
|
| 773 |
+
*Ensures:* `valid() == rhs.valid()`.
|
| 774 |
|
| 775 |
``` cpp
|
| 776 |
const R& shared_future::get() const;
|
| 777 |
R& shared_future<R&>::get() const;
|
| 778 |
void shared_future<void>::get() const;
|
|
|
|
| 782 |
specializations differ only in the return type and return value of the
|
| 783 |
member function `get`. — *end note*]
|
| 784 |
|
| 785 |
[*Note 2*: Access to a value object stored in the shared state is
|
| 786 |
unsynchronized, so programmers should apply only those operations on `R`
|
| 787 |
+
that do not introduce a data 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:*
|
|
|
|
| 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
|
| 805 |
state.
|
| 806 |
|
| 807 |
``` cpp
|
| 808 |
bool valid() const noexcept;
|
| 809 |
```
|
|
|
|
| 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 is
|
| 826 |
+
ready or until the relative timeout [[thread.req.timing]] specified by
|
| 827 |
+
`rel_time` has expired.
|
| 828 |
|
| 829 |
*Returns:*
|
| 830 |
|
| 831 |
- `future_status::deferred` if the shared state contains a deferred
|
| 832 |
function.
|
| 833 |
- `future_status::ready` if the shared state is ready.
|
| 834 |
- `future_status::timeout` if the function is returning because the
|
| 835 |
+
relative timeout [[thread.req.timing]] specified by `rel_time` has
|
| 836 |
expired.
|
| 837 |
|
| 838 |
+
*Throws:* timeout-related exceptions [[thread.req.timing]].
|
| 839 |
|
| 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 is
|
| 847 |
+
ready or until the absolute timeout [[thread.req.timing]] specified by
|
| 848 |
+
`abs_time` has expired.
|
| 849 |
|
| 850 |
*Returns:*
|
| 851 |
|
| 852 |
- `future_status::deferred` if the shared state contains a deferred
|
| 853 |
function.
|
| 854 |
- `future_status::ready` if the shared state is ready.
|
| 855 |
- `future_status::timeout` if the function is returning because the
|
| 856 |
+
absolute timeout [[thread.req.timing]] specified by `abs_time` has
|
| 857 |
expired.
|
| 858 |
|
| 859 |
+
*Throws:* timeout-related exceptions [[thread.req.timing]].
|
| 860 |
|
| 861 |
### Function template `async` <a id="futures.async">[[futures.async]]</a>
|
| 862 |
|
| 863 |
The function template `async` provides a mechanism to launch a function
|
| 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 |
+
[[nodiscard]] future<invoke_result_t<decay_t<F>, decay_t<Args>...>>
|
| 870 |
async(F&& f, Args&&... args);
|
| 871 |
template<class F, class... Args>
|
| 872 |
+
[[nodiscard]] future<invoke_result_t<decay_t<F>, decay_t<Args>...>>
|
| 873 |
async(launch policy, F&& f, Args&&... args);
|
| 874 |
```
|
| 875 |
|
| 876 |
+
*Mandates:* The following are all `true`:
|
|
|
|
| 877 |
|
| 878 |
+
- `is_constructible_v<decay_t<F>, F>`,
|
| 879 |
+
- `(is_constructible_v<decay_t<Args>, Args> &&...)`,
|
| 880 |
+
- `is_move_constructible_v<decay_t<F>>`,
|
| 881 |
+
- `(is_move_constructible_v<decay_t<Args>> &&...)`, and
|
| 882 |
+
- `is_invocable_v<decay_t<F>, decay_t<Args>...>`.
|
| 883 |
|
| 884 |
+
*Preconditions:* `decay_t<F>` and each type in `decay_t<Args>` meet the
|
| 885 |
+
*Cpp17MoveConstructible* requirements.
|
| 886 |
|
| 887 |
*Effects:* The first function behaves the same as a call to the second
|
| 888 |
function with a `policy` argument of `launch::async | launch::deferred`
|
| 889 |
and the same arguments for `F` and `Args`. The second function creates a
|
| 890 |
shared state that is associated with the returned `future` object. The
|
| 891 |
further behavior of the second function depends on the `policy` argument
|
| 892 |
as follows (if more than one of these conditions applies, the
|
| 893 |
implementation may choose any of the corresponding policies):
|
| 894 |
|
| 895 |
- If `launch::async` is set in `policy`, calls
|
| 896 |
+
`invoke(`*`decay-copy`*`(std::forward<F>(f)),`
|
| 897 |
+
*decay-copy*(std::forward\<Args\>(args))...) ([[func.require]],
|
| 898 |
[[thread.thread.constr]]) as if in a new thread of execution
|
| 899 |
+
represented by a `thread` object with the calls to *`decay-copy`*
|
| 900 |
being evaluated in the thread that called `async`. Any return value is
|
| 901 |
stored as the result in the shared state. Any exception propagated
|
| 902 |
+
from the execution of
|
| 903 |
+
`invoke(`*`decay-copy`*`(std::forward<F>(f)), `*`decay-copy`*`(std::forward<Args>(args))...)`
|
| 904 |
+
is stored as the exceptional result in the shared state. The `thread`
|
| 905 |
+
object is stored in the shared state and affects the behavior of any
|
| 906 |
+
asynchronous return objects that reference that state.
|
| 907 |
- If `launch::deferred` is set in `policy`, stores
|
| 908 |
+
*decay-copy*(std::forward\<F\>(f)) and
|
| 909 |
+
*decay-copy*(std::forward\<Args\>(args))... in the shared state. These
|
| 910 |
copies of `f` and `args` constitute a *deferred function*. Invocation
|
| 911 |
+
of the deferred function evaluates
|
| 912 |
+
`invoke(std::move(g), std::move(xyz))` where `g` is the stored value
|
| 913 |
+
of *decay-copy*(std::forward\<F\>(f)) and `xyz` is the stored copy of
|
| 914 |
+
*decay-copy*(std::forward\<Args\>(args)).... Any return value is
|
| 915 |
stored as the result in the shared state. Any exception propagated
|
| 916 |
from the execution of the deferred function is stored as the
|
| 917 |
exceptional result in the shared state. The shared state is not made
|
| 918 |
ready until the function has completed. The first call to a non-timed
|
| 919 |
+
waiting function [[futures.state]] on an asynchronous return object
|
| 920 |
+
referring to this shared state invokes the deferred function in the
|
| 921 |
+
thread that called the waiting function. Once evaluation of
|
| 922 |
+
`invoke(std::move(g), std::move(xyz))` begins, the function is no
|
| 923 |
longer considered deferred. \[*Note 1*: If this policy is specified
|
| 924 |
together with other policies, such as when using a `policy` value of
|
| 925 |
`launch::async | launch::deferred`, implementations should defer
|
| 926 |
invocation or the selection of the policy when no more concurrency can
|
| 927 |
be effectively exploited. — *end note*]
|
| 928 |
- If no value is set in the launch policy, or a value is set that is
|
| 929 |
+
neither specified in this document nor by the implementation, the
|
| 930 |
+
behavior is undefined.
|
| 931 |
|
| 932 |
*Returns:* An object of type
|
| 933 |
`future<invoke_result_t<decay_t<F>, decay_t<Args>...>``>` that refers to
|
| 934 |
the shared state created by this call to `async`.
|
| 935 |
|
| 936 |
[*Note 1*: If a future obtained from `async` is moved outside the local
|
| 937 |
+
scope, other code that uses the future should be aware that the future’s
|
| 938 |
+
destructor can block for the shared state to become
|
| 939 |
ready. — *end note*]
|
| 940 |
|
| 941 |
*Synchronization:* Regardless of the provided `policy` argument,
|
| 942 |
|
| 943 |
+
- the invocation of `async` synchronizes with [[intro.multithread]] the
|
| 944 |
+
invocation of `f`. \[*Note 2*: This statement applies even when the
|
| 945 |
+
corresponding `future` object is moved to another
|
| 946 |
thread. — *end note*] ; and
|
| 947 |
- the completion of the function `f` is sequenced
|
| 948 |
+
before [[intro.multithread]] the shared state is made ready.
|
| 949 |
\[*Note 3*: `f` might not be called at all, so its completion might
|
| 950 |
never happen. — *end note*]
|
| 951 |
|
| 952 |
If the implementation chooses the `launch::async` policy,
|
| 953 |
|
| 954 |
- a call to a waiting function on an asynchronous return object that
|
| 955 |
shares the shared state created by this `async` call shall block until
|
| 956 |
the associated thread has completed, as if joined, or else time
|
| 957 |
+
out [[thread.thread.member]];
|
| 958 |
- the associated thread completion synchronizes
|
| 959 |
+
with [[intro.multithread]] the return from the first function that
|
| 960 |
successfully detects the ready status of the shared state or with the
|
| 961 |
return from the last function that releases the shared state,
|
| 962 |
whichever happens first.
|
| 963 |
|
| 964 |
*Throws:* `system_error` if `policy == launch::async` and the
|
|
|
|
| 1018 |
void operator()(ArgTypes... );
|
| 1019 |
void make_ready_at_thread_exit(ArgTypes...);
|
| 1020 |
|
| 1021 |
void reset();
|
| 1022 |
};
|
| 1023 |
+
|
| 1024 |
template<class R, class... ArgTypes>
|
| 1025 |
void swap(packaged_task<R(ArgTypes...)>& x, packaged_task<R(ArgTypes...)>& y) noexcept;
|
|
|
|
|
|
|
| 1026 |
}
|
| 1027 |
```
|
| 1028 |
|
| 1029 |
+
#### Member functions <a id="futures.task.members">[[futures.task.members]]</a>
|
| 1030 |
|
| 1031 |
``` cpp
|
| 1032 |
packaged_task() noexcept;
|
| 1033 |
```
|
| 1034 |
|
| 1035 |
+
*Effects:* The object has no shared state and no stored task.
|
|
|
|
| 1036 |
|
| 1037 |
``` cpp
|
| 1038 |
template<class F>
|
| 1039 |
packaged_task(F&& f);
|
| 1040 |
```
|
| 1041 |
|
| 1042 |
+
*Constraints:* `remove_cvref_t<F>` is not the same type as
|
| 1043 |
+
`packaged_task<R(ArgTypes...)>`.
|
| 1044 |
+
|
| 1045 |
+
*Mandates:* `is_invocable_r_v<R, F&, ArgTypes...>` is `true`.
|
| 1046 |
+
|
| 1047 |
+
*Preconditions:* Invoking a copy of `f` behaves the same as invoking
|
| 1048 |
`f`.
|
| 1049 |
|
|
|
|
|
|
|
|
|
|
| 1050 |
*Effects:* Constructs a new `packaged_task` object with a shared state
|
| 1051 |
and initializes the object’s stored task with `std::forward<F>(f)`.
|
| 1052 |
|
| 1053 |
+
*Throws:* Any exceptions thrown by the copy or move constructor of `f`,
|
| 1054 |
+
or `bad_alloc` if memory for the internal data structures could not be
|
| 1055 |
+
allocated.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1056 |
|
| 1057 |
``` cpp
|
| 1058 |
packaged_task(packaged_task&& rhs) noexcept;
|
| 1059 |
```
|
| 1060 |
|
| 1061 |
+
*Effects:* Transfers ownership of `rhs`’s shared state to `*this`,
|
| 1062 |
+
leaving `rhs` with no shared state. Moves the stored task from `rhs` to
|
| 1063 |
+
`*this`.
|
| 1064 |
|
| 1065 |
+
*Ensures:* `rhs` has no shared state.
|
| 1066 |
|
| 1067 |
``` cpp
|
| 1068 |
packaged_task& operator=(packaged_task&& rhs) noexcept;
|
| 1069 |
```
|
| 1070 |
|
| 1071 |
*Effects:*
|
| 1072 |
|
| 1073 |
+
- Releases any shared state [[futures.state]];
|
| 1074 |
- calls `packaged_task(std::move(rhs)).swap(*this)`.
|
| 1075 |
|
| 1076 |
``` cpp
|
| 1077 |
~packaged_task();
|
| 1078 |
```
|
| 1079 |
|
| 1080 |
+
*Effects:* Abandons any shared state [[futures.state]].
|
| 1081 |
|
| 1082 |
``` cpp
|
| 1083 |
void swap(packaged_task& other) noexcept;
|
| 1084 |
```
|
| 1085 |
|
| 1086 |
*Effects:* Exchanges the shared states and stored tasks of `*this` and
|
| 1087 |
`other`.
|
| 1088 |
|
| 1089 |
+
*Ensures:* `*this` has the same shared state and stored task (if any) as
|
| 1090 |
+
`other` prior to the call to `swap`. `other` has the same shared state
|
| 1091 |
+
and stored task (if any) as `*this` prior to the call to `swap`.
|
| 1092 |
|
| 1093 |
``` cpp
|
| 1094 |
bool valid() const noexcept;
|
| 1095 |
```
|
| 1096 |
|
|
|
|
| 1101 |
```
|
| 1102 |
|
| 1103 |
*Returns:* A `future` object that shares the same shared state as
|
| 1104 |
`*this`.
|
| 1105 |
|
| 1106 |
+
*Synchronization:* Calls to this function do not introduce data
|
| 1107 |
+
races [[intro.multithread]] with calls to `operator()` or
|
| 1108 |
+
`make_ready_at_thread_exit`.
|
| 1109 |
+
|
| 1110 |
+
[*Note 1*: Such calls need not synchronize with each
|
| 1111 |
+
other. — *end note*]
|
| 1112 |
+
|
| 1113 |
+
*Throws:* A `future_error` object if an error occurs.
|
| 1114 |
|
| 1115 |
*Error conditions:*
|
| 1116 |
|
| 1117 |
- `future_already_retrieved` if `get_future` has already been called on
|
| 1118 |
a `packaged_task` object with the same shared state as `*this`.
|
|
|
|
| 1120 |
|
| 1121 |
``` cpp
|
| 1122 |
void operator()(ArgTypes... args);
|
| 1123 |
```
|
| 1124 |
|
| 1125 |
+
*Effects:* As if by *INVOKE*\<R\>(f, t₁, t₂, …, t$_N$) [[func.require]],
|
| 1126 |
+
where `f` is the stored task of `*this` and `t`₁`, t`₂`, `…`, t`$_N$ are
|
| 1127 |
+
the values in `args...`. If the task returns normally, the return value
|
| 1128 |
+
is stored as the asynchronous result in the shared state of `*this`,
|
| 1129 |
+
otherwise the exception thrown by the task is stored. The shared state
|
| 1130 |
+
of `*this` is made ready, and any threads blocked in a function waiting
|
| 1131 |
+
for the shared state of `*this` to become ready are unblocked.
|
| 1132 |
|
| 1133 |
+
*Throws:* A `future_error` exception object if there is no shared state
|
| 1134 |
or the stored task has already been invoked.
|
| 1135 |
|
| 1136 |
*Error conditions:*
|
| 1137 |
|
| 1138 |
- `promise_already_satisfied` if the stored task has already been
|
|
|
|
| 1141 |
|
| 1142 |
``` cpp
|
| 1143 |
void make_ready_at_thread_exit(ArgTypes... args);
|
| 1144 |
```
|
| 1145 |
|
| 1146 |
+
*Effects:* As if by *INVOKE*\<R\>(f, t₁, t₂, …, t$_N$) [[func.require]],
|
| 1147 |
+
where `f` is the stored task and `t`₁`, t`₂`, `…`, t`$_N$ are the values
|
| 1148 |
+
in `args...`. If the task returns normally, the return value is stored
|
| 1149 |
+
as the asynchronous result in the shared state of `*this`, otherwise the
|
| 1150 |
+
exception thrown by the task is stored. In either case, this is done
|
| 1151 |
+
without making that state ready [[futures.state]] immediately. Schedules
|
| 1152 |
+
the shared state to be made ready when the current thread exits, after
|
| 1153 |
+
all objects of thread storage duration associated with the current
|
| 1154 |
+
thread have been destroyed.
|
| 1155 |
|
| 1156 |
*Throws:* `future_error` if an error condition occurs.
|
| 1157 |
|
| 1158 |
*Error conditions:*
|
| 1159 |
|
|
|
|
| 1166 |
```
|
| 1167 |
|
| 1168 |
*Effects:* As if `*this = packaged_task(std::move(f))`, where `f` is the
|
| 1169 |
task stored in `*this`.
|
| 1170 |
|
| 1171 |
+
[*Note 2*: This constructs a new shared state for `*this`. The old
|
| 1172 |
+
state is abandoned [[futures.state]]. — *end note*]
|
| 1173 |
|
| 1174 |
*Throws:*
|
| 1175 |
|
| 1176 |
- `bad_alloc` if memory for the new shared state could not be allocated.
|
| 1177 |
- any exception thrown by the move constructor of the task stored in the
|
| 1178 |
shared state.
|
| 1179 |
- `future_error` with an error condition of `no_state` if `*this` has no
|
| 1180 |
shared state.
|
| 1181 |
|
| 1182 |
+
#### Globals <a id="futures.task.nonmembers">[[futures.task.nonmembers]]</a>
|
| 1183 |
|
| 1184 |
``` cpp
|
| 1185 |
template<class R, class... ArgTypes>
|
| 1186 |
void swap(packaged_task<R(ArgTypes...)>& x, packaged_task<R(ArgTypes...)>& y) noexcept;
|
| 1187 |
```
|
| 1188 |
|
| 1189 |
*Effects:* As if by `x.swap(y)`.
|
| 1190 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1191 |
<!-- Link reference definitions -->
|
| 1192 |
[alg.sorting]: algorithms.md#alg.sorting
|
|
|
|
| 1193 |
[atomics]: atomics.md#atomics
|
| 1194 |
+
[barrier.syn]: #barrier.syn
|
| 1195 |
[basic.life]: basic.md#basic.life
|
| 1196 |
[basic.stc.thread]: basic.md#basic.stc.thread
|
| 1197 |
[bitmask.types]: library.md#bitmask.types
|
| 1198 |
+
[class.prop]: class.md#class.prop
|
| 1199 |
+
[condition.variable.syn]: #condition.variable.syn
|
| 1200 |
+
[cpp17.allocator]: #cpp17.allocator
|
| 1201 |
+
[cpp17.defaultconstructible]: #cpp17.defaultconstructible
|
| 1202 |
+
[cpp17.destructible]: #cpp17.destructible
|
| 1203 |
+
[cpp17.moveassignable]: #cpp17.moveassignable
|
| 1204 |
+
[cpp17.moveconstructible]: #cpp17.moveconstructible
|
| 1205 |
+
[defns.block]: intro.md#defns.block
|
| 1206 |
[except.terminate]: except.md#except.terminate
|
| 1207 |
[func.require]: utilities.md#func.require
|
| 1208 |
[future.syn]: #future.syn
|
| 1209 |
[futures]: #futures
|
| 1210 |
[futures.async]: #futures.async
|
| 1211 |
[futures.errors]: #futures.errors
|
| 1212 |
+
[futures.future.error]: #futures.future.error
|
| 1213 |
[futures.overview]: #futures.overview
|
| 1214 |
[futures.promise]: #futures.promise
|
| 1215 |
+
[futures.shared.future]: #futures.shared.future
|
| 1216 |
[futures.state]: #futures.state
|
| 1217 |
[futures.task]: #futures.task
|
| 1218 |
[futures.task.members]: #futures.task.members
|
| 1219 |
[futures.task.nonmembers]: #futures.task.nonmembers
|
| 1220 |
+
[futures.unique.future]: #futures.unique.future
|
| 1221 |
+
[intro.multithread]: basic.md#intro.multithread
|
| 1222 |
+
[intro.races]: basic.md#intro.races
|
| 1223 |
+
[latch.syn]: #latch.syn
|
| 1224 |
[mutex.syn]: #mutex.syn
|
| 1225 |
[res.on.data.races]: library.md#res.on.data.races
|
| 1226 |
[res.on.exception.handling]: library.md#res.on.exception.handling
|
| 1227 |
+
[semaphore.syn]: #semaphore.syn
|
| 1228 |
+
[shared.mutex.syn]: #shared.mutex.syn
|
| 1229 |
+
[stopcallback]: #stopcallback
|
| 1230 |
+
[stopcallback.cons]: #stopcallback.cons
|
| 1231 |
+
[stopsource]: #stopsource
|
| 1232 |
+
[stopsource.cons]: #stopsource.cons
|
| 1233 |
+
[stopsource.mem]: #stopsource.mem
|
| 1234 |
+
[stopsource.nonmembers]: #stopsource.nonmembers
|
| 1235 |
+
[stoptoken]: #stoptoken
|
| 1236 |
+
[stoptoken.cons]: #stoptoken.cons
|
| 1237 |
+
[stoptoken.mem]: #stoptoken.mem
|
| 1238 |
+
[stoptoken.nonmembers]: #stoptoken.nonmembers
|
| 1239 |
[syserr]: diagnostics.md#syserr
|
| 1240 |
[syserr.syserr]: diagnostics.md#syserr.syserr
|
|
|
|
| 1241 |
[thread]: #thread
|
| 1242 |
+
[thread.barrier]: #thread.barrier
|
| 1243 |
+
[thread.barrier.class]: #thread.barrier.class
|
| 1244 |
[thread.condition]: #thread.condition
|
| 1245 |
[thread.condition.condvar]: #thread.condition.condvar
|
| 1246 |
[thread.condition.condvarany]: #thread.condition.condvarany
|
| 1247 |
[thread.condition.nonmember]: #thread.condition.nonmember
|
| 1248 |
+
[thread.condvarany.intwait]: #thread.condvarany.intwait
|
| 1249 |
+
[thread.condvarany.wait]: #thread.condvarany.wait
|
| 1250 |
+
[thread.coord]: #thread.coord
|
| 1251 |
[thread.general]: #thread.general
|
| 1252 |
+
[thread.jthread.class]: #thread.jthread.class
|
| 1253 |
+
[thread.jthread.cons]: #thread.jthread.cons
|
| 1254 |
+
[thread.jthread.mem]: #thread.jthread.mem
|
| 1255 |
+
[thread.jthread.special]: #thread.jthread.special
|
| 1256 |
+
[thread.jthread.static]: #thread.jthread.static
|
| 1257 |
+
[thread.jthread.stop]: #thread.jthread.stop
|
| 1258 |
+
[thread.latch]: #thread.latch
|
| 1259 |
+
[thread.latch.class]: #thread.latch.class
|
| 1260 |
[thread.lock]: #thread.lock
|
| 1261 |
[thread.lock.algorithm]: #thread.lock.algorithm
|
| 1262 |
[thread.lock.guard]: #thread.lock.guard
|
| 1263 |
[thread.lock.scoped]: #thread.lock.scoped
|
| 1264 |
[thread.lock.shared]: #thread.lock.shared
|
|
|
|
| 1288 |
[thread.req.lockable.req]: #thread.req.lockable.req
|
| 1289 |
[thread.req.lockable.timed]: #thread.req.lockable.timed
|
| 1290 |
[thread.req.native]: #thread.req.native
|
| 1291 |
[thread.req.paramname]: #thread.req.paramname
|
| 1292 |
[thread.req.timing]: #thread.req.timing
|
| 1293 |
+
[thread.sema]: #thread.sema
|
| 1294 |
+
[thread.sema.cnt]: #thread.sema.cnt
|
| 1295 |
[thread.sharedmutex.class]: #thread.sharedmutex.class
|
| 1296 |
[thread.sharedmutex.requirements]: #thread.sharedmutex.requirements
|
| 1297 |
[thread.sharedtimedmutex.class]: #thread.sharedtimedmutex.class
|
| 1298 |
[thread.sharedtimedmutex.requirements]: #thread.sharedtimedmutex.requirements
|
| 1299 |
+
[thread.stoptoken]: #thread.stoptoken
|
| 1300 |
+
[thread.stoptoken.intro]: #thread.stoptoken.intro
|
| 1301 |
+
[thread.stoptoken.syn]: #thread.stoptoken.syn
|
| 1302 |
+
[thread.summary]: #thread.summary
|
| 1303 |
[thread.syn]: #thread.syn
|
| 1304 |
[thread.thread.algorithm]: #thread.thread.algorithm
|
| 1305 |
[thread.thread.assign]: #thread.thread.assign
|
| 1306 |
[thread.thread.class]: #thread.thread.class
|
| 1307 |
[thread.thread.constr]: #thread.thread.constr
|
|
|
|
| 1312 |
[thread.thread.this]: #thread.thread.this
|
| 1313 |
[thread.threads]: #thread.threads
|
| 1314 |
[thread.timedmutex.class]: #thread.timedmutex.class
|
| 1315 |
[thread.timedmutex.recursive]: #thread.timedmutex.recursive
|
| 1316 |
[thread.timedmutex.requirements]: #thread.timedmutex.requirements
|
| 1317 |
+
[time]: time.md#time
|
| 1318 |
+
[time.clock]: time.md#time.clock
|
| 1319 |
+
[time.clock.req]: time.md#time.clock.req
|
| 1320 |
+
[time.duration]: time.md#time.duration
|
| 1321 |
+
[time.point]: time.md#time.point
|
| 1322 |
[unord.hash]: utilities.md#unord.hash
|
| 1323 |
|
| 1324 |
[^1]: All implementations for which standard time units are meaningful
|
| 1325 |
must necessarily have a steady clock within their hardware
|
| 1326 |
implementation.
|