- tmp/tmp67t7zsl9/{from.md → to.md} +929 -647
tmp/tmp67t7zsl9/{from.md → to.md}
RENAMED
|
@@ -36,33 +36,35 @@ shall be thrown if any of the function’s error conditions is detected or
|
|
| 36 |
a call to an operating system or other underlying API results in an
|
| 37 |
error that prevents the library function from meeting its
|
| 38 |
specifications. Failure to allocate storage shall be reported as
|
| 39 |
described in [[res.on.exception.handling]].
|
| 40 |
|
| 41 |
-
Consider a function in this clause that is specified to
|
| 42 |
-
of type `system_error` and specifies error conditions
|
| 43 |
-
`operation_not_permitted` for a thread that does not have
|
| 44 |
-
to perform the operation. Assume that, during the
|
| 45 |
-
function, an `errno` of `EPERM` is reported by a POSIX
|
| 46 |
-
the implementation. Since POSIX specifies an `errno` of
|
| 47 |
-
“the caller does not have the privilege to perform the
|
| 48 |
-
implementation maps `EPERM` to an `error_condition` of
|
| 49 |
`operation_not_permitted` ([[syserr]]) and an exception of type
|
| 50 |
-
`system_error` is thrown.
|
| 51 |
|
| 52 |
The `error_code` reported by such an exception’s `code()` member
|
| 53 |
function shall compare equal to one of the conditions specified in the
|
| 54 |
function’s error condition element.
|
| 55 |
|
| 56 |
### Native handles <a id="thread.req.native">[[thread.req.native]]</a>
|
| 57 |
|
| 58 |
Several classes described in this Clause have members
|
| 59 |
`native_handle_type` and `native_handle`. The presence of these members
|
| 60 |
-
and their semantics is *implementation-defined*.
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
| 64 |
|
| 65 |
### Timing specifications <a id="thread.req.timing">[[thread.req.timing]]</a>
|
| 66 |
|
| 67 |
Several functions described in this Clause take an argument to specify a
|
| 68 |
timeout. These timeouts are specified as either a `duration` or a
|
|
@@ -90,29 +92,32 @@ the clock time point of the return from timeout should be Cₜ + Dᵢ + Dₘ
|
|
| 90 |
when the clock is not adjusted during the timeout. If the clock is
|
| 91 |
adjusted to the time Cₐ during the timeout, the behavior should be as
|
| 92 |
follows:
|
| 93 |
|
| 94 |
- if Cₐ > Cₜ, the waiting function should wake as soon as possible, i.e.
|
| 95 |
-
Cₐ + Dᵢ + Dₘ, since the timeout is already satisfied. This
|
| 96 |
specification may result in the total duration of the wait decreasing
|
| 97 |
-
when measured against a steady clock.
|
| 98 |
- if Cₐ <= Cₜ, the waiting function should not time out until
|
| 99 |
`Clock::now()` returns a time Cₙ >= Cₜ, i.e. waking at Cₜ + Dᵢ + Dₘ.
|
| 100 |
-
When the clock is adjusted backwards, this specification
|
| 101 |
-
the total duration of the wait increasing when measured
|
| 102 |
-
steady clock. When the clock is adjusted forwards, this
|
| 103 |
-
may result in the total duration of the wait decreasing
|
| 104 |
-
against a steady clock.
|
| 105 |
|
| 106 |
An implementation shall return from such a timeout at any point from the
|
| 107 |
time specified above to the time it would return from a steady-clock
|
| 108 |
relative timeout on the difference between Cₜ and the time point of the
|
| 109 |
-
call to the `_until` function.
|
| 110 |
-
duration of the wait when the clock is adjusted forwards.
|
| 111 |
|
| 112 |
-
|
| 113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
The resolution of timing provided by an implementation depends on both
|
| 116 |
operating system and hardware. The finest resolution provided by an
|
| 117 |
implementation is called the *native resolution*.
|
| 118 |
|
|
@@ -120,37 +125,45 @@ Implementation-provided clocks that are used for these functions shall
|
|
| 120 |
meet the `TrivialClock` requirements ([[time.clock.req]]).
|
| 121 |
|
| 122 |
A function that takes an argument which specifies a timeout will throw
|
| 123 |
if, during its execution, a clock, time point, or time duration throws
|
| 124 |
an exception. Such exceptions are referred to as *timeout-related
|
| 125 |
-
exceptions*.
|
|
|
|
|
|
|
| 126 |
supplied by the implementation as specified in [[time.clock]] do not
|
| 127 |
-
throw exceptions.
|
| 128 |
|
| 129 |
-
### Requirements for Lockable types <a id="thread.req.lockable">[[thread.req.lockable]]</a>
|
| 130 |
|
| 131 |
#### In general <a id="thread.req.lockable.general">[[thread.req.lockable.general]]</a>
|
| 132 |
|
| 133 |
An *execution agent* is an entity such as a thread that may perform work
|
| 134 |
-
in parallel with other execution agents.
|
| 135 |
-
|
|
|
|
|
|
|
|
|
|
| 136 |
The calling agent is determined by context, e.g. the calling thread that
|
| 137 |
contains the call, and so on.
|
| 138 |
|
| 139 |
-
Some lockable objects are “agent oblivious” in that they
|
| 140 |
-
execution agent model because they do not determine or
|
| 141 |
-
ID (e.g., an ordinary spin lock).
|
| 142 |
|
| 143 |
The standard library templates `unique_lock` ([[thread.lock.unique]]),
|
| 144 |
-
`
|
| 145 |
-
[[thread.lock.
|
|
|
|
| 146 |
[[thread.condition.condvarany]]) all operate on user-supplied lockable
|
| 147 |
objects. The `BasicLockable` requirements, the `Lockable` requirements,
|
| 148 |
and the `TimedLockable` requirements list the requirements imposed by
|
| 149 |
these library types in order to acquire or release ownership of a `lock`
|
| 150 |
-
by a given execution agent.
|
| 151 |
-
|
|
|
|
|
|
|
| 152 |
|
| 153 |
#### `BasicLockable` requirements <a id="thread.req.lockable.basic">[[thread.req.lockable.basic]]</a>
|
| 154 |
|
| 155 |
A type `L` meets the `BasicLockable` requirements if the following
|
| 156 |
expressions are well-formed and have the specified semantics (`m`
|
|
@@ -183,11 +196,11 @@ type `L`).
|
|
| 183 |
|
| 184 |
``` cpp
|
| 185 |
m.try_lock()
|
| 186 |
```
|
| 187 |
|
| 188 |
-
*Effects:*
|
| 189 |
without blocking. If an exception is thrown then a lock shall not have
|
| 190 |
been acquired for the current execution agent.
|
| 191 |
|
| 192 |
*Return type:* `bool`.
|
| 193 |
|
|
@@ -204,11 +217,11 @@ of `time_point` ([[time.point]])).
|
|
| 204 |
|
| 205 |
``` cpp
|
| 206 |
m.try_lock_for(rel_time)
|
| 207 |
```
|
| 208 |
|
| 209 |
-
*Effects:*
|
| 210 |
within the relative timeout ([[thread.req.timing]]) specified by
|
| 211 |
`rel_time`. The function shall not return within the timeout specified
|
| 212 |
by `rel_time` unless it has obtained a lock on `m` for the current
|
| 213 |
execution agent. If an exception is thrown then a lock shall not have
|
| 214 |
been acquired for the current execution agent.
|
|
@@ -219,11 +232,11 @@ been acquired for the current execution agent.
|
|
| 219 |
|
| 220 |
``` cpp
|
| 221 |
m.try_lock_until(abs_time)
|
| 222 |
```
|
| 223 |
|
| 224 |
-
*Effects:*
|
| 225 |
before the absolute timeout ([[thread.req.timing]]) specified by
|
| 226 |
`abs_time`. The function shall not return before the timeout specified
|
| 227 |
by `abs_time` unless it has obtained a lock on `m` for the current
|
| 228 |
execution agent. If an exception is thrown then a lock shall not have
|
| 229 |
been acquired for the current execution agent.
|
|
@@ -232,11 +245,11 @@ been acquired for the current execution agent.
|
|
| 232 |
|
| 233 |
*Returns:* `true` if the lock was acquired, `false` otherwise.
|
| 234 |
|
| 235 |
### `decay_copy` <a id="thread.decaycopy">[[thread.decaycopy]]</a>
|
| 236 |
|
| 237 |
-
In several places in this Clause the operation
|
| 238 |
All such uses mean call the function `decay_copy(x)` and use the result,
|
| 239 |
where `decay_copy` is defined as follows:
|
| 240 |
|
| 241 |
``` cpp
|
| 242 |
template <class T> decay_t<T> decay_copy(T&& v)
|
|
@@ -244,12 +257,16 @@ template <class T> decay_t<T> decay_copy(T&& v)
|
|
| 244 |
```
|
| 245 |
|
| 246 |
## Threads <a id="thread.threads">[[thread.threads]]</a>
|
| 247 |
|
| 248 |
[[thread.threads]] describes components that can be used to create and
|
| 249 |
-
manage threads.
|
| 250 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 251 |
|
| 252 |
``` cpp
|
| 253 |
namespace std {
|
| 254 |
class thread;
|
| 255 |
|
|
@@ -276,21 +293,23 @@ thread. A `thread` object uniquely represents a particular thread of
|
|
| 276 |
execution. That representation may be transferred to other `thread`
|
| 277 |
objects in such a way that no two `thread` objects simultaneously
|
| 278 |
represent the same thread of execution. A thread of execution is
|
| 279 |
*detached* when no `thread` object represents that thread. Objects of
|
| 280 |
class `thread` can be in a state that does not represent a thread of
|
| 281 |
-
execution.
|
|
|
|
|
|
|
| 282 |
after default construction, after being moved from, or after a
|
| 283 |
-
successful call to `detach` or `join`.
|
| 284 |
|
| 285 |
``` cpp
|
| 286 |
namespace std {
|
| 287 |
class thread {
|
| 288 |
public:
|
| 289 |
// types:
|
| 290 |
class id;
|
| 291 |
-
|
| 292 |
|
| 293 |
// construct/copy/destroy:
|
| 294 |
thread() noexcept;
|
| 295 |
template <class F, class... Args> explicit thread(F&& f, Args&&... args);
|
| 296 |
~thread();
|
|
@@ -342,19 +361,19 @@ namespace std {
|
|
| 342 |
An object of type `thread::id` provides a unique identifier for each
|
| 343 |
thread of execution and a single distinct value for all `thread` objects
|
| 344 |
that do not represent a thread of execution ([[thread.thread.class]]).
|
| 345 |
Each thread of execution has an associated `thread::id` object that is
|
| 346 |
not equal to the `thread::id` object of any other thread of execution
|
| 347 |
-
and that is not equal to the `thread::id` object of any `
|
| 348 |
-
|
| 349 |
|
| 350 |
`thread::id` shall be a trivially copyable class (Clause [[class]]).
|
| 351 |
The library may reuse the value of a `thread::id` of a terminated thread
|
| 352 |
that can no longer be joined.
|
| 353 |
|
| 354 |
-
Relational operators allow `thread::id` objects to be used
|
| 355 |
-
associative containers.
|
| 356 |
|
| 357 |
``` cpp
|
| 358 |
id() noexcept;
|
| 359 |
```
|
| 360 |
|
|
@@ -385,78 +404,80 @@ described in [[alg.sorting]].
|
|
| 385 |
|
| 386 |
``` cpp
|
| 387 |
bool operator<=(thread::id x, thread::id y) noexcept;
|
| 388 |
```
|
| 389 |
|
| 390 |
-
*Returns:* `!(y < x)`
|
| 391 |
|
| 392 |
``` cpp
|
| 393 |
bool operator>(thread::id x, thread::id y) noexcept;
|
| 394 |
```
|
| 395 |
|
| 396 |
-
*Returns:* `y < x`
|
| 397 |
|
| 398 |
``` cpp
|
| 399 |
bool operator>=(thread::id x, thread::id y) noexcept;
|
| 400 |
```
|
| 401 |
|
| 402 |
-
*Returns:* `!(x < y)`
|
| 403 |
|
| 404 |
``` cpp
|
| 405 |
template<class charT, class traits>
|
| 406 |
basic_ostream<charT, traits>&
|
| 407 |
-
operator<< (basic_ostream<charT, traits>&
|
| 408 |
```
|
| 409 |
|
| 410 |
*Effects:* Inserts an unspecified text representation of `id` into
|
| 411 |
`out`. For two objects of type `thread::id` `x` and `y`, if `x == y` the
|
| 412 |
`thread::id` objects shall have the same text representation and if
|
| 413 |
`x != y` the `thread::id` objects shall have distinct text
|
| 414 |
representations.
|
| 415 |
|
| 416 |
-
*Returns:* `out`
|
| 417 |
|
| 418 |
``` cpp
|
| 419 |
template <> struct hash<thread::id>;
|
| 420 |
```
|
| 421 |
|
| 422 |
-
The
|
| 423 |
-
template `hash` ([[unord.hash]]).
|
| 424 |
|
| 425 |
#### `thread` constructors <a id="thread.thread.constr">[[thread.thread.constr]]</a>
|
| 426 |
|
| 427 |
``` cpp
|
| 428 |
thread() noexcept;
|
| 429 |
```
|
| 430 |
|
| 431 |
*Effects:* Constructs a `thread` object that does not represent a thread
|
| 432 |
of execution.
|
| 433 |
|
| 434 |
-
`get_id() == id()`
|
| 435 |
|
| 436 |
``` cpp
|
| 437 |
template <class F, class... Args> explicit thread(F&& f, Args&&... args);
|
| 438 |
```
|
| 439 |
|
| 440 |
*Requires:* `F` and each `Ti` in `Args` shall satisfy the
|
| 441 |
-
`MoveConstructible` requirements.
|
| 442 |
-
`std::forward<F>(f)), `*`DECAY_COPY`*`(std::forward<Args>(args))...)` ([[func.require]])
|
| 443 |
shall be a valid expression.
|
| 444 |
|
| 445 |
*Remarks:* This constructor shall not participate in overload resolution
|
| 446 |
if `decay_t<F>` is the same type as `std::thread`.
|
| 447 |
|
| 448 |
*Effects:* Constructs an object of type `thread`. The new thread of
|
| 449 |
execution executes
|
| 450 |
-
*`INVOKE`*`(`*`DECAY_COPY`*`(
|
| 451 |
with the calls to *`DECAY_COPY`* being evaluated in the constructing
|
| 452 |
-
thread. Any return value from this invocation is ignored.
|
| 453 |
-
|
| 454 |
-
|
| 455 |
-
invocation of
|
| 456 |
-
|
| 457 |
-
|
|
|
|
|
|
|
|
|
|
| 458 |
|
| 459 |
*Synchronization:* The completion of the invocation of the constructor
|
| 460 |
synchronizes with the beginning of the invocation of the copy of `f`.
|
| 461 |
|
| 462 |
*Postconditions:* `get_id() != id()`. `*this` represents the newly
|
|
@@ -484,30 +505,31 @@ of `x.get_id()` prior to the start of construction.
|
|
| 484 |
|
| 485 |
``` cpp
|
| 486 |
~thread();
|
| 487 |
```
|
| 488 |
|
| 489 |
-
If `joinable()`, calls `
|
| 490 |
-
|
| 491 |
-
|
| 492 |
-
|
| 493 |
-
|
| 494 |
-
|
|
|
|
| 495 |
|
| 496 |
#### `thread` assignment <a id="thread.thread.assign">[[thread.thread.assign]]</a>
|
| 497 |
|
| 498 |
``` cpp
|
| 499 |
thread& operator=(thread&& x) noexcept;
|
| 500 |
```
|
| 501 |
|
| 502 |
-
*Effects:* If `joinable()`, calls `
|
| 503 |
-
|
| 504 |
|
| 505 |
*Postconditions:* `x.get_id() == id()` and `get_id()` returns the value
|
| 506 |
of `x.get_id()` prior to the assignment.
|
| 507 |
|
| 508 |
-
*Returns:* `*this`
|
| 509 |
|
| 510 |
#### `thread` members <a id="thread.thread.member">[[thread.thread.member]]</a>
|
| 511 |
|
| 512 |
``` cpp
|
| 513 |
void swap(thread& x) noexcept;
|
|
@@ -517,51 +539,49 @@ void swap(thread& x) noexcept;
|
|
| 517 |
|
| 518 |
``` cpp
|
| 519 |
bool joinable() const noexcept;
|
| 520 |
```
|
| 521 |
|
| 522 |
-
*Returns:* `get_id() != id()`
|
| 523 |
|
| 524 |
``` cpp
|
| 525 |
void join();
|
| 526 |
```
|
| 527 |
|
| 528 |
-
`joinable()` is `true`.
|
| 529 |
-
|
| 530 |
*Effects:* Blocks until the thread represented by `*this` has
|
| 531 |
completed.
|
| 532 |
|
| 533 |
*Synchronization:* The completion of the thread represented by `*this`
|
| 534 |
synchronizes with ([[intro.multithread]]) the corresponding successful
|
| 535 |
-
`join()` return.
|
|
|
|
|
|
|
| 536 |
|
| 537 |
*Postconditions:* The thread represented by `*this` has completed.
|
| 538 |
`get_id() == id()`.
|
| 539 |
|
| 540 |
*Throws:* `system_error` when an exception is
|
| 541 |
required ([[thread.req.exception]]).
|
| 542 |
|
| 543 |
*Error conditions:*
|
| 544 |
|
| 545 |
- `resource_deadlock_would_occur` — if deadlock is detected or
|
| 546 |
-
`
|
| 547 |
- `no_such_process` — if the thread is not valid.
|
| 548 |
- `invalid_argument` — if the thread is not joinable.
|
| 549 |
|
| 550 |
``` cpp
|
| 551 |
void detach();
|
| 552 |
```
|
| 553 |
|
| 554 |
-
`joinable()` is `true`.
|
| 555 |
-
|
| 556 |
*Effects:* The thread represented by `*this` continues execution without
|
| 557 |
the calling thread blocking. When `detach()` returns, `*this` no longer
|
| 558 |
represents the possibly continuing thread of execution. When the thread
|
| 559 |
previously represented by `*this` ends execution, the implementation
|
| 560 |
shall release any owned resources.
|
| 561 |
|
| 562 |
-
`get_id() == id()`.
|
| 563 |
|
| 564 |
*Throws:* `system_error` when an exception is
|
| 565 |
required ([[thread.req.exception]]).
|
| 566 |
|
| 567 |
*Error conditions:*
|
|
@@ -581,36 +601,38 @@ execution represented by `*this`.
|
|
| 581 |
|
| 582 |
``` cpp
|
| 583 |
unsigned hardware_concurrency() noexcept;
|
| 584 |
```
|
| 585 |
|
| 586 |
-
*Returns:* The number of hardware thread contexts.
|
| 587 |
-
|
| 588 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 589 |
|
| 590 |
#### `thread` specialized algorithms <a id="thread.thread.algorithm">[[thread.thread.algorithm]]</a>
|
| 591 |
|
| 592 |
``` cpp
|
| 593 |
void swap(thread& x, thread& y) noexcept;
|
| 594 |
```
|
| 595 |
|
| 596 |
-
*Effects:* `x.swap(y)`
|
| 597 |
|
| 598 |
### Namespace `this_thread` <a id="thread.thread.this">[[thread.thread.this]]</a>
|
| 599 |
|
| 600 |
``` cpp
|
| 601 |
-
namespace std {
|
| 602 |
-
namespace this_thread {
|
| 603 |
thread::id get_id() noexcept;
|
| 604 |
|
| 605 |
void yield() noexcept;
|
| 606 |
template <class Clock, class Duration>
|
| 607 |
void sleep_until(const chrono::time_point<Clock, Duration>& abs_time);
|
| 608 |
template <class Rep, class Period>
|
| 609 |
void sleep_for(const chrono::duration<Rep, Period>& rel_time);
|
| 610 |
}
|
| 611 |
-
}
|
| 612 |
```
|
| 613 |
|
| 614 |
``` cpp
|
| 615 |
thread::id this_thread::get_id() noexcept;
|
| 616 |
```
|
|
@@ -657,48 +679,49 @@ timeout ([[thread.req.timing]]) specified by `rel_time`.
|
|
| 657 |
|
| 658 |
This section provides mechanisms for mutual exclusion: mutexes, locks,
|
| 659 |
and call once. These mechanisms ease the production of race-free
|
| 660 |
programs ([[intro.multithread]]).
|
| 661 |
|
|
|
|
|
|
|
| 662 |
``` cpp
|
| 663 |
namespace std {
|
| 664 |
class mutex;
|
| 665 |
class recursive_mutex;
|
| 666 |
class timed_mutex;
|
| 667 |
class recursive_timed_mutex;
|
| 668 |
|
| 669 |
-
struct defer_lock_t { };
|
| 670 |
-
struct try_to_lock_t { };
|
| 671 |
-
struct adopt_lock_t { };
|
| 672 |
|
| 673 |
-
constexpr defer_lock_t defer_lock { };
|
| 674 |
-
constexpr try_to_lock_t try_to_lock { };
|
| 675 |
-
constexpr adopt_lock_t adopt_lock { };
|
| 676 |
|
| 677 |
template <class Mutex> class lock_guard;
|
|
|
|
| 678 |
template <class Mutex> class unique_lock;
|
| 679 |
|
| 680 |
template <class Mutex>
|
| 681 |
void swap(unique_lock<Mutex>& x, unique_lock<Mutex>& y) noexcept;
|
| 682 |
|
| 683 |
template <class L1, class L2, class... L3> int try_lock(L1&, L2&, L3&...);
|
| 684 |
template <class L1, class L2, class... L3> void lock(L1&, L2&, L3&...);
|
| 685 |
|
| 686 |
-
struct once_flag
|
| 687 |
-
constexpr once_flag() noexcept;
|
| 688 |
-
|
| 689 |
-
once_flag(const once_flag&) = delete;
|
| 690 |
-
once_flag& operator=(const once_flag&) = delete;
|
| 691 |
-
};
|
| 692 |
|
| 693 |
template<class Callable, class... Args>
|
| 694 |
void call_once(once_flag& flag, Callable&& func, Args&&... args);
|
| 695 |
}
|
| 696 |
```
|
| 697 |
|
|
|
|
|
|
|
| 698 |
``` cpp
|
| 699 |
namespace std {
|
|
|
|
| 700 |
class shared_timed_mutex;
|
| 701 |
template <class Mutex> class shared_lock;
|
| 702 |
template <class Mutex>
|
| 703 |
void swap(shared_lock<Mutex>& x, shared_lock<Mutex>& y) noexcept;
|
| 704 |
}
|
|
@@ -716,15 +739,15 @@ unlock. Mutexes can be either recursive or non-recursive, and can grant
|
|
| 716 |
simultaneous ownership to one or many execution agents. Both recursive
|
| 717 |
and non-recursive mutexes are supplied.
|
| 718 |
|
| 719 |
#### Mutex types <a id="thread.mutex.requirements.mutex">[[thread.mutex.requirements.mutex]]</a>
|
| 720 |
|
| 721 |
-
The *mutex types* are the standard library types `
|
| 722 |
-
`
|
| 723 |
-
`
|
| 724 |
-
|
| 725 |
-
|
| 726 |
|
| 727 |
The mutex types shall meet the `Lockable` requirements (
|
| 728 |
[[thread.req.lockable.req]]).
|
| 729 |
|
| 730 |
The mutex types shall be `DefaultConstructible` and `Destructible`. If
|
|
@@ -737,37 +760,39 @@ functions of the mutex types shall be:
|
|
| 737 |
|
| 738 |
- `resource_unavailable_try_again` — if any native handle type
|
| 739 |
manipulated is not available.
|
| 740 |
- `operation_not_permitted` — if the thread does not have the privilege
|
| 741 |
to perform the operation.
|
| 742 |
-
- `device_or_resource_busy` — if any native handle type manipulated is
|
| 743 |
-
already locked.
|
| 744 |
- `invalid_argument` — if any native handle type manipulated as part of
|
| 745 |
mutex construction is incorrect.
|
| 746 |
|
| 747 |
The implementation shall provide lock and unlock operations, as
|
| 748 |
described below. For purposes of determining the existence of a data
|
| 749 |
race, these behave as atomic operations ([[intro.multithread]]). The
|
| 750 |
lock and unlock operations on a single mutex shall appear to occur in a
|
| 751 |
-
single total order.
|
| 752 |
-
|
| 753 |
-
|
| 754 |
-
|
| 755 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 756 |
|
| 757 |
The expression `m.lock()` shall be well-formed and have the following
|
| 758 |
semantics:
|
| 759 |
|
| 760 |
-
*Requires:* If `m` is of type `
|
| 761 |
-
`
|
| 762 |
|
| 763 |
*Effects:* Blocks the calling thread until ownership of the mutex can be
|
| 764 |
obtained for the calling thread.
|
| 765 |
|
| 766 |
-
The calling thread owns the mutex.
|
| 767 |
|
| 768 |
-
*Return type:* `void`
|
| 769 |
|
| 770 |
*Synchronization:* Prior `unlock()` operations on the same object shall
|
| 771 |
*synchronize with* ([[intro.multithread]]) this operation.
|
| 772 |
|
| 773 |
*Throws:* `system_error` when an exception is
|
|
@@ -777,51 +802,53 @@ required ([[thread.req.exception]]).
|
|
| 777 |
|
| 778 |
- `operation_not_permitted` — if the thread does not have the privilege
|
| 779 |
to perform the operation.
|
| 780 |
- `resource_deadlock_would_occur` — if the implementation detects that a
|
| 781 |
deadlock would occur.
|
| 782 |
-
- `device_or_resource_busy` — if the mutex is already locked and
|
| 783 |
-
blocking is not possible.
|
| 784 |
|
| 785 |
The expression `m.try_lock()` shall be well-formed and have the
|
| 786 |
following semantics:
|
| 787 |
|
| 788 |
-
*Requires:* If `m` is of type `
|
| 789 |
-
`
|
| 790 |
|
| 791 |
*Effects:* Attempts to obtain ownership of the mutex for the calling
|
| 792 |
thread without blocking. If ownership is not obtained, there is no
|
| 793 |
effect and `try_lock()` immediately returns. An implementation may fail
|
| 794 |
-
to obtain the lock even if it is not held by any other thread.
|
| 795 |
-
spurious failure is normally uncommon, but allows interesting
|
| 796 |
-
implementations based on a simple compare and exchange
|
| 797 |
-
(Clause [[atomics]]). An implementation should ensure that `try_lock()`
|
| 798 |
-
does not consistently return `false` in the absence of contending mutex
|
| 799 |
-
acquisitions.
|
| 800 |
|
| 801 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 802 |
|
| 803 |
*Returns:* `true` if ownership of the mutex was obtained for the calling
|
| 804 |
thread, otherwise `false`.
|
| 805 |
|
| 806 |
*Synchronization:* If `try_lock()` returns `true`, prior `unlock()`
|
| 807 |
operations on the same object *synchronize
|
| 808 |
-
with* ([[intro.multithread]]) this operation.
|
| 809 |
-
|
| 810 |
-
|
| 811 |
-
|
|
|
|
|
|
|
| 812 |
|
| 813 |
*Throws:* Nothing.
|
| 814 |
|
| 815 |
The expression `m.unlock()` shall be well-formed and have the following
|
| 816 |
semantics:
|
| 817 |
|
| 818 |
-
The calling thread shall own the mutex.
|
| 819 |
|
| 820 |
*Effects:* Releases the calling thread’s ownership of the mutex.
|
| 821 |
|
| 822 |
-
*Return type:* `void`
|
| 823 |
|
| 824 |
*Synchronization:* This operation synchronizes
|
| 825 |
with ([[intro.multithread]]) subsequent lock operations that obtain
|
| 826 |
ownership on the same object.
|
| 827 |
|
|
@@ -841,11 +868,11 @@ namespace std {
|
|
| 841 |
|
| 842 |
void lock();
|
| 843 |
bool try_lock();
|
| 844 |
void unlock();
|
| 845 |
|
| 846 |
-
|
| 847 |
native_handle_type native_handle(); // See~[thread.req.native]
|
| 848 |
};
|
| 849 |
}
|
| 850 |
```
|
| 851 |
|
|
@@ -853,26 +880,27 @@ The class `mutex` provides a non-recursive mutex with exclusive
|
|
| 853 |
ownership semantics. If one thread owns a mutex object, attempts by
|
| 854 |
another thread to acquire ownership of that object will fail (for
|
| 855 |
`try_lock()`) or block (for `lock()`) until the owning thread has
|
| 856 |
released ownership with a call to `unlock()`.
|
| 857 |
|
| 858 |
-
After a thread `A` has called `unlock()`, releasing a mutex,
|
| 859 |
-
possible for another thread `B` to lock the same mutex, observe
|
| 860 |
-
is no longer in use, unlock it, and destroy it, before thread
|
| 861 |
-
appears to have returned from its unlock call. Implementations are
|
| 862 |
required to handle such scenarios correctly, as long as thread `A`
|
| 863 |
doesn’t access the mutex after the unlock call returns. These cases
|
| 864 |
typically occur when a reference-counted object contains a mutex that is
|
| 865 |
-
used to protect the reference count.
|
| 866 |
|
| 867 |
-
The class `mutex` shall satisfy all the
|
| 868 |
[[thread.mutex.requirements]]). It shall be a standard-layout class
|
| 869 |
(Clause [[class]]).
|
| 870 |
|
| 871 |
-
A program may deadlock if the thread that owns a `mutex`
|
| 872 |
-
`lock()` on that object. If the implementation can detect
|
| 873 |
-
a `resource_deadlock_would_occur` error condition may be
|
|
|
|
| 874 |
|
| 875 |
The behavior of a program is undefined if it destroys a `mutex` object
|
| 876 |
owned by any thread or a thread terminates while owning a `mutex`
|
| 877 |
object.
|
| 878 |
|
|
@@ -890,11 +918,11 @@ namespace std {
|
|
| 890 |
|
| 891 |
void lock();
|
| 892 |
bool try_lock() noexcept;
|
| 893 |
void unlock();
|
| 894 |
|
| 895 |
-
|
| 896 |
native_handle_type native_handle(); // See~[thread.req.native]
|
| 897 |
};
|
| 898 |
}
|
| 899 |
```
|
| 900 |
|
|
@@ -902,13 +930,13 @@ The class `recursive_mutex` provides a recursive mutex with exclusive
|
|
| 902 |
ownership semantics. If one thread owns a `recursive_mutex` object,
|
| 903 |
attempts by another thread to acquire ownership of that object will fail
|
| 904 |
(for `try_lock()`) or block (for `lock()`) until the first thread has
|
| 905 |
completely released ownership.
|
| 906 |
|
| 907 |
-
The class `recursive_mutex` shall satisfy all the
|
| 908 |
-
[[thread.mutex.requirements]]). It shall be a
|
| 909 |
-
(Clause [[class]]).
|
| 910 |
|
| 911 |
A thread that owns a `recursive_mutex` object may acquire additional
|
| 912 |
levels of ownership by calling `lock()` or `try_lock()` on that object.
|
| 913 |
It is unspecified how many levels of ownership may be acquired by a
|
| 914 |
single thread. If a thread has already acquired the maximum level of
|
|
@@ -924,38 +952,39 @@ The behavior of a program is undefined if:
|
|
| 924 |
- it destroys a `recursive_mutex` object owned by any thread or
|
| 925 |
- a thread terminates while owning a `recursive_mutex` object.
|
| 926 |
|
| 927 |
#### Timed mutex types <a id="thread.timedmutex.requirements">[[thread.timedmutex.requirements]]</a>
|
| 928 |
|
| 929 |
-
The *timed mutex types* are the standard library types
|
| 930 |
-
`
|
| 931 |
-
|
| 932 |
-
|
| 933 |
-
`
|
| 934 |
-
[[time.
|
| 935 |
-
of `time_point` ([[time.point]]).
|
| 936 |
|
| 937 |
The timed mutex types shall meet the `TimedLockable` requirements (
|
| 938 |
[[thread.req.lockable.timed]]).
|
| 939 |
|
| 940 |
The expression `m.try_lock_for(rel_time)` shall be well-formed and have
|
| 941 |
the following semantics:
|
| 942 |
|
| 943 |
-
If `m` is of type `
|
| 944 |
calling thread does not own the mutex.
|
| 945 |
|
| 946 |
*Effects:* The function attempts to obtain ownership of the mutex within
|
| 947 |
the relative timeout ([[thread.req.timing]]) specified by `rel_time`.
|
| 948 |
If the time specified by `rel_time` is less than or equal to
|
| 949 |
`rel_time.zero()`, the function attempts to obtain ownership without
|
| 950 |
blocking (as if by calling `try_lock()`). The function shall return
|
| 951 |
within the timeout specified by `rel_time` only if it has obtained
|
| 952 |
-
ownership of the mutex object.
|
| 953 |
-
guarantee that ownership will be obtained if the lock is available, but
|
| 954 |
-
implementations are expected to make a strong effort to do so.
|
| 955 |
|
| 956 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
| 957 |
|
| 958 |
*Returns:* `true` if ownership was obtained, otherwise `false`.
|
| 959 |
|
| 960 |
*Synchronization:* If `try_lock_for()` returns `true`, prior `unlock()`
|
| 961 |
operations on the same object *synchronize
|
|
@@ -964,23 +993,24 @@ with* ([[intro.multithread]]) this operation.
|
|
| 964 |
*Throws:* Timeout-related exceptions ([[thread.req.timing]]).
|
| 965 |
|
| 966 |
The expression `m.try_lock_until(abs_time)` shall be well-formed and
|
| 967 |
have the following semantics:
|
| 968 |
|
| 969 |
-
*Requires:* If `m` is of type `
|
| 970 |
-
|
| 971 |
|
| 972 |
*Effects:* The function attempts to obtain ownership of the mutex. If
|
| 973 |
`abs_time` has already passed, the function attempts to obtain ownership
|
| 974 |
without blocking (as if by calling `try_lock()`). The function shall
|
| 975 |
return before the absolute timeout ([[thread.req.timing]]) specified by
|
| 976 |
-
`abs_time` only if it has obtained ownership of the mutex object.
|
| 977 |
-
with `try_lock()`, there is no guarantee that ownership will be obtained
|
| 978 |
-
if the lock is available, but implementations are expected to make a
|
| 979 |
-
strong effort to do so.
|
| 980 |
|
| 981 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
| 982 |
|
| 983 |
*Returns:* `true` if ownership was obtained, otherwise `false`.
|
| 984 |
|
| 985 |
*Synchronization:* If `try_lock_until()` returns `true`, prior
|
| 986 |
`unlock()` operations on the same object *synchronize
|
|
@@ -1006,11 +1036,11 @@ namespace std {
|
|
| 1006 |
bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
|
| 1007 |
template <class Clock, class Duration>
|
| 1008 |
bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
|
| 1009 |
void unlock();
|
| 1010 |
|
| 1011 |
-
|
| 1012 |
native_handle_type native_handle(); // See~[thread.req.native]
|
| 1013 |
};
|
| 1014 |
}
|
| 1015 |
```
|
| 1016 |
|
|
@@ -1020,11 +1050,11 @@ by another thread to acquire ownership of that object will fail (for
|
|
| 1020 |
`try_lock()`) or block (for `lock()`, `try_lock_for()`, and
|
| 1021 |
`try_lock_until()`) until the owning thread has released ownership with
|
| 1022 |
a call to `unlock()` or the call to `try_lock_for()` or
|
| 1023 |
`try_lock_until()` times out (having failed to obtain ownership).
|
| 1024 |
|
| 1025 |
-
The class `timed_mutex` shall satisfy all of the
|
| 1026 |
requirements ([[thread.timedmutex.requirements]]). It shall be a
|
| 1027 |
standard-layout class (Clause [[class]]).
|
| 1028 |
|
| 1029 |
The behavior of a program is undefined if:
|
| 1030 |
|
|
@@ -1052,11 +1082,11 @@ namespace std {
|
|
| 1052 |
bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
|
| 1053 |
template <class Clock, class Duration>
|
| 1054 |
bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
|
| 1055 |
void unlock();
|
| 1056 |
|
| 1057 |
-
|
| 1058 |
native_handle_type native_handle(); // See~[thread.req.native]
|
| 1059 |
};
|
| 1060 |
}
|
| 1061 |
```
|
| 1062 |
|
|
@@ -1066,11 +1096,11 @@ exclusive ownership semantics. If one thread owns a
|
|
| 1066 |
ownership of that object will fail (for `try_lock()`) or block (for
|
| 1067 |
`lock()`, `try_lock_for()`, and `try_lock_until()`) until the owning
|
| 1068 |
thread has completely released ownership or the call to `try_lock_for()`
|
| 1069 |
or `try_lock_until()` times out (having failed to obtain ownership).
|
| 1070 |
|
| 1071 |
-
The class `recursive_timed_mutex` shall satisfy all of the
|
| 1072 |
requirements ([[thread.timedmutex.requirements]]). It shall be a
|
| 1073 |
standard-layout class (Clause [[class]]).
|
| 1074 |
|
| 1075 |
A thread that owns a `recursive_timed_mutex` object may acquire
|
| 1076 |
additional levels of ownership by calling `lock()`, `try_lock()`,
|
|
@@ -1088,20 +1118,17 @@ the object be acquired by another thread.
|
|
| 1088 |
The behavior of a program is undefined if:
|
| 1089 |
|
| 1090 |
- it destroys a `recursive_timed_mutex` object owned by any thread, or
|
| 1091 |
- a thread terminates while owning a `recursive_timed_mutex` object.
|
| 1092 |
|
| 1093 |
-
#### Shared
|
| 1094 |
|
| 1095 |
-
The standard library
|
| 1096 |
-
mutex
|
| 1097 |
-
|
| 1098 |
-
|
| 1099 |
-
|
| 1100 |
-
an object of an instantiation of `duration` ([[time.duration]]), and
|
| 1101 |
-
`abs_time` denotes an object of an instantiation of `time_point` (
|
| 1102 |
-
[[time.point]]).
|
| 1103 |
|
| 1104 |
In addition to the exclusive lock ownership mode specified in
|
| 1105 |
[[thread.mutex.requirements.mutex]], shared mutex types provide a
|
| 1106 |
*shared lock* ownership mode. Multiple execution agents can
|
| 1107 |
simultaneously hold a shared lock ownership of a shared mutex type. But
|
|
@@ -1121,28 +1148,26 @@ following semantics:
|
|
| 1121 |
|
| 1122 |
*Effects:* Blocks the calling thread until shared ownership of the mutex
|
| 1123 |
can be obtained for the calling thread. If an exception is thrown then a
|
| 1124 |
shared lock shall not have been acquired for the current thread.
|
| 1125 |
|
| 1126 |
-
The calling thread has a shared lock on the mutex.
|
| 1127 |
|
| 1128 |
*Return type:* `void`.
|
| 1129 |
|
| 1130 |
*Synchronization:* Prior `unlock()` operations on the same object shall
|
| 1131 |
synchronize with ([[intro.multithread]]) this operation.
|
| 1132 |
|
| 1133 |
-
*Throws:* `system_error` when an exception is
|
| 1134 |
-
([[thread.req.exception]]).
|
| 1135 |
|
| 1136 |
*Error conditions:*
|
| 1137 |
|
| 1138 |
- `operation_not_permitted` — if the thread does not have the privilege
|
| 1139 |
to perform the operation.
|
| 1140 |
- `resource_deadlock_would_occur` — if the implementation detects that a
|
| 1141 |
deadlock would occur.
|
| 1142 |
-
- `device_or_resource_busy` — if the mutex is already locked and
|
| 1143 |
-
blocking is not possible.
|
| 1144 |
|
| 1145 |
The expression `m.unlock_shared()` shall be well-formed and have the
|
| 1146 |
following semantics:
|
| 1147 |
|
| 1148 |
*Requires:* The calling thread shall hold a shared lock on the mutex.
|
|
@@ -1173,15 +1198,71 @@ other thread.
|
|
| 1173 |
|
| 1174 |
*Returns:* `true` if the shared ownership lock was acquired, `false`
|
| 1175 |
otherwise.
|
| 1176 |
|
| 1177 |
*Synchronization:* If `try_lock_shared()` returns `true`, prior
|
| 1178 |
-
`unlock()` operations on the same object synchronize
|
| 1179 |
-
([[intro.multithread]]) this operation.
|
| 1180 |
|
| 1181 |
*Throws:* Nothing.
|
| 1182 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1183 |
The expression `m.try_lock_shared_for(rel_time)` shall be well-formed
|
| 1184 |
and have the following semantics:
|
| 1185 |
|
| 1186 |
*Requires:* The calling thread has no ownership of the mutex.
|
| 1187 |
|
|
@@ -1189,14 +1270,17 @@ and have the following semantics:
|
|
| 1189 |
thread within the relative timeout ([[thread.req.timing]]) specified by
|
| 1190 |
`rel_time`. If the time specified by `rel_time` is less than or equal to
|
| 1191 |
`rel_time.zero()`, the function attempts to obtain ownership without
|
| 1192 |
blocking (as if by calling `try_lock_shared()`). The function shall
|
| 1193 |
return within the timeout specified by `rel_time` only if it has
|
| 1194 |
-
obtained shared ownership of the mutex object.
|
| 1195 |
-
|
| 1196 |
-
|
| 1197 |
-
|
|
|
|
|
|
|
|
|
|
| 1198 |
acquired for the current thread.
|
| 1199 |
|
| 1200 |
*Return type:* `bool`.
|
| 1201 |
|
| 1202 |
*Returns:* `true` if the shared lock was acquired, `false` otherwise.
|
|
@@ -1215,14 +1299,17 @@ and have the following semantics:
|
|
| 1215 |
*Effects:* The function attempts to obtain shared ownership of the
|
| 1216 |
mutex. If `abs_time` has already passed, the function attempts to obtain
|
| 1217 |
shared ownership without blocking (as if by calling
|
| 1218 |
`try_lock_shared()`). The function shall return before the absolute
|
| 1219 |
timeout ([[thread.req.timing]]) specified by `abs_time` only if it has
|
| 1220 |
-
obtained shared ownership of the mutex object.
|
| 1221 |
-
|
| 1222 |
-
|
| 1223 |
-
|
|
|
|
|
|
|
|
|
|
| 1224 |
acquired for the current thread.
|
| 1225 |
|
| 1226 |
*Return type:* `bool`.
|
| 1227 |
|
| 1228 |
*Returns:* `true` if the shared lock was acquired, `false` otherwise.
|
|
@@ -1269,14 +1356,13 @@ namespace std {
|
|
| 1269 |
```
|
| 1270 |
|
| 1271 |
The class `shared_timed_mutex` provides a non-recursive mutex with
|
| 1272 |
shared ownership semantics.
|
| 1273 |
|
| 1274 |
-
The class `shared_timed_mutex` shall satisfy all of the
|
| 1275 |
-
|
| 1276 |
-
|
| 1277 |
-
class (Clause [[class]]).
|
| 1278 |
|
| 1279 |
The behavior of a program is undefined if:
|
| 1280 |
|
| 1281 |
- it destroys a `shared_timed_mutex` object owned by any thread,
|
| 1282 |
- a thread attempts to recursively gain any ownership of a
|
|
@@ -1290,13 +1376,15 @@ A *lock* is an object that holds a reference to a lockable object and
|
|
| 1290 |
may unlock the lockable object during the lock’s destruction (such as
|
| 1291 |
when leaving block scope). An execution agent may use a lock to aid in
|
| 1292 |
managing ownership of a lockable object in an exception safe manner. A
|
| 1293 |
lock is said to *own* a lockable object if it is currently managing the
|
| 1294 |
ownership of that lockable object for an execution agent. A lock does
|
| 1295 |
-
not manage the lifetime of the lockable object it references.
|
| 1296 |
-
|
| 1297 |
-
|
|
|
|
|
|
|
| 1298 |
|
| 1299 |
Some lock constructors take tag types which describe what should be done
|
| 1300 |
with the lockable object during the lock’s construction.
|
| 1301 |
|
| 1302 |
``` cpp
|
|
@@ -1305,35 +1393,37 @@ namespace std {
|
|
| 1305 |
struct try_to_lock_t { }; // try to acquire ownership of the mutex
|
| 1306 |
// without blocking
|
| 1307 |
struct adopt_lock_t { }; // assume the calling thread has already
|
| 1308 |
// obtained mutex ownership and manage it
|
| 1309 |
|
| 1310 |
-
constexpr defer_lock_t defer_lock { };
|
| 1311 |
-
constexpr try_to_lock_t try_to_lock { };
|
| 1312 |
-
constexpr adopt_lock_t adopt_lock { };
|
| 1313 |
}
|
| 1314 |
```
|
| 1315 |
|
| 1316 |
#### Class template `lock_guard` <a id="thread.lock.guard">[[thread.lock.guard]]</a>
|
| 1317 |
|
| 1318 |
``` cpp
|
| 1319 |
namespace std {
|
| 1320 |
template <class Mutex>
|
| 1321 |
class lock_guard {
|
| 1322 |
public:
|
| 1323 |
-
|
| 1324 |
|
| 1325 |
explicit lock_guard(mutex_type& m);
|
| 1326 |
lock_guard(mutex_type& m, adopt_lock_t);
|
| 1327 |
~lock_guard();
|
| 1328 |
|
| 1329 |
-
lock_guard(
|
| 1330 |
-
lock_guard& operator=(
|
| 1331 |
|
| 1332 |
private:
|
| 1333 |
mutex_type& pm; // exposition only
|
| 1334 |
};
|
|
|
|
|
|
|
| 1335 |
}
|
| 1336 |
```
|
| 1337 |
|
| 1338 |
An object of type `lock_guard` controls the ownership of a lockable
|
| 1339 |
object within a scope. A `lock_guard` object maintains ownership of a
|
|
@@ -1345,43 +1435,107 @@ object referenced by `pm` does not exist for the entire lifetime of the
|
|
| 1345 |
|
| 1346 |
``` cpp
|
| 1347 |
explicit lock_guard(mutex_type& m);
|
| 1348 |
```
|
| 1349 |
|
| 1350 |
-
If `mutex_type` is not a recursive mutex, the calling thread
|
| 1351 |
-
own the mutex `m`.
|
| 1352 |
|
| 1353 |
-
*Effects:* `m.lock()`
|
| 1354 |
|
| 1355 |
-
`&pm == &m`
|
| 1356 |
|
| 1357 |
``` cpp
|
| 1358 |
lock_guard(mutex_type& m, adopt_lock_t);
|
| 1359 |
```
|
| 1360 |
|
| 1361 |
-
The calling thread owns the mutex `m`.
|
| 1362 |
|
| 1363 |
-
`&pm == &m`
|
| 1364 |
|
| 1365 |
*Throws:* Nothing.
|
| 1366 |
|
| 1367 |
``` cpp
|
| 1368 |
~lock_guard();
|
| 1369 |
```
|
| 1370 |
|
| 1371 |
-
*Effects:* `pm.unlock()`
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1372 |
|
| 1373 |
#### Class template `unique_lock` <a id="thread.lock.unique">[[thread.lock.unique]]</a>
|
| 1374 |
|
| 1375 |
``` cpp
|
| 1376 |
namespace std {
|
| 1377 |
template <class Mutex>
|
| 1378 |
class unique_lock {
|
| 1379 |
public:
|
| 1380 |
-
|
| 1381 |
|
| 1382 |
-
// [thread.lock.unique.cons], construct/copy/destroy
|
| 1383 |
unique_lock() noexcept;
|
| 1384 |
explicit unique_lock(mutex_type& m);
|
| 1385 |
unique_lock(mutex_type& m, defer_lock_t) noexcept;
|
| 1386 |
unique_lock(mutex_type& m, try_to_lock_t);
|
| 1387 |
unique_lock(mutex_type& m, adopt_lock_t);
|
|
@@ -1389,41 +1543,43 @@ namespace std {
|
|
| 1389 |
unique_lock(mutex_type& m, const chrono::time_point<Clock, Duration>& abs_time);
|
| 1390 |
template <class Rep, class Period>
|
| 1391 |
unique_lock(mutex_type& m, const chrono::duration<Rep, Period>& rel_time);
|
| 1392 |
~unique_lock();
|
| 1393 |
|
| 1394 |
-
unique_lock(
|
| 1395 |
-
unique_lock& operator=(
|
| 1396 |
|
| 1397 |
unique_lock(unique_lock&& u) noexcept;
|
| 1398 |
unique_lock& operator=(unique_lock&& u);
|
| 1399 |
|
| 1400 |
-
// [thread.lock.unique.locking], locking
|
| 1401 |
void lock();
|
| 1402 |
bool try_lock();
|
| 1403 |
|
| 1404 |
template <class Rep, class Period>
|
| 1405 |
bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
|
| 1406 |
template <class Clock, class Duration>
|
| 1407 |
bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
|
| 1408 |
|
| 1409 |
void unlock();
|
| 1410 |
|
| 1411 |
-
// [thread.lock.unique.mod], modifiers
|
| 1412 |
void swap(unique_lock& u) noexcept;
|
| 1413 |
mutex_type* release() noexcept;
|
| 1414 |
|
| 1415 |
-
// [thread.lock.unique.obs], observers
|
| 1416 |
bool owns_lock() const noexcept;
|
| 1417 |
explicit operator bool () const noexcept;
|
| 1418 |
mutex_type* mutex() const noexcept;
|
| 1419 |
|
| 1420 |
private:
|
| 1421 |
mutex_type* pm; // exposition only
|
| 1422 |
bool owns; // exposition only
|
| 1423 |
};
|
| 1424 |
|
|
|
|
|
|
|
| 1425 |
template <class Mutex>
|
| 1426 |
void swap(unique_lock<Mutex>& x, unique_lock<Mutex>& y) noexcept;
|
| 1427 |
}
|
| 1428 |
```
|
| 1429 |
|
|
@@ -1436,15 +1592,16 @@ program is undefined if the contained pointer `pm` is not null and the
|
|
| 1436 |
lockable object pointed to by `pm` does not exist for the entire
|
| 1437 |
remaining lifetime ([[basic.life]]) of the `unique_lock` object. The
|
| 1438 |
supplied `Mutex` type shall meet the `BasicLockable` requirements (
|
| 1439 |
[[thread.req.lockable.basic]]).
|
| 1440 |
|
| 1441 |
-
`unique_lock<Mutex>` meets the `BasicLockable` requirements.
|
| 1442 |
-
meets the `Lockable` requirements (
|
| 1443 |
-
`unique_lock<Mutex>` also meets the
|
| 1444 |
-
meets the `TimedLockable`
|
| 1445 |
-
`unique_lock<Mutex>` also
|
|
|
|
| 1446 |
|
| 1447 |
##### `unique_lock` constructors, destructor, and assignment <a id="thread.lock.unique.cons">[[thread.lock.unique.cons]]</a>
|
| 1448 |
|
| 1449 |
``` cpp
|
| 1450 |
unique_lock() noexcept;
|
|
@@ -1456,81 +1613,81 @@ unique_lock() noexcept;
|
|
| 1456 |
|
| 1457 |
``` cpp
|
| 1458 |
explicit unique_lock(mutex_type& m);
|
| 1459 |
```
|
| 1460 |
|
| 1461 |
-
If `mutex_type` is not a recursive mutex the calling thread
|
| 1462 |
-
the mutex.
|
| 1463 |
|
| 1464 |
*Effects:* Constructs an object of type `unique_lock` and calls
|
| 1465 |
`m.lock()`.
|
| 1466 |
|
| 1467 |
-
*Postconditions:* `pm ==
|
| 1468 |
|
| 1469 |
``` cpp
|
| 1470 |
unique_lock(mutex_type& m, defer_lock_t) noexcept;
|
| 1471 |
```
|
| 1472 |
|
| 1473 |
*Effects:* Constructs an object of type `unique_lock`.
|
| 1474 |
|
| 1475 |
-
*Postconditions:* `pm ==
|
| 1476 |
|
| 1477 |
``` cpp
|
| 1478 |
unique_lock(mutex_type& m, try_to_lock_t);
|
| 1479 |
```
|
| 1480 |
|
| 1481 |
-
The supplied `Mutex` type shall meet the `Lockable`
|
| 1482 |
requirements ([[thread.req.lockable.req]]). If `mutex_type` is not a
|
| 1483 |
recursive mutex the calling thread does not own the mutex.
|
| 1484 |
|
| 1485 |
*Effects:* Constructs an object of type `unique_lock` and calls
|
| 1486 |
`m.try_lock()`.
|
| 1487 |
|
| 1488 |
-
*Postconditions:* `pm ==
|
| 1489 |
-
returned by the call to `m.try_lock()`.
|
| 1490 |
|
| 1491 |
``` cpp
|
| 1492 |
unique_lock(mutex_type& m, adopt_lock_t);
|
| 1493 |
```
|
| 1494 |
|
| 1495 |
-
The calling thread
|
| 1496 |
|
| 1497 |
*Effects:* Constructs an object of type `unique_lock`.
|
| 1498 |
|
| 1499 |
-
*Postconditions:* `pm ==
|
| 1500 |
|
| 1501 |
*Throws:* Nothing.
|
| 1502 |
|
| 1503 |
``` cpp
|
| 1504 |
template <class Clock, class Duration>
|
| 1505 |
unique_lock(mutex_type& m, const chrono::time_point<Clock, Duration>& abs_time);
|
| 1506 |
```
|
| 1507 |
|
| 1508 |
-
If `mutex_type` is not a recursive mutex the calling thread
|
| 1509 |
-
the mutex. The supplied `Mutex` type shall meet the
|
| 1510 |
-
requirements ([[thread.req.lockable.timed]]).
|
| 1511 |
|
| 1512 |
*Effects:* Constructs an object of type `unique_lock` and calls
|
| 1513 |
`m.try_lock_until(abs_time)`.
|
| 1514 |
|
| 1515 |
-
*Postconditions:* `pm ==
|
| 1516 |
-
returned by the call to `m.try_lock_until(abs_time)`.
|
| 1517 |
|
| 1518 |
``` cpp
|
| 1519 |
template <class Rep, class Period>
|
| 1520 |
unique_lock(mutex_type& m, const chrono::duration<Rep, Period>& rel_time);
|
| 1521 |
```
|
| 1522 |
|
| 1523 |
-
If `mutex_type` is not a recursive mutex the calling thread
|
| 1524 |
-
the mutex. The supplied `Mutex` type shall meet the
|
| 1525 |
-
requirements ([[thread.req.lockable.timed]]).
|
| 1526 |
|
| 1527 |
*Effects:* Constructs an object of type `unique_lock` and calls
|
| 1528 |
`m.try_lock_for(rel_time)`.
|
| 1529 |
|
| 1530 |
-
*Postconditions:* `pm ==
|
| 1531 |
-
returned by the call to `m.try_lock_for(rel_time)`.
|
| 1532 |
|
| 1533 |
``` cpp
|
| 1534 |
unique_lock(unique_lock&& u) noexcept;
|
| 1535 |
```
|
| 1536 |
|
|
@@ -1546,13 +1703,13 @@ unique_lock& operator=(unique_lock&& u);
|
|
| 1546 |
|
| 1547 |
*Postconditions:* `pm == u_p.pm` and `owns == u_p.owns` (where `u_p` is
|
| 1548 |
the state of `u` just prior to this construction), `u.pm == 0` and
|
| 1549 |
`u.owns == false`.
|
| 1550 |
|
| 1551 |
-
With a recursive mutex it is possible for both `*this` and
|
| 1552 |
-
the same mutex before the assignment. In this case, `*this`
|
| 1553 |
-
mutex after the assignment and `u` will not.
|
| 1554 |
|
| 1555 |
*Throws:* Nothing.
|
| 1556 |
|
| 1557 |
``` cpp
|
| 1558 |
~unique_lock();
|
|
@@ -1564,96 +1721,104 @@ mutex after the assignment and `u` will not.
|
|
| 1564 |
|
| 1565 |
``` cpp
|
| 1566 |
void lock();
|
| 1567 |
```
|
| 1568 |
|
| 1569 |
-
*Effects:* `pm->lock()`
|
| 1570 |
|
| 1571 |
-
`owns == true`
|
| 1572 |
|
| 1573 |
-
*Throws:* Any exception thrown by `pm->lock()`. `system_error`
|
| 1574 |
-
exception is required ([[thread.req.exception]]).
|
| 1575 |
-
|
| 1576 |
-
|
| 1577 |
-
|
|
|
|
|
|
|
| 1578 |
|
| 1579 |
``` cpp
|
| 1580 |
bool try_lock();
|
| 1581 |
```
|
| 1582 |
|
| 1583 |
-
The supplied `Mutex` shall meet the `Lockable`
|
| 1584 |
requirements ([[thread.req.lockable.req]]).
|
| 1585 |
|
| 1586 |
-
*Effects:* `pm->try_lock()`
|
| 1587 |
|
| 1588 |
*Returns:* The value returned by the call to `try_lock()`.
|
| 1589 |
|
| 1590 |
-
`owns == res`, where `res` is the value returned by
|
| 1591 |
-
`try_lock()`.
|
| 1592 |
|
| 1593 |
-
*Throws:* Any exception thrown by `pm->try_lock()`. `system_error`
|
| 1594 |
-
exception is required
|
| 1595 |
-
|
| 1596 |
-
|
| 1597 |
-
|
|
|
|
|
|
|
| 1598 |
|
| 1599 |
``` cpp
|
| 1600 |
template <class Clock, class Duration>
|
| 1601 |
bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
|
| 1602 |
```
|
| 1603 |
|
| 1604 |
*Requires:* The supplied `Mutex` type shall meet the `TimedLockable`
|
| 1605 |
requirements ([[thread.req.lockable.timed]]).
|
| 1606 |
|
| 1607 |
-
*Effects:* `pm->try_lock_until(abs_time)`
|
| 1608 |
|
| 1609 |
*Returns:* The value returned by the call to `try_lock_until(abs_time)`.
|
| 1610 |
|
| 1611 |
-
`owns == res`, where `res` is the value returned by
|
| 1612 |
-
`try_lock_until(abs_time)`.
|
| 1613 |
|
| 1614 |
*Throws:* Any exception thrown by `pm->try_lock_until()`. `system_error`
|
| 1615 |
-
|
| 1616 |
-
|
| 1617 |
-
|
| 1618 |
-
|
|
|
|
|
|
|
| 1619 |
|
| 1620 |
``` cpp
|
| 1621 |
template <class Rep, class Period>
|
| 1622 |
bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
|
| 1623 |
```
|
| 1624 |
|
| 1625 |
*Requires:* The supplied `Mutex` type shall meet the `TimedLockable`
|
| 1626 |
requirements ([[thread.req.lockable.timed]]).
|
| 1627 |
|
| 1628 |
-
*Effects:* `pm->try_lock_for(rel_time)`.
|
| 1629 |
|
| 1630 |
*Returns:* The value returned by the call to `try_lock_until(rel_time)`.
|
| 1631 |
|
| 1632 |
-
`owns == res`, where `res` is the value returned by
|
| 1633 |
-
`try_lock_for(rel_time)`.
|
| 1634 |
|
| 1635 |
*Throws:* Any exception thrown by `pm->try_lock_for()`. `system_error`
|
| 1636 |
-
|
| 1637 |
-
|
| 1638 |
-
|
| 1639 |
-
|
|
|
|
|
|
|
| 1640 |
|
| 1641 |
``` cpp
|
| 1642 |
void unlock();
|
| 1643 |
```
|
| 1644 |
|
| 1645 |
-
*Effects:* `pm->unlock()`
|
| 1646 |
|
| 1647 |
-
`owns == false`
|
| 1648 |
|
| 1649 |
*Throws:* `system_error` when an exception is
|
| 1650 |
required ([[thread.req.exception]]).
|
| 1651 |
|
| 1652 |
*Error conditions:*
|
| 1653 |
|
| 1654 |
-
- `operation_not_permitted` — if on entry `owns` is false.
|
| 1655 |
|
| 1656 |
##### `unique_lock` modifiers <a id="thread.lock.unique.mod">[[thread.lock.unique.mod]]</a>
|
| 1657 |
|
| 1658 |
``` cpp
|
| 1659 |
void swap(unique_lock& u) noexcept;
|
|
@@ -1672,43 +1837,42 @@ mutex_type* release() noexcept;
|
|
| 1672 |
``` cpp
|
| 1673 |
template <class Mutex>
|
| 1674 |
void swap(unique_lock<Mutex>& x, unique_lock<Mutex>& y) noexcept;
|
| 1675 |
```
|
| 1676 |
|
| 1677 |
-
*Effects:* `x.swap(y)`
|
| 1678 |
|
| 1679 |
##### `unique_lock` observers <a id="thread.lock.unique.obs">[[thread.lock.unique.obs]]</a>
|
| 1680 |
|
| 1681 |
``` cpp
|
| 1682 |
bool owns_lock() const noexcept;
|
| 1683 |
```
|
| 1684 |
|
| 1685 |
-
*Returns:* `owns`
|
| 1686 |
|
| 1687 |
``` cpp
|
| 1688 |
explicit operator bool() const noexcept;
|
| 1689 |
```
|
| 1690 |
|
| 1691 |
-
*Returns:* `owns`
|
| 1692 |
|
| 1693 |
``` cpp
|
| 1694 |
mutex_type *mutex() const noexcept;
|
| 1695 |
```
|
| 1696 |
|
| 1697 |
-
*Returns:* `pm`
|
| 1698 |
|
| 1699 |
#### Class template `shared_lock` <a id="thread.lock.shared">[[thread.lock.shared]]</a>
|
| 1700 |
|
| 1701 |
``` cpp
|
| 1702 |
namespace std {
|
| 1703 |
-
|
| 1704 |
template <class Mutex>
|
| 1705 |
class shared_lock {
|
| 1706 |
public:
|
| 1707 |
-
|
| 1708 |
|
| 1709 |
-
|
| 1710 |
shared_lock() noexcept;
|
| 1711 |
explicit shared_lock(mutex_type& m); // blocking
|
| 1712 |
shared_lock(mutex_type& m, defer_lock_t) noexcept;
|
| 1713 |
shared_lock(mutex_type& m, try_to_lock_t);
|
| 1714 |
shared_lock(mutex_type& m, adopt_lock_t);
|
|
@@ -1718,42 +1882,44 @@ public:
|
|
| 1718 |
template <class Rep, class Period>
|
| 1719 |
shared_lock(mutex_type& m,
|
| 1720 |
const chrono::duration<Rep, Period>& rel_time);
|
| 1721 |
~shared_lock();
|
| 1722 |
|
| 1723 |
-
|
| 1724 |
-
|
| 1725 |
|
| 1726 |
shared_lock(shared_lock&& u) noexcept;
|
| 1727 |
shared_lock& operator=(shared_lock&& u) noexcept;
|
| 1728 |
|
|
|
|
| 1729 |
void lock(); // blocking
|
| 1730 |
bool try_lock();
|
| 1731 |
template <class Rep, class Period>
|
| 1732 |
bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
|
| 1733 |
template <class Clock, class Duration>
|
| 1734 |
bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
|
| 1735 |
void unlock();
|
| 1736 |
|
| 1737 |
-
|
| 1738 |
void swap(shared_lock& u) noexcept;
|
| 1739 |
mutex_type* release() noexcept;
|
| 1740 |
|
| 1741 |
-
|
| 1742 |
bool owns_lock() const noexcept;
|
| 1743 |
explicit operator bool () const noexcept;
|
| 1744 |
mutex_type* mutex() const noexcept;
|
| 1745 |
|
| 1746 |
private:
|
| 1747 |
mutex_type* pm; // exposition only
|
| 1748 |
bool owns; // exposition only
|
| 1749 |
};
|
| 1750 |
|
|
|
|
|
|
|
| 1751 |
template <class Mutex>
|
| 1752 |
void swap(shared_lock<Mutex>& x, shared_lock<Mutex>& y) noexcept;
|
| 1753 |
-
|
| 1754 |
-
} // std
|
| 1755 |
```
|
| 1756 |
|
| 1757 |
An object of type `shared_lock` controls the shared ownership of a
|
| 1758 |
lockable object within a scope. Shared ownership of the lockable object
|
| 1759 |
may be acquired at construction or after construction, and may be
|
|
@@ -1763,12 +1929,12 @@ a program is undefined if the contained pointer `pm` is not null and the
|
|
| 1763 |
lockable object pointed to by `pm` does not exist for the entire
|
| 1764 |
remaining lifetime ([[basic.life]]) of the `shared_lock` object. The
|
| 1765 |
supplied `Mutex` type shall meet the shared mutex requirements (
|
| 1766 |
[[thread.sharedtimedmutex.requirements]]).
|
| 1767 |
|
| 1768 |
-
`shared_lock<Mutex>` meets the `TimedLockable`
|
| 1769 |
-
[[thread.req.lockable.timed]]).
|
| 1770 |
|
| 1771 |
##### `shared_lock` constructors, destructor, and assignment <a id="thread.lock.shared.cons">[[thread.lock.shared.cons]]</a>
|
| 1772 |
|
| 1773 |
``` cpp
|
| 1774 |
shared_lock() noexcept;
|
|
@@ -1786,19 +1952,19 @@ explicit shared_lock(mutex_type& m);
|
|
| 1786 |
mode.
|
| 1787 |
|
| 1788 |
*Effects:* Constructs an object of type `shared_lock` and calls
|
| 1789 |
`m.lock_shared()`.
|
| 1790 |
|
| 1791 |
-
*Postconditions:* `pm ==
|
| 1792 |
|
| 1793 |
``` cpp
|
| 1794 |
shared_lock(mutex_type& m, defer_lock_t) noexcept;
|
| 1795 |
```
|
| 1796 |
|
| 1797 |
*Effects:* Constructs an object of type `shared_lock`.
|
| 1798 |
|
| 1799 |
-
*Postconditions:* `pm ==
|
| 1800 |
|
| 1801 |
``` cpp
|
| 1802 |
shared_lock(mutex_type& m, try_to_lock_t);
|
| 1803 |
```
|
| 1804 |
|
|
@@ -1806,22 +1972,22 @@ shared_lock(mutex_type& m, try_to_lock_t);
|
|
| 1806 |
mode.
|
| 1807 |
|
| 1808 |
*Effects:* Constructs an object of type `shared_lock` and calls
|
| 1809 |
`m.try_lock_shared()`.
|
| 1810 |
|
| 1811 |
-
*Postconditions:* `pm ==
|
| 1812 |
-
returned by the call to `m.try_lock_shared()`.
|
| 1813 |
|
| 1814 |
``` cpp
|
| 1815 |
shared_lock(mutex_type& m, adopt_lock_t);
|
| 1816 |
```
|
| 1817 |
|
| 1818 |
*Requires:* The calling thread has shared ownership of the mutex.
|
| 1819 |
|
| 1820 |
*Effects:* Constructs an object of type `shared_lock`.
|
| 1821 |
|
| 1822 |
-
*Postconditions:* `pm ==
|
| 1823 |
|
| 1824 |
``` cpp
|
| 1825 |
template <class Clock, class Duration>
|
| 1826 |
shared_lock(mutex_type& m,
|
| 1827 |
const chrono::time_point<Clock, Duration>& abs_time);
|
|
@@ -1831,12 +1997,12 @@ template <class Clock, class Duration>
|
|
| 1831 |
mode.
|
| 1832 |
|
| 1833 |
*Effects:* Constructs an object of type `shared_lock` and calls
|
| 1834 |
`m.try_lock_shared_until(abs_time)`.
|
| 1835 |
|
| 1836 |
-
*Postconditions:* `pm ==
|
| 1837 |
-
returned by the call to `m.try_lock_shared_until(abs_time)`.
|
| 1838 |
|
| 1839 |
``` cpp
|
| 1840 |
template <class Rep, class Period>
|
| 1841 |
shared_lock(mutex_type& m,
|
| 1842 |
const chrono::duration<Rep, Period>& rel_time);
|
|
@@ -1846,12 +2012,12 @@ template <class Rep, class Period>
|
|
| 1846 |
mode.
|
| 1847 |
|
| 1848 |
*Effects:* Constructs an object of type `shared_lock` and calls
|
| 1849 |
`m.try_lock_shared_for(rel_time)`.
|
| 1850 |
|
| 1851 |
-
*Postconditions:* `pm ==
|
| 1852 |
-
returned by the call to `m.try_lock_shared_for(rel_time)`.
|
| 1853 |
|
| 1854 |
``` cpp
|
| 1855 |
~shared_lock();
|
| 1856 |
```
|
| 1857 |
|
|
@@ -1859,106 +2025,117 @@ returned by the call to `m.try_lock_shared_for(rel_time)`.
|
|
| 1859 |
|
| 1860 |
``` cpp
|
| 1861 |
shared_lock(shared_lock&& sl) noexcept;
|
| 1862 |
```
|
| 1863 |
|
| 1864 |
-
*Postconditions:* `pm ==
|
| 1865 |
is the state of `sl` just prior to this construction),
|
| 1866 |
`sl.pm == nullptr` and `sl.owns == false`.
|
| 1867 |
|
| 1868 |
``` cpp
|
| 1869 |
shared_lock& operator=(shared_lock&& sl) noexcept;
|
| 1870 |
```
|
| 1871 |
|
| 1872 |
*Effects:* If `owns` calls `pm->unlock_shared()`.
|
| 1873 |
|
| 1874 |
-
*Postconditions:* `pm ==
|
| 1875 |
is the state of `sl` just prior to this assignment), `sl.pm == nullptr`
|
| 1876 |
and `sl.owns == false`.
|
| 1877 |
|
| 1878 |
##### `shared_lock` locking <a id="thread.lock.shared.locking">[[thread.lock.shared.locking]]</a>
|
| 1879 |
|
| 1880 |
``` cpp
|
| 1881 |
void lock();
|
| 1882 |
```
|
| 1883 |
|
| 1884 |
-
*Effects:* `pm->lock_shared()`.
|
| 1885 |
|
| 1886 |
*Postconditions:* `owns == true`.
|
| 1887 |
|
| 1888 |
-
*Throws:* Any exception thrown by `pm->lock_shared()`. `system_error`
|
| 1889 |
-
an exception is required ([[thread.req.exception]]).
|
| 1890 |
-
|
| 1891 |
-
|
| 1892 |
-
|
|
|
|
|
|
|
| 1893 |
|
| 1894 |
``` cpp
|
| 1895 |
bool try_lock();
|
| 1896 |
```
|
| 1897 |
|
| 1898 |
-
*Effects:* `pm->try_lock_shared()`.
|
| 1899 |
|
| 1900 |
*Returns:* The value returned by the call to `pm->try_lock_shared()`.
|
| 1901 |
|
| 1902 |
*Postconditions:* `owns == res`, where `res` is the value returned by
|
| 1903 |
the call to `pm->try_lock_shared()`.
|
| 1904 |
|
| 1905 |
*Throws:* Any exception thrown by `pm->try_lock_shared()`.
|
| 1906 |
-
`system_error`
|
| 1907 |
-
|
| 1908 |
-
|
| 1909 |
-
|
|
|
|
|
|
|
|
|
|
| 1910 |
|
| 1911 |
``` cpp
|
| 1912 |
template <class Clock, class Duration>
|
| 1913 |
bool
|
| 1914 |
try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
|
| 1915 |
```
|
| 1916 |
|
| 1917 |
-
*Effects:* `pm->try_lock_shared_until(abs_time)`.
|
| 1918 |
|
| 1919 |
*Returns:* The value returned by the call to
|
| 1920 |
`pm->try_lock_shared_until(abs_time)`.
|
| 1921 |
|
| 1922 |
*Postconditions:* `owns == res`, where `res` is the value returned by
|
| 1923 |
the call to `pm->try_lock_shared_until(abs_time)`.
|
| 1924 |
|
| 1925 |
*Throws:* Any exception thrown by `pm->try_lock_shared_until(abs_time)`.
|
| 1926 |
-
`system_error`
|
| 1927 |
-
|
| 1928 |
-
|
| 1929 |
-
|
|
|
|
|
|
|
|
|
|
| 1930 |
|
| 1931 |
``` cpp
|
| 1932 |
template <class Rep, class Period>
|
| 1933 |
bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
|
| 1934 |
```
|
| 1935 |
|
| 1936 |
-
*Effects:* `pm->try_lock_shared_for(rel_time)`.
|
| 1937 |
|
| 1938 |
*Returns:* The value returned by the call to
|
| 1939 |
`pm->try_lock_shared_for(rel_time)`.
|
| 1940 |
|
| 1941 |
*Postconditions:* `owns == res`, where `res` is the value returned by
|
| 1942 |
the call to `pm->try_lock_shared_for(rel_time)`.
|
| 1943 |
|
| 1944 |
*Throws:* Any exception thrown by `pm->try_lock_shared_for(rel_time)`.
|
| 1945 |
-
`system_error`
|
| 1946 |
-
|
| 1947 |
-
|
| 1948 |
-
|
|
|
|
|
|
|
|
|
|
| 1949 |
|
| 1950 |
``` cpp
|
| 1951 |
void unlock();
|
| 1952 |
```
|
| 1953 |
|
| 1954 |
-
*Effects:* `pm->unlock_shared()`.
|
| 1955 |
|
| 1956 |
*Postconditions:* `owns == false`.
|
| 1957 |
|
| 1958 |
-
*Throws:* `system_error` when an exception is
|
| 1959 |
-
([[thread.req.exception]]).
|
| 1960 |
|
| 1961 |
*Error conditions:*
|
| 1962 |
|
| 1963 |
- `operation_not_permitted` — if on entry `owns` is `false`.
|
| 1964 |
|
|
@@ -1981,11 +2158,11 @@ mutex_type* release() noexcept;
|
|
| 1981 |
``` cpp
|
| 1982 |
template <class Mutex>
|
| 1983 |
void swap(shared_lock<Mutex>& x, shared_lock<Mutex>& y) noexcept;
|
| 1984 |
```
|
| 1985 |
|
| 1986 |
-
*Effects:* `x.swap(y)`.
|
| 1987 |
|
| 1988 |
##### `shared_lock` observers <a id="thread.lock.shared.obs">[[thread.lock.shared.obs]]</a>
|
| 1989 |
|
| 1990 |
``` cpp
|
| 1991 |
bool owns_lock() const noexcept;
|
|
@@ -2010,97 +2187,120 @@ mutex_type* mutex() const noexcept;
|
|
| 2010 |
``` cpp
|
| 2011 |
template <class L1, class L2, class... L3> int try_lock(L1&, L2&, L3&...);
|
| 2012 |
```
|
| 2013 |
|
| 2014 |
*Requires:* Each template parameter type shall meet the `Lockable`
|
| 2015 |
-
requirements.
|
| 2016 |
-
|
|
|
|
|
|
|
| 2017 |
|
| 2018 |
*Effects:* Calls `try_lock()` for each argument in order beginning with
|
| 2019 |
the first until all arguments have been processed or a call to
|
| 2020 |
`try_lock()` fails, either by returning `false` or by throwing an
|
| 2021 |
exception. If a call to `try_lock()` fails, `unlock()` shall be called
|
| 2022 |
for all prior arguments and there shall be no further calls to
|
| 2023 |
`try_lock()`.
|
| 2024 |
|
| 2025 |
*Returns:* `-1` if all calls to `try_lock()` returned `true`, otherwise
|
| 2026 |
-
a
|
| 2027 |
-
returned `false`.
|
| 2028 |
|
| 2029 |
``` cpp
|
| 2030 |
template <class L1, class L2, class... L3> void lock(L1&, L2&, L3&...);
|
| 2031 |
```
|
| 2032 |
|
| 2033 |
*Requires:* Each template parameter type shall meet the `Lockable`
|
| 2034 |
-
requirements,
|
| 2035 |
-
|
|
|
|
|
|
|
| 2036 |
|
| 2037 |
*Effects:* All arguments are locked via a sequence of calls to `lock()`,
|
| 2038 |
`try_lock()`, or `unlock()` on each argument. The sequence of calls
|
| 2039 |
-
shall not result in deadlock, but is otherwise unspecified.
|
| 2040 |
-
|
| 2041 |
-
|
| 2042 |
-
|
| 2043 |
-
|
| 2044 |
-
|
|
|
|
|
|
|
|
|
|
| 2045 |
|
| 2046 |
### Call once <a id="thread.once">[[thread.once]]</a>
|
| 2047 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2048 |
The class `once_flag` is an opaque data structure that `call_once` uses
|
| 2049 |
to initialize data without causing a data race or deadlock.
|
| 2050 |
|
| 2051 |
-
#### Struct `once_flag` <a id="thread.once.onceflag">[[thread.once.onceflag]]</a>
|
| 2052 |
-
|
| 2053 |
``` cpp
|
| 2054 |
constexpr once_flag() noexcept;
|
| 2055 |
```
|
| 2056 |
|
| 2057 |
*Effects:* Constructs an object of type `once_flag`.
|
| 2058 |
|
| 2059 |
*Synchronization:* The construction of a `once_flag` object is not
|
| 2060 |
synchronized.
|
| 2061 |
|
| 2062 |
-
The object’s internal state is set to indicate to an
|
| 2063 |
-
`call_once` with the object as its initial argument that
|
| 2064 |
-
been called.
|
| 2065 |
|
| 2066 |
#### Function `call_once` <a id="thread.once.callonce">[[thread.once.callonce]]</a>
|
| 2067 |
|
| 2068 |
``` cpp
|
| 2069 |
template<class Callable, class... Args>
|
| 2070 |
void call_once(once_flag& flag, Callable&& func, Args&&... args);
|
| 2071 |
```
|
| 2072 |
|
| 2073 |
-
*Requires:*
|
| 2074 |
-
|
| 2075 |
-
`
|
| 2076 |
-
(
|
|
|
|
|
|
|
|
|
|
| 2077 |
|
| 2078 |
*Effects:* An execution of `call_once` that does not call its `func` is
|
| 2079 |
a *passive* execution. An execution of `call_once` that calls its `func`
|
| 2080 |
-
is an *active* execution. An active execution shall call
|
| 2081 |
-
|
| 2082 |
-
`
|
| 2083 |
-
|
| 2084 |
-
|
| 2085 |
-
|
| 2086 |
-
|
| 2087 |
-
|
| 2088 |
-
|
| 2089 |
-
|
| 2090 |
-
|
| 2091 |
-
execution.
|
| 2092 |
|
| 2093 |
*Synchronization:* For any given `once_flag`: all active executions
|
| 2094 |
occur in a total order; completion of an active execution synchronizes
|
| 2095 |
with ([[intro.multithread]]) the start of the next one in this total
|
| 2096 |
order; and the returning execution synchronizes with the return from all
|
| 2097 |
passive executions.
|
| 2098 |
|
| 2099 |
*Throws:* `system_error` when an exception is
|
| 2100 |
required ([[thread.req.exception]]), or any exception thrown by `func`.
|
| 2101 |
|
|
|
|
|
|
|
| 2102 |
``` cpp
|
| 2103 |
// global flag, regular function
|
| 2104 |
void init();
|
| 2105 |
std::once_flag flag;
|
| 2106 |
|
|
@@ -2125,10 +2325,12 @@ class information {
|
|
| 2125 |
public:
|
| 2126 |
void verify() { std::call_once(verified, &information::verifier, *this); }
|
| 2127 |
};
|
| 2128 |
```
|
| 2129 |
|
|
|
|
|
|
|
| 2130 |
## Condition variables <a id="thread.condition">[[thread.condition]]</a>
|
| 2131 |
|
| 2132 |
Condition variables provide synchronization primitives used to block a
|
| 2133 |
thread until notified by some other thread that some condition is met or
|
| 2134 |
until a system time is reached. Class `condition_variable` provides a
|
|
@@ -2155,10 +2357,12 @@ executions are executed in a single unspecified total order consistent
|
|
| 2155 |
with the "happens before" order.
|
| 2156 |
|
| 2157 |
Condition variable construction and destruction need not be
|
| 2158 |
synchronized.
|
| 2159 |
|
|
|
|
|
|
|
| 2160 |
``` cpp
|
| 2161 |
namespace std {
|
| 2162 |
class condition_variable;
|
| 2163 |
class condition_variable_any;
|
| 2164 |
|
|
@@ -2166,10 +2370,12 @@ namespace std {
|
|
| 2166 |
|
| 2167 |
enum class cv_status { no_timeout, timeout };
|
| 2168 |
}
|
| 2169 |
```
|
| 2170 |
|
|
|
|
|
|
|
| 2171 |
``` cpp
|
| 2172 |
void notify_all_at_thread_exit(condition_variable& cond, unique_lock<mutex> lk);
|
| 2173 |
```
|
| 2174 |
|
| 2175 |
*Requires:* `lk` is locked by the calling thread and either
|
|
@@ -2177,37 +2383,38 @@ void notify_all_at_thread_exit(condition_variable& cond, unique_lock<mutex> lk);
|
|
| 2177 |
- no other thread is waiting on `cond`, or
|
| 2178 |
- `lk.mutex()` returns the same value for each of the lock arguments
|
| 2179 |
supplied by all concurrently waiting (via `wait`, `wait_for`, or
|
| 2180 |
`wait_until`) threads.
|
| 2181 |
|
| 2182 |
-
*Effects:*
|
| 2183 |
internal storage and schedules `cond` to be notified when the current
|
| 2184 |
thread exits, after all objects of thread storage duration associated
|
| 2185 |
with the current thread have been destroyed. This notification shall be
|
| 2186 |
-
as if
|
| 2187 |
|
| 2188 |
``` cpp
|
| 2189 |
lk.unlock();
|
| 2190 |
cond.notify_all();
|
| 2191 |
```
|
| 2192 |
|
| 2193 |
*Synchronization:* The implied `lk.unlock()` call is sequenced after the
|
| 2194 |
destruction of all objects with thread storage duration associated with
|
| 2195 |
the current thread.
|
| 2196 |
|
| 2197 |
-
*Note
|
| 2198 |
-
must be taken to ensure that this does not cause deadlock due to
|
| 2199 |
-
ordering issues. After calling `notify_all_at_thread_exit` it is
|
| 2200 |
recommended that the thread should be exited as soon as possible, and
|
| 2201 |
-
that no blocking or time-consuming tasks are run on that
|
|
|
|
| 2202 |
|
| 2203 |
-
*Note
|
| 2204 |
-
do not erroneously assume that the thread has finished if they
|
| 2205 |
experience spurious wakeups. This typically requires that the condition
|
| 2206 |
being waited for is satisfied while holding the lock on `lk`, and that
|
| 2207 |
this lock is not released and reacquired prior to calling
|
| 2208 |
-
`notify_all_at_thread_exit`.
|
| 2209 |
|
| 2210 |
### Class `condition_variable` <a id="thread.condition.condvar">[[thread.condition.condvar]]</a>
|
| 2211 |
|
| 2212 |
``` cpp
|
| 2213 |
namespace std {
|
|
@@ -2239,11 +2446,11 @@ namespace std {
|
|
| 2239 |
template <class Rep, class Period, class Predicate>
|
| 2240 |
bool wait_for(unique_lock<mutex>& lock,
|
| 2241 |
const chrono::duration<Rep, Period>& rel_time,
|
| 2242 |
Predicate pred);
|
| 2243 |
|
| 2244 |
-
|
| 2245 |
native_handle_type native_handle(); // See~[thread.req.native]
|
| 2246 |
};
|
| 2247 |
}
|
| 2248 |
```
|
| 2249 |
|
|
@@ -2266,19 +2473,21 @@ required ([[thread.req.exception]]).
|
|
| 2266 |
|
| 2267 |
``` cpp
|
| 2268 |
~condition_variable();
|
| 2269 |
```
|
| 2270 |
|
| 2271 |
-
There shall be no thread blocked on `*this`.
|
| 2272 |
-
|
| 2273 |
-
|
| 2274 |
-
|
| 2275 |
-
|
| 2276 |
-
|
| 2277 |
-
|
| 2278 |
-
|
| 2279 |
-
|
|
|
|
|
|
|
| 2280 |
|
| 2281 |
*Effects:* Destroys the object.
|
| 2282 |
|
| 2283 |
``` cpp
|
| 2284 |
void notify_one() noexcept;
|
|
@@ -2295,12 +2504,12 @@ void notify_all() noexcept;
|
|
| 2295 |
|
| 2296 |
``` cpp
|
| 2297 |
void wait(unique_lock<mutex>& lock);
|
| 2298 |
```
|
| 2299 |
|
| 2300 |
-
`lock.owns_lock()` is `true` and `lock.mutex()` is locked by
|
| 2301 |
-
thread, and either
|
| 2302 |
|
| 2303 |
- no other thread is waiting on this `condition_variable` object or
|
| 2304 |
- `lock.mutex()` returns the same value for each of the `lock` arguments
|
| 2305 |
supplied by all concurrently waiting (via `wait`, `wait_for`, or
|
| 2306 |
`wait_until`) threads.
|
|
@@ -2312,15 +2521,17 @@ thread, and either
|
|
| 2312 |
then returns.
|
| 2313 |
- The function will unblock when signaled by a call to `notify_one()` or
|
| 2314 |
a call to `notify_all()`, or spuriously.
|
| 2315 |
|
| 2316 |
*Remarks:* If the function fails to meet the postcondition,
|
| 2317 |
-
`
|
| 2318 |
-
happen if the re-locking of the mutex throws an exception.
|
| 2319 |
|
| 2320 |
-
|
| 2321 |
-
|
|
|
|
|
|
|
|
|
|
| 2322 |
|
| 2323 |
*Throws:* Nothing.
|
| 2324 |
|
| 2325 |
``` cpp
|
| 2326 |
template <class Predicate>
|
|
@@ -2341,27 +2552,28 @@ the calling thread, and either
|
|
| 2341 |
while (!pred())
|
| 2342 |
wait(lock);
|
| 2343 |
```
|
| 2344 |
|
| 2345 |
*Remarks:* If the function fails to meet the postcondition,
|
| 2346 |
-
`
|
| 2347 |
-
happen if the re-locking of the mutex throws an exception.
|
| 2348 |
|
| 2349 |
-
|
| 2350 |
-
|
| 2351 |
|
| 2352 |
-
*
|
| 2353 |
-
|
|
|
|
|
|
|
| 2354 |
|
| 2355 |
``` cpp
|
| 2356 |
template <class Clock, class Duration>
|
| 2357 |
cv_status wait_until(unique_lock<mutex>& lock,
|
| 2358 |
const chrono::time_point<Clock, Duration>& abs_time);
|
| 2359 |
```
|
| 2360 |
|
| 2361 |
-
`lock.owns_lock()` is `true` and `lock.mutex()` is locked by
|
| 2362 |
-
thread, and either
|
| 2363 |
|
| 2364 |
- no other thread is waiting on this `condition_variable` object or
|
| 2365 |
- `lock.mutex()` returns the same value for each of the `lock` arguments
|
| 2366 |
supplied by all concurrently waiting (via `wait`, `wait_for`, or
|
| 2367 |
`wait_until`) threads.
|
|
@@ -2377,15 +2589,17 @@ thread, and either
|
|
| 2377 |
spuriously.
|
| 2378 |
- If the function exits via an exception, `lock.lock()` shall be called
|
| 2379 |
prior to exiting the function.
|
| 2380 |
|
| 2381 |
*Remarks:* If the function fails to meet the postcondition,
|
| 2382 |
-
`
|
| 2383 |
-
happen if the re-locking of the mutex throws an exception.
|
| 2384 |
|
| 2385 |
-
|
| 2386 |
-
|
|
|
|
|
|
|
|
|
|
| 2387 |
|
| 2388 |
*Returns:* `cv_status::timeout` if the absolute
|
| 2389 |
timeout ([[thread.req.timing]]) specified by `abs_time` expired,
|
| 2390 |
otherwise `cv_status::no_timeout`.
|
| 2391 |
|
|
@@ -2395,12 +2609,12 @@ otherwise `cv_status::no_timeout`.
|
|
| 2395 |
template <class Rep, class Period>
|
| 2396 |
cv_status wait_for(unique_lock<mutex>& lock,
|
| 2397 |
const chrono::duration<Rep, Period>& rel_time);
|
| 2398 |
```
|
| 2399 |
|
| 2400 |
-
`lock.owns_lock()` is `true` and `lock.mutex()` is locked by
|
| 2401 |
-
thread, and either
|
| 2402 |
|
| 2403 |
- no other thread is waiting on this `condition_variable` object or
|
| 2404 |
- `lock.mutex()` returns the same value for each of the `lock` arguments
|
| 2405 |
supplied by all concurrently waiting (via `wait`, `wait_for`, or
|
| 2406 |
`wait_until`) threads.
|
|
@@ -2414,15 +2628,17 @@ return wait_until(lock, chrono::steady_clock::now() + rel_time);
|
|
| 2414 |
*Returns:* `cv_status::timeout` if the relative
|
| 2415 |
timeout ([[thread.req.timing]]) specified by `rel_time` expired,
|
| 2416 |
otherwise `cv_status::no_timeout`.
|
| 2417 |
|
| 2418 |
*Remarks:* If the function fails to meet the postcondition,
|
| 2419 |
-
`
|
| 2420 |
-
happen if the re-locking of the mutex throws an exception.
|
| 2421 |
|
| 2422 |
-
|
| 2423 |
-
|
|
|
|
|
|
|
|
|
|
| 2424 |
|
| 2425 |
*Throws:* Timeout-related exceptions ([[thread.req.timing]]).
|
| 2426 |
|
| 2427 |
``` cpp
|
| 2428 |
template <class Clock, class Duration, class Predicate>
|
|
@@ -2447,18 +2663,21 @@ while (!pred())
|
|
| 2447 |
return pred();
|
| 2448 |
return true;
|
| 2449 |
```
|
| 2450 |
|
| 2451 |
*Remarks:* If the function fails to meet the postcondition,
|
| 2452 |
-
`
|
| 2453 |
-
happen if the re-locking of the mutex throws an exception.
|
| 2454 |
|
| 2455 |
-
|
| 2456 |
-
|
| 2457 |
|
| 2458 |
-
|
| 2459 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2460 |
|
| 2461 |
*Throws:* Timeout-related exceptions ([[thread.req.timing]]) or any
|
| 2462 |
exception thrown by `pred`.
|
| 2463 |
|
| 2464 |
``` cpp
|
|
@@ -2466,12 +2685,12 @@ template <class Rep, class Period, class Predicate>
|
|
| 2466 |
bool wait_for(unique_lock<mutex>& lock,
|
| 2467 |
const chrono::duration<Rep, Period>& rel_time,
|
| 2468 |
Predicate pred);
|
| 2469 |
```
|
| 2470 |
|
| 2471 |
-
`lock.owns_lock()` is `true` and `lock.mutex()` is locked by
|
| 2472 |
-
thread, and either
|
| 2473 |
|
| 2474 |
- no other thread is waiting on this `condition_variable` object or
|
| 2475 |
- `lock.mutex()` returns the same value for each of the `lock` arguments
|
| 2476 |
supplied by all concurrently waiting (via `wait`, `wait_for`, or
|
| 2477 |
`wait_until`) threads.
|
|
@@ -2480,35 +2699,40 @@ thread, and either
|
|
| 2480 |
|
| 2481 |
``` cpp
|
| 2482 |
return wait_until(lock, chrono::steady_clock::now() + rel_time, std::move(pred));
|
| 2483 |
```
|
| 2484 |
|
| 2485 |
-
There is no blocking if `pred()` is initially `true`, even
|
| 2486 |
-
timeout has already expired.
|
| 2487 |
|
| 2488 |
*Remarks:* If the function fails to meet the postcondition,
|
| 2489 |
-
`
|
| 2490 |
-
happen if the re-locking of the mutex throws an exception.
|
| 2491 |
|
| 2492 |
-
|
| 2493 |
-
|
| 2494 |
|
| 2495 |
-
|
| 2496 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2497 |
|
| 2498 |
*Throws:* Timeout-related exceptions ([[thread.req.timing]]) or any
|
| 2499 |
exception thrown by `pred`.
|
| 2500 |
|
| 2501 |
### Class `condition_variable_any` <a id="thread.condition.condvarany">[[thread.condition.condvarany]]</a>
|
| 2502 |
|
| 2503 |
A `Lock` type shall meet the `BasicLockable` requirements (
|
| 2504 |
-
[[thread.req.lockable.basic]]).
|
| 2505 |
-
|
| 2506 |
-
|
|
|
|
|
|
|
| 2507 |
`condition_variable_any`, the user must ensure that any necessary
|
| 2508 |
synchronization is in place with respect to the predicate associated
|
| 2509 |
-
with the `condition_variable_any` instance.
|
| 2510 |
|
| 2511 |
``` cpp
|
| 2512 |
namespace std {
|
| 2513 |
class condition_variable_any {
|
| 2514 |
public:
|
|
@@ -2557,19 +2781,21 @@ required ([[thread.req.exception]]).
|
|
| 2557 |
|
| 2558 |
``` cpp
|
| 2559 |
~condition_variable_any();
|
| 2560 |
```
|
| 2561 |
|
| 2562 |
-
There shall be no thread blocked on `*this`.
|
| 2563 |
-
|
| 2564 |
-
|
| 2565 |
-
|
| 2566 |
-
|
| 2567 |
-
|
| 2568 |
-
|
| 2569 |
-
|
| 2570 |
-
|
|
|
|
|
|
|
| 2571 |
|
| 2572 |
*Effects:* Destroys the object.
|
| 2573 |
|
| 2574 |
``` cpp
|
| 2575 |
void notify_one() noexcept;
|
|
@@ -2587,27 +2813,29 @@ void notify_all() noexcept;
|
|
| 2587 |
``` cpp
|
| 2588 |
template <class Lock>
|
| 2589 |
void wait(Lock& lock);
|
| 2590 |
```
|
| 2591 |
|
| 2592 |
-
*Note
|
| 2593 |
unspecified whether the `Lock` is held. One can use a `Lock` type that
|
| 2594 |
-
allows to query that, such as the `unique_lock` wrapper.
|
| 2595 |
|
| 2596 |
*Effects:*
|
| 2597 |
|
| 2598 |
- Atomically calls `lock.unlock()` and blocks on `*this`.
|
| 2599 |
- When unblocked, calls `lock.lock()` (possibly blocking on the lock)
|
| 2600 |
and returns.
|
| 2601 |
- The function will unblock when signaled by a call to `notify_one()`, a
|
| 2602 |
call to `notify_all()`, or spuriously.
|
| 2603 |
|
| 2604 |
*Remarks:* If the function fails to meet the postcondition,
|
| 2605 |
-
`
|
| 2606 |
-
happen if the re-locking of the mutex throws an exception.
|
| 2607 |
|
| 2608 |
-
|
|
|
|
|
|
|
|
|
|
| 2609 |
|
| 2610 |
*Throws:* Nothing.
|
| 2611 |
|
| 2612 |
``` cpp
|
| 2613 |
template <class Lock, class Predicate>
|
|
@@ -2637,14 +2865,16 @@ template <class Lock, class Clock, class Duration>
|
|
| 2637 |
spuriously.
|
| 2638 |
- If the function exits via an exception, `lock.lock()` shall be called
|
| 2639 |
prior to exiting the function.
|
| 2640 |
|
| 2641 |
*Remarks:* If the function fails to meet the postcondition,
|
| 2642 |
-
`
|
| 2643 |
-
happen if the re-locking of the mutex throws an exception.
|
| 2644 |
|
| 2645 |
-
|
|
|
|
|
|
|
|
|
|
| 2646 |
|
| 2647 |
*Returns:* `cv_status::timeout` if the absolute
|
| 2648 |
timeout ([[thread.req.timing]]) specified by `abs_time` expired,
|
| 2649 |
otherwise `cv_status::no_timeout`.
|
| 2650 |
|
|
@@ -2664,14 +2894,16 @@ return wait_until(lock, chrono::steady_clock::now() + rel_time);
|
|
| 2664 |
*Returns:* `cv_status::timeout` if the relative
|
| 2665 |
timeout ([[thread.req.timing]]) specified by `rel_time` expired,
|
| 2666 |
otherwise `cv_status::no_timeout`.
|
| 2667 |
|
| 2668 |
*Remarks:* If the function fails to meet the postcondition,
|
| 2669 |
-
`
|
| 2670 |
-
happen if the re-locking of the mutex throws an exception.
|
| 2671 |
|
| 2672 |
-
|
|
|
|
|
|
|
|
|
|
| 2673 |
|
| 2674 |
*Throws:* Timeout-related exceptions ([[thread.req.timing]]).
|
| 2675 |
|
| 2676 |
``` cpp
|
| 2677 |
template <class Lock, class Clock, class Duration, class Predicate>
|
|
@@ -2685,15 +2917,16 @@ while (!pred())
|
|
| 2685 |
if (wait_until(lock, abs_time) == cv_status::timeout)
|
| 2686 |
return pred();
|
| 2687 |
return true;
|
| 2688 |
```
|
| 2689 |
|
| 2690 |
-
There is no blocking if `pred()` is initially `true`, or if
|
| 2691 |
-
has already expired.
|
| 2692 |
|
| 2693 |
-
The returned value indicates whether the predicate evaluates
|
| 2694 |
-
regardless of whether the timeout was
|
|
|
|
| 2695 |
|
| 2696 |
``` cpp
|
| 2697 |
template <class Lock, class Rep, class Period, class Predicate>
|
| 2698 |
bool wait_for(Lock& lock, const chrono::duration<Rep, Period>& rel_time, Predicate pred);
|
| 2699 |
```
|
|
@@ -2708,13 +2941,17 @@ return wait_until(lock, chrono::steady_clock::now() + rel_time, std::move(pred))
|
|
| 2708 |
|
| 2709 |
### Overview <a id="futures.overview">[[futures.overview]]</a>
|
| 2710 |
|
| 2711 |
[[futures]] describes components that a C++program can use to retrieve
|
| 2712 |
in one thread the result (value or exception) from a function that has
|
| 2713 |
-
run in the same thread or another thread.
|
| 2714 |
-
|
| 2715 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2716 |
|
| 2717 |
``` cpp
|
| 2718 |
namespace std {
|
| 2719 |
enum class future_errc {
|
| 2720 |
broken_promise = implementation-defined,
|
|
@@ -2759,36 +2996,37 @@ namespace std {
|
|
| 2759 |
|
| 2760 |
template <class R> class shared_future;
|
| 2761 |
template <class R> class shared_future<R&>;
|
| 2762 |
template <> class shared_future<void>;
|
| 2763 |
|
| 2764 |
-
template <class> class packaged_task; //
|
| 2765 |
template <class R, class... ArgTypes>
|
| 2766 |
class packaged_task<R(ArgTypes...)>;
|
| 2767 |
|
| 2768 |
template <class R, class... ArgTypes>
|
| 2769 |
void swap(packaged_task<R(ArgTypes...)>&, packaged_task<R(ArgTypes...)>&) noexcept;
|
| 2770 |
|
| 2771 |
template <class R, class Alloc>
|
| 2772 |
struct uses_allocator<packaged_task<R>, Alloc>;
|
| 2773 |
|
| 2774 |
template <class F, class... Args>
|
| 2775 |
-
future<
|
| 2776 |
async(F&& f, Args&&... args);
|
| 2777 |
template <class F, class... Args>
|
| 2778 |
-
future<
|
| 2779 |
async(launch policy, F&& f, Args&&... args);
|
| 2780 |
}
|
| 2781 |
```
|
| 2782 |
|
| 2783 |
The `enum` type `launch` is a bitmask type ([[bitmask.types]]) with
|
| 2784 |
-
`launch::async` and `launch::deferred`
|
| 2785 |
-
|
| 2786 |
-
|
|
|
|
| 2787 |
corresponding subset of available launch policies. Implementations can
|
| 2788 |
extend the behavior of the first overload of `async()` by adding their
|
| 2789 |
-
extensions to the launch policy under the “as if” rule.
|
| 2790 |
|
| 2791 |
The enum values of `future_errc` are distinct and not zero.
|
| 2792 |
|
| 2793 |
### Error handling <a id="futures.errors">[[futures.errors]]</a>
|
| 2794 |
|
|
@@ -2819,53 +3057,67 @@ error_condition make_error_condition(future_errc e) noexcept;
|
|
| 2819 |
|
| 2820 |
``` cpp
|
| 2821 |
namespace std {
|
| 2822 |
class future_error : public logic_error {
|
| 2823 |
public:
|
| 2824 |
-
future_error(
|
| 2825 |
|
| 2826 |
const error_code& code() const noexcept;
|
| 2827 |
const char* what() const noexcept;
|
|
|
|
|
|
|
| 2828 |
};
|
| 2829 |
}
|
| 2830 |
```
|
| 2831 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2832 |
``` cpp
|
| 2833 |
const error_code& code() const noexcept;
|
| 2834 |
```
|
| 2835 |
|
| 2836 |
-
*Returns:*
|
| 2837 |
-
constructor.
|
| 2838 |
|
| 2839 |
``` cpp
|
| 2840 |
const char* what() const noexcept;
|
| 2841 |
```
|
| 2842 |
|
| 2843 |
*Returns:* An NTBSincorporating `code().message()`.
|
| 2844 |
|
| 2845 |
### Shared state <a id="futures.state">[[futures.state]]</a>
|
| 2846 |
|
| 2847 |
-
Many of the classes introduced in this
|
| 2848 |
communicate results. This *shared state* consists of some state
|
| 2849 |
information and some (possibly not yet evaluated) *result*, which can be
|
| 2850 |
-
a (possibly void) value or an exception.
|
| 2851 |
-
defined in this clause reference such shared state.
|
| 2852 |
|
| 2853 |
-
|
| 2854 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2855 |
|
| 2856 |
An *asynchronous return object* is an object that reads results from a
|
| 2857 |
shared state. A *waiting function* of an asynchronous return object is
|
| 2858 |
one that potentially blocks to wait for the shared state to be made
|
| 2859 |
ready. If a waiting function can return before the state is made ready
|
| 2860 |
because of a timeout ([[thread.req.lockable]]), then it is a *timed
|
| 2861 |
waiting function*, otherwise it is a *non-timed waiting function*.
|
| 2862 |
|
| 2863 |
An *asynchronous provider* is an object that provides a result to a
|
| 2864 |
shared state. The result of a shared state is set by respective
|
| 2865 |
-
functions on the asynchronous provider.
|
| 2866 |
-
|
|
|
|
|
|
|
|
|
|
| 2867 |
description of those classes and functions that create such a state
|
| 2868 |
object.
|
| 2869 |
|
| 2870 |
When an asynchronous return object or an asynchronous provider is said
|
| 2871 |
to release its shared state, it means:
|
|
@@ -2912,16 +3164,18 @@ the shared state ready until the calling thread exits. The destruction
|
|
| 2912 |
of each of that thread’s objects with thread storage duration (
|
| 2913 |
[[basic.stc.thread]]) is sequenced before making that shared state
|
| 2914 |
ready.
|
| 2915 |
|
| 2916 |
Access to the result of the same shared state may conflict (
|
| 2917 |
-
[[intro.multithread]]).
|
| 2918 |
-
|
| 2919 |
-
|
| 2920 |
-
|
| 2921 |
-
|
| 2922 |
-
|
|
|
|
|
|
|
| 2923 |
|
| 2924 |
### Class template `promise` <a id="futures.promise">[[futures.promise]]</a>
|
| 2925 |
|
| 2926 |
``` cpp
|
| 2927 |
namespace std {
|
|
@@ -2946,11 +3200,10 @@ namespace std {
|
|
| 2946 |
// setting the result
|
| 2947 |
void set_value(see below);
|
| 2948 |
void set_exception(exception_ptr p);
|
| 2949 |
|
| 2950 |
// setting the result with deferred notification
|
| 2951 |
-
void set_value_at_thread_exit(const R& r);
|
| 2952 |
void set_value_at_thread_exit(see below);
|
| 2953 |
void set_exception_at_thread_exit(exception_ptr p);
|
| 2954 |
};
|
| 2955 |
template <class R>
|
| 2956 |
void swap(promise<R>& x, promise<R>& y) noexcept;
|
|
@@ -2959,12 +3212,12 @@ namespace std {
|
|
| 2959 |
}
|
| 2960 |
```
|
| 2961 |
|
| 2962 |
The implementation shall provide the template `promise` and two
|
| 2963 |
specializations, `promise<R&>` and `promise<{}void>`. These differ only
|
| 2964 |
-
in the argument type of the member
|
| 2965 |
-
|
| 2966 |
|
| 2967 |
The `set_value`, `set_exception`, `set_value_at_thread_exit`, and
|
| 2968 |
`set_exception_at_thread_exit` member functions behave as though they
|
| 2969 |
acquire a single mutex associated with the promise object while updating
|
| 2970 |
the promise object.
|
|
@@ -2992,11 +3245,11 @@ promise(promise&& rhs) noexcept;
|
|
| 2992 |
```
|
| 2993 |
|
| 2994 |
*Effects:* constructs a new `promise` object and transfers ownership of
|
| 2995 |
the shared state of `rhs` (if any) to the newly-constructed object.
|
| 2996 |
|
| 2997 |
-
`rhs` has no shared state.
|
| 2998 |
|
| 2999 |
``` cpp
|
| 3000 |
~promise();
|
| 3001 |
```
|
| 3002 |
|
|
@@ -3015,13 +3268,13 @@ promise& operator=(promise&& rhs) noexcept;
|
|
| 3015 |
void swap(promise& other) noexcept;
|
| 3016 |
```
|
| 3017 |
|
| 3018 |
*Effects:* Exchanges the shared state of `*this` and `other`.
|
| 3019 |
|
| 3020 |
-
`*this` has the shared state (if any) that `other` had
|
| 3021 |
-
to `swap`. `other` has the shared state (if any) that
|
| 3022 |
-
to the call to `swap`.
|
| 3023 |
|
| 3024 |
``` cpp
|
| 3025 |
future<R> get_future();
|
| 3026 |
```
|
| 3027 |
|
|
@@ -3042,11 +3295,11 @@ void promise::set_value(const R& r);
|
|
| 3042 |
void promise::set_value(R&& r);
|
| 3043 |
void promise<R&>::set_value(R& r);
|
| 3044 |
void promise<void>::set_value();
|
| 3045 |
```
|
| 3046 |
|
| 3047 |
-
*Effects:*
|
| 3048 |
that state ready ([[futures.state]]).
|
| 3049 |
|
| 3050 |
*Throws:*
|
| 3051 |
|
| 3052 |
- `future_error` if its shared state already has a stored value or
|
|
@@ -3064,11 +3317,13 @@ that state ready ([[futures.state]]).
|
|
| 3064 |
|
| 3065 |
``` cpp
|
| 3066 |
void set_exception(exception_ptr p);
|
| 3067 |
```
|
| 3068 |
|
| 3069 |
-
*
|
|
|
|
|
|
|
| 3070 |
state and makes that state ready ([[futures.state]]).
|
| 3071 |
|
| 3072 |
*Throws:* `future_error` if its shared state already has a stored value
|
| 3073 |
or exception.
|
| 3074 |
|
|
@@ -3107,10 +3362,12 @@ associated with the current thread have been destroyed.
|
|
| 3107 |
|
| 3108 |
``` cpp
|
| 3109 |
void set_exception_at_thread_exit(exception_ptr p);
|
| 3110 |
```
|
| 3111 |
|
|
|
|
|
|
|
| 3112 |
*Effects:* Stores the exception pointer `p` in the shared state without
|
| 3113 |
making that state ready immediately. Schedules that state to be made
|
| 3114 |
ready when the current thread exits, after all objects of thread storage
|
| 3115 |
duration associated with the current thread have been destroyed.
|
| 3116 |
|
|
@@ -3122,14 +3379,14 @@ duration associated with the current thread have been destroyed.
|
|
| 3122 |
value or exception.
|
| 3123 |
- `no_state` if `*this` has no shared state.
|
| 3124 |
|
| 3125 |
``` cpp
|
| 3126 |
template <class R>
|
| 3127 |
-
void swap(promise<R>& x, promise<R>& y);
|
| 3128 |
```
|
| 3129 |
|
| 3130 |
-
*Effects:* `x.swap(y)`.
|
| 3131 |
|
| 3132 |
### Class template `future` <a id="futures.unique_future">[[futures.unique_future]]</a>
|
| 3133 |
|
| 3134 |
The class template `future` defines a type for asynchronous return
|
| 3135 |
objects which do not share their shared state with other asynchronous
|
|
@@ -3139,18 +3396,23 @@ on asynchronous providers ([[futures.state]]) or by the move
|
|
| 3139 |
constructor and shares its shared state with the original asynchronous
|
| 3140 |
provider. The result (value or exception) of a `future` object can be
|
| 3141 |
set by calling a respective function on an object that shares the same
|
| 3142 |
shared state.
|
| 3143 |
|
| 3144 |
-
Member functions of `future` do not synchronize with
|
| 3145 |
-
member functions of `shared_future`.
|
| 3146 |
|
| 3147 |
The effect of calling any member function other than the destructor, the
|
| 3148 |
-
move-assignment operator, or `valid` on a `future` object for
|
| 3149 |
-
`valid() == false` is undefined.
|
| 3150 |
-
|
| 3151 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3152 |
|
| 3153 |
``` cpp
|
| 3154 |
namespace std {
|
| 3155 |
template <class R>
|
| 3156 |
class future {
|
|
@@ -3159,11 +3421,11 @@ namespace std {
|
|
| 3159 |
future(future&&) noexcept;
|
| 3160 |
future(const future& rhs) = delete;
|
| 3161 |
~future();
|
| 3162 |
future& operator=(const future& rhs) = delete;
|
| 3163 |
future& operator=(future&&) noexcept;
|
| 3164 |
-
shared_future<R> share();
|
| 3165 |
|
| 3166 |
// retrieving the value
|
| 3167 |
see below get();
|
| 3168 |
|
| 3169 |
// functions to check state
|
|
@@ -3185,20 +3447,20 @@ out in its description, below.
|
|
| 3185 |
|
| 3186 |
``` cpp
|
| 3187 |
future() noexcept;
|
| 3188 |
```
|
| 3189 |
|
| 3190 |
-
*Effects:*
|
| 3191 |
a shared state.
|
| 3192 |
|
| 3193 |
-
`valid() == false`.
|
| 3194 |
|
| 3195 |
``` cpp
|
| 3196 |
future(future&& rhs) noexcept;
|
| 3197 |
```
|
| 3198 |
|
| 3199 |
-
*Effects:*
|
| 3200 |
state that was originally referred to by `rhs` (if any).
|
| 3201 |
|
| 3202 |
*Postconditions:*
|
| 3203 |
|
| 3204 |
- `valid()` returns the same value as `rhs.valid()` prior to the
|
|
@@ -3209,48 +3471,51 @@ state that was originally referred to by `rhs` (if any).
|
|
| 3209 |
~future();
|
| 3210 |
```
|
| 3211 |
|
| 3212 |
*Effects:*
|
| 3213 |
|
| 3214 |
-
-
|
| 3215 |
- destroys `*this`.
|
| 3216 |
|
| 3217 |
``` cpp
|
| 3218 |
future& operator=(future&& rhs) noexcept;
|
| 3219 |
```
|
| 3220 |
|
| 3221 |
*Effects:*
|
| 3222 |
|
| 3223 |
-
-
|
| 3224 |
- move assigns the contents of `rhs` to `*this`.
|
| 3225 |
|
| 3226 |
*Postconditions:*
|
| 3227 |
|
| 3228 |
- `valid()` returns the same value as `rhs.valid()` prior to the
|
| 3229 |
assignment.
|
| 3230 |
- `rhs.valid() == false`.
|
| 3231 |
|
| 3232 |
``` cpp
|
| 3233 |
-
shared_future<R> share();
|
| 3234 |
```
|
| 3235 |
|
| 3236 |
*Returns:* `shared_future<R>(std::move(*this))`.
|
| 3237 |
|
| 3238 |
-
`valid() == false`.
|
| 3239 |
|
| 3240 |
``` cpp
|
| 3241 |
R future::get();
|
| 3242 |
R& future<R&>::get();
|
| 3243 |
void future<void>::get();
|
| 3244 |
```
|
| 3245 |
|
| 3246 |
-
*Note
|
| 3247 |
specializations differ only in the return type and return value of the
|
| 3248 |
-
member function `get`.
|
| 3249 |
|
| 3250 |
-
*Effects:*
|
| 3251 |
-
|
|
|
|
|
|
|
|
|
|
| 3252 |
|
| 3253 |
*Returns:*
|
| 3254 |
|
| 3255 |
- `future::get()` returns the value `v` stored in the object’s shared
|
| 3256 |
state as `std::move(v)`.
|
|
@@ -3259,11 +3524,11 @@ value stored in the shared state.
|
|
| 3259 |
- `future<void>::get()` returns nothing.
|
| 3260 |
|
| 3261 |
*Throws:* the stored exception, if an exception was stored in the shared
|
| 3262 |
state.
|
| 3263 |
|
| 3264 |
-
`valid() == false`.
|
| 3265 |
|
| 3266 |
``` cpp
|
| 3267 |
bool valid() const noexcept;
|
| 3268 |
```
|
| 3269 |
|
|
@@ -3271,18 +3536,18 @@ bool valid() const noexcept;
|
|
| 3271 |
|
| 3272 |
``` cpp
|
| 3273 |
void wait() const;
|
| 3274 |
```
|
| 3275 |
|
| 3276 |
-
*Effects:*
|
| 3277 |
|
| 3278 |
``` cpp
|
| 3279 |
template <class Rep, class Period>
|
| 3280 |
future_status wait_for(const chrono::duration<Rep, Period>& rel_time) const;
|
| 3281 |
```
|
| 3282 |
|
| 3283 |
-
*Effects:*
|
| 3284 |
function ([[futures.async]]), otherwise blocks until the shared state
|
| 3285 |
is ready or until the relative timeout ([[thread.req.timing]])
|
| 3286 |
specified by `rel_time` has expired.
|
| 3287 |
|
| 3288 |
*Returns:*
|
|
@@ -3299,11 +3564,11 @@ specified by `rel_time` has expired.
|
|
| 3299 |
``` cpp
|
| 3300 |
template <class Clock, class Duration>
|
| 3301 |
future_status wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
|
| 3302 |
```
|
| 3303 |
|
| 3304 |
-
*Effects:*
|
| 3305 |
function ([[futures.async]]), otherwise blocks until the shared state
|
| 3306 |
is ready or until the absolute timeout ([[thread.req.timing]])
|
| 3307 |
specified by `abs_time` has expired.
|
| 3308 |
|
| 3309 |
*Returns:*
|
|
@@ -3327,31 +3592,35 @@ can be created by conversion from a `future` object and shares its
|
|
| 3327 |
shared state with the original asynchronous provider (
|
| 3328 |
[[futures.state]]) of the shared state. The result (value or exception)
|
| 3329 |
of a `shared_future` object can be set by calling a respective function
|
| 3330 |
on an object that shares the same shared state.
|
| 3331 |
|
| 3332 |
-
Member functions of `shared_future` do not synchronize with
|
| 3333 |
-
but they synchronize with the shared
|
| 3334 |
|
| 3335 |
The effect of calling any member function other than the destructor, the
|
| 3336 |
-
move-assignment operator, or `valid()` on
|
| 3337 |
-
which `valid() ==
|
| 3338 |
-
|
| 3339 |
-
|
| 3340 |
-
`
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3341 |
|
| 3342 |
``` cpp
|
| 3343 |
namespace std {
|
| 3344 |
template <class R>
|
| 3345 |
class shared_future {
|
| 3346 |
public:
|
| 3347 |
shared_future() noexcept;
|
| 3348 |
-
shared_future(const shared_future& rhs);
|
| 3349 |
shared_future(future<R>&&) noexcept;
|
| 3350 |
shared_future(shared_future&& rhs) noexcept;
|
| 3351 |
~shared_future();
|
| 3352 |
-
shared_future& operator=(const shared_future& rhs);
|
| 3353 |
shared_future& operator=(shared_future&& rhs) noexcept;
|
| 3354 |
|
| 3355 |
// retrieving the value
|
| 3356 |
see below get() const;
|
| 3357 |
|
|
@@ -3374,30 +3643,30 @@ differ only in the return type and return value of the member function
|
|
| 3374 |
|
| 3375 |
``` cpp
|
| 3376 |
shared_future() noexcept;
|
| 3377 |
```
|
| 3378 |
|
| 3379 |
-
*Effects:*
|
| 3380 |
refer to a shared state.
|
| 3381 |
|
| 3382 |
-
`valid() == false`.
|
| 3383 |
|
| 3384 |
``` cpp
|
| 3385 |
-
shared_future(const shared_future& rhs);
|
| 3386 |
```
|
| 3387 |
|
| 3388 |
-
*Effects:*
|
| 3389 |
shared state as `rhs` (if any).
|
| 3390 |
|
| 3391 |
-
`valid()` returns the same value as `rhs.valid()`.
|
| 3392 |
|
| 3393 |
``` cpp
|
| 3394 |
shared_future(future<R>&& rhs) noexcept;
|
| 3395 |
shared_future(shared_future&& rhs) noexcept;
|
| 3396 |
```
|
| 3397 |
|
| 3398 |
-
*Effects:*
|
| 3399 |
shared state that was originally referred to by `rhs` (if any).
|
| 3400 |
|
| 3401 |
*Postconditions:*
|
| 3402 |
|
| 3403 |
- `valid()` returns the same value as `rhs.valid()` returned prior to
|
|
@@ -3408,64 +3677,67 @@ shared state that was originally referred to by `rhs` (if any).
|
|
| 3408 |
~shared_future();
|
| 3409 |
```
|
| 3410 |
|
| 3411 |
*Effects:*
|
| 3412 |
|
| 3413 |
-
-
|
| 3414 |
- destroys `*this`.
|
| 3415 |
|
| 3416 |
``` cpp
|
| 3417 |
shared_future& operator=(shared_future&& rhs) noexcept;
|
| 3418 |
```
|
| 3419 |
|
| 3420 |
*Effects:*
|
| 3421 |
|
| 3422 |
-
-
|
| 3423 |
- move assigns the contents of `rhs` to `*this`.
|
| 3424 |
|
| 3425 |
*Postconditions:*
|
| 3426 |
|
| 3427 |
- `valid()` returns the same value as `rhs.valid()` returned prior to
|
| 3428 |
the assignment.
|
| 3429 |
- `rhs.valid() == false`.
|
| 3430 |
|
| 3431 |
``` cpp
|
| 3432 |
-
shared_future& operator=(const shared_future& rhs);
|
| 3433 |
```
|
| 3434 |
|
| 3435 |
*Effects:*
|
| 3436 |
|
| 3437 |
-
-
|
| 3438 |
-
- assigns the contents of `rhs` to `*this`. As a result,
|
| 3439 |
-
to the same shared state as `rhs` (if
|
|
|
|
| 3440 |
|
| 3441 |
*Postconditions:* `valid() == rhs.valid()`.
|
| 3442 |
|
| 3443 |
``` cpp
|
| 3444 |
const R& shared_future::get() const;
|
| 3445 |
R& shared_future<R&>::get() const;
|
| 3446 |
void shared_future<void>::get() const;
|
| 3447 |
```
|
| 3448 |
|
| 3449 |
-
*Note
|
| 3450 |
specializations differ only in the return type and return value of the
|
| 3451 |
-
member function `get`.
|
| 3452 |
|
| 3453 |
-
*Note
|
| 3454 |
unsynchronized, so programmers should apply only those operations on `R`
|
| 3455 |
-
that do not introduce a data
|
|
|
|
| 3456 |
|
| 3457 |
*Effects:* `wait()`s until the shared state is ready, then retrieves the
|
| 3458 |
value stored in the shared state.
|
| 3459 |
|
| 3460 |
*Returns:*
|
| 3461 |
|
| 3462 |
- `shared_future::get()` returns a const reference to the value stored
|
| 3463 |
-
in the object’s shared state. Access through that
|
| 3464 |
-
shared state has been destroyed produces undefined
|
| 3465 |
-
be avoided by not storing the reference in any
|
| 3466 |
-
lifetime than the `shared_future` object that
|
|
|
|
| 3467 |
- `shared_future<R&>::get()` returns the reference stored as value in
|
| 3468 |
the object’s shared state.
|
| 3469 |
- `shared_future<void>::get()` returns nothing.
|
| 3470 |
|
| 3471 |
*Throws:* the stored exception, if an exception was stored in the shared
|
|
@@ -3479,18 +3751,18 @@ bool valid() const noexcept;
|
|
| 3479 |
|
| 3480 |
``` cpp
|
| 3481 |
void wait() const;
|
| 3482 |
```
|
| 3483 |
|
| 3484 |
-
*Effects:*
|
| 3485 |
|
| 3486 |
``` cpp
|
| 3487 |
template <class Rep, class Period>
|
| 3488 |
future_status wait_for(const chrono::duration<Rep, Period>& rel_time) const;
|
| 3489 |
```
|
| 3490 |
|
| 3491 |
-
*Effects:*
|
| 3492 |
function ([[futures.async]]), otherwise blocks until the shared state
|
| 3493 |
is ready or until the relative timeout ([[thread.req.timing]])
|
| 3494 |
specified by `rel_time` has expired.
|
| 3495 |
|
| 3496 |
*Returns:*
|
|
@@ -3507,11 +3779,11 @@ specified by `rel_time` has expired.
|
|
| 3507 |
``` cpp
|
| 3508 |
template <class Clock, class Duration>
|
| 3509 |
future_status wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
|
| 3510 |
```
|
| 3511 |
|
| 3512 |
-
*Effects:*
|
| 3513 |
function ([[futures.async]]), otherwise blocks until the shared state
|
| 3514 |
is ready or until the absolute timeout ([[thread.req.timing]])
|
| 3515 |
specified by `abs_time` has expired.
|
| 3516 |
|
| 3517 |
*Returns:*
|
|
@@ -3531,81 +3803,91 @@ The function template `async` provides a mechanism to launch a function
|
|
| 3531 |
potentially in a new thread and provides the result of the function in a
|
| 3532 |
`future` object with which it shares a shared state.
|
| 3533 |
|
| 3534 |
``` cpp
|
| 3535 |
template <class F, class... Args>
|
| 3536 |
-
future<
|
|
|
|
| 3537 |
template <class F, class... Args>
|
| 3538 |
-
future<
|
|
|
|
| 3539 |
```
|
| 3540 |
|
| 3541 |
*Requires:* `F` and each `Ti` in `Args` shall satisfy the
|
| 3542 |
-
`MoveConstructible` requirements
|
| 3543 |
-
|
| 3544 |
-
|
| 3545 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3546 |
|
| 3547 |
*Effects:* The first function behaves the same as a call to the second
|
| 3548 |
function with a `policy` argument of `launch::async | launch::deferred`
|
| 3549 |
and the same arguments for `F` and `Args`. The second function creates a
|
| 3550 |
shared state that is associated with the returned `future` object. The
|
| 3551 |
further behavior of the second function depends on the `policy` argument
|
| 3552 |
as follows (if more than one of these conditions applies, the
|
| 3553 |
implementation may choose any of the corresponding policies):
|
| 3554 |
|
| 3555 |
-
-
|
| 3556 |
-
*
|
| 3557 |
-
*
|
| 3558 |
[[thread.thread.constr]]) as if in a new thread of execution
|
| 3559 |
-
represented by a `thread` object with the calls to *
|
| 3560 |
being evaluated in the thread that called `async`. Any return value is
|
| 3561 |
stored as the result in the shared state. Any exception propagated
|
| 3562 |
-
from the execution of
|
| 3563 |
-
*
|
| 3564 |
-
|
| 3565 |
-
|
| 3566 |
-
|
| 3567 |
-
-
|
| 3568 |
-
*
|
| 3569 |
-
*
|
| 3570 |
-
|
| 3571 |
-
|
| 3572 |
-
|
| 3573 |
-
|
| 3574 |
-
|
| 3575 |
-
|
| 3576 |
-
|
| 3577 |
-
|
| 3578 |
-
|
| 3579 |
-
|
| 3580 |
-
|
| 3581 |
-
|
| 3582 |
-
|
| 3583 |
-
|
| 3584 |
together with other policies, such as when using a `policy` value of
|
| 3585 |
`launch::async | launch::deferred`, implementations should defer
|
| 3586 |
invocation or the selection of the policy when no more concurrency can
|
| 3587 |
-
be effectively exploited.
|
| 3588 |
- If no value is set in the launch policy, or a value is set that is
|
| 3589 |
-
neither specified in this International Standard
|
| 3590 |
-
implementation, the
|
| 3591 |
|
| 3592 |
*Returns:* An object of type
|
| 3593 |
-
`future<
|
| 3594 |
-
shared state created by this call to `async`.
|
| 3595 |
-
|
| 3596 |
-
|
| 3597 |
-
|
|
|
|
|
|
|
| 3598 |
|
| 3599 |
*Synchronization:* Regardless of the provided `policy` argument,
|
| 3600 |
|
| 3601 |
- the invocation of `async` synchronizes with ([[intro.multithread]])
|
| 3602 |
-
the invocation of `f`. This statement applies even when
|
| 3603 |
-
corresponding `future` object is moved to another
|
|
|
|
| 3604 |
- the completion of the function `f` is sequenced
|
| 3605 |
-
before ([[intro.multithread]]) the shared state is made ready.
|
| 3606 |
-
might not be called at all, so its completion might
|
|
|
|
| 3607 |
|
| 3608 |
If the implementation chooses the `launch::async` policy,
|
| 3609 |
|
| 3610 |
- a call to a waiting function on an asynchronous return object that
|
| 3611 |
shares the shared state created by this `async` call shall block until
|
|
@@ -3616,32 +3898,23 @@ If the implementation chooses the `launch::async` policy,
|
|
| 3616 |
successfully detects the ready status of the shared state or with the
|
| 3617 |
return from the last function that releases the shared state,
|
| 3618 |
whichever happens first.
|
| 3619 |
|
| 3620 |
*Throws:* `system_error` if `policy == launch::async` and the
|
| 3621 |
-
implementation is unable to start a new thread
|
|
|
|
| 3622 |
|
| 3623 |
*Error conditions:*
|
| 3624 |
|
| 3625 |
- `resource_unavailable_try_again` — if `policy == launch::async` and
|
| 3626 |
the system is unable to start a new thread.
|
| 3627 |
|
| 3628 |
-
``
|
| 3629 |
-
|
| 3630 |
-
|
| 3631 |
-
|
| 3632 |
-
|
| 3633 |
-
int tmp = work1(value);
|
| 3634 |
-
return tmp + handle.get(); // #1
|
| 3635 |
-
}
|
| 3636 |
-
```
|
| 3637 |
-
|
| 3638 |
-
Line \#1 might not result in concurrency because the `async` call uses
|
| 3639 |
-
the default policy, which may use `launch::deferred`, in which case the
|
| 3640 |
-
lambda might not be invoked until the `get()` call; in that case,
|
| 3641 |
-
`work1` and `work2` are called on the same thread and there is no
|
| 3642 |
-
concurrency.
|
| 3643 |
|
| 3644 |
### Class template `packaged_task` <a id="futures.task">[[futures.task]]</a>
|
| 3645 |
|
| 3646 |
The class template `packaged_task` defines a type for wrapping a
|
| 3647 |
function or callable object so that the return value of the function or
|
|
@@ -3652,21 +3925,19 @@ and the result (whether normal or exceptional) stored in the shared
|
|
| 3652 |
state. Any futures that share the shared state will then be able to
|
| 3653 |
access the stored result.
|
| 3654 |
|
| 3655 |
``` cpp
|
| 3656 |
namespace std {
|
| 3657 |
-
template<class> class packaged_task; //
|
| 3658 |
|
| 3659 |
template<class R, class... ArgTypes>
|
| 3660 |
class packaged_task<R(ArgTypes...)> {
|
| 3661 |
public:
|
| 3662 |
// construction and destruction
|
| 3663 |
packaged_task() noexcept;
|
| 3664 |
template <class F>
|
| 3665 |
explicit packaged_task(F&& f);
|
| 3666 |
-
template <class F, class Allocator>
|
| 3667 |
-
explicit packaged_task(allocator_arg_t, const Allocator& a, F&& f);
|
| 3668 |
~packaged_task();
|
| 3669 |
|
| 3670 |
// no copy
|
| 3671 |
packaged_task(const packaged_task&) = delete;
|
| 3672 |
packaged_task& operator=(const packaged_task&) = delete;
|
|
@@ -3698,73 +3969,73 @@ namespace std {
|
|
| 3698 |
|
| 3699 |
``` cpp
|
| 3700 |
packaged_task() noexcept;
|
| 3701 |
```
|
| 3702 |
|
| 3703 |
-
*Effects:*
|
| 3704 |
no stored task.
|
| 3705 |
|
| 3706 |
``` cpp
|
| 3707 |
template <class F>
|
| 3708 |
packaged_task(F&& f);
|
| 3709 |
-
template <class F, class Allocator>
|
| 3710 |
-
explicit packaged_task(allocator_arg_t, const Allocator& a, F&& f);
|
| 3711 |
```
|
| 3712 |
|
| 3713 |
-
*Requires:* *
|
| 3714 |
are values of the corresponding types in `ArgTypes...`, shall be a valid
|
| 3715 |
expression. Invoking a copy of `f` shall behave the same as invoking
|
| 3716 |
`f`.
|
| 3717 |
|
| 3718 |
-
*Remarks:*
|
| 3719 |
-
|
| 3720 |
-
`std::packaged_task<R(ArgTypes...)>`.
|
| 3721 |
|
| 3722 |
-
*Effects:*
|
| 3723 |
-
and initializes the object’s stored task with `std::forward<F>(f)`.
|
| 3724 |
-
constructors that take an `Allocator` argument use it to allocate memory
|
| 3725 |
-
needed to store the internal data structures.
|
| 3726 |
|
| 3727 |
-
*Throws:*
|
| 3728 |
-
|
| 3729 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3730 |
|
| 3731 |
``` cpp
|
| 3732 |
packaged_task(packaged_task&& rhs) noexcept;
|
| 3733 |
```
|
| 3734 |
|
| 3735 |
-
*Effects:*
|
| 3736 |
ownership of `rhs`’s shared state to `*this`, leaving `rhs` with no
|
| 3737 |
shared state. Moves the stored task from `rhs` to `*this`.
|
| 3738 |
|
| 3739 |
-
`rhs` has no shared state.
|
| 3740 |
|
| 3741 |
``` cpp
|
| 3742 |
packaged_task& operator=(packaged_task&& rhs) noexcept;
|
| 3743 |
```
|
| 3744 |
|
| 3745 |
*Effects:*
|
| 3746 |
|
| 3747 |
-
-
|
| 3748 |
-
- `packaged_task(std::move(rhs)).swap(*this)`.
|
| 3749 |
|
| 3750 |
``` cpp
|
| 3751 |
~packaged_task();
|
| 3752 |
```
|
| 3753 |
|
| 3754 |
-
*Effects:* Abandons any shared state
|
| 3755 |
|
| 3756 |
``` cpp
|
| 3757 |
void swap(packaged_task& other) noexcept;
|
| 3758 |
```
|
| 3759 |
|
| 3760 |
-
*Effects:*
|
| 3761 |
`other`.
|
| 3762 |
|
| 3763 |
-
`*this` has the same shared state and stored task (if
|
| 3764 |
-
prior to the call to `swap`. `other` has the same shared
|
| 3765 |
-
stored task (if any) as `*this` prior to the call to `swap`.
|
| 3766 |
|
| 3767 |
``` cpp
|
| 3768 |
bool valid() const noexcept;
|
| 3769 |
```
|
| 3770 |
|
|
@@ -3787,14 +4058,14 @@ future<R> get_future();
|
|
| 3787 |
|
| 3788 |
``` cpp
|
| 3789 |
void operator()(ArgTypes... args);
|
| 3790 |
```
|
| 3791 |
|
| 3792 |
-
*Effects:* *
|
| 3793 |
-
task of `*this` and `t1, t2, ..., tN` are the values in
|
| 3794 |
-
the task returns normally, the return value is stored as
|
| 3795 |
-
asynchronous result in the shared state of `*this`, otherwise the
|
| 3796 |
exception thrown by the task is stored. The shared state of `*this` is
|
| 3797 |
made ready, and any threads blocked in a function waiting for the shared
|
| 3798 |
state of `*this` to become ready are unblocked.
|
| 3799 |
|
| 3800 |
*Throws:* a `future_error` exception object if there is no shared state
|
|
@@ -3808,18 +4079,18 @@ or the stored task has already been invoked.
|
|
| 3808 |
|
| 3809 |
``` cpp
|
| 3810 |
void make_ready_at_thread_exit(ArgTypes... args);
|
| 3811 |
```
|
| 3812 |
|
| 3813 |
-
*Effects:* *
|
| 3814 |
-
task and `t1, t2, ..., tN` are the values in `args...`. If the
|
| 3815 |
-
returns normally, the return value is stored as the asynchronous
|
| 3816 |
-
in the shared state of `*this`, otherwise the exception thrown by
|
| 3817 |
-
task is stored. In either case, this shall be done without making
|
| 3818 |
-
state ready ([[futures.state]]) immediately. Schedules the shared
|
| 3819 |
-
to be made ready when the current thread exits, after all objects
|
| 3820 |
-
thread storage duration associated with the current thread have been
|
| 3821 |
destroyed.
|
| 3822 |
|
| 3823 |
*Throws:* `future_error` if an error condition occurs.
|
| 3824 |
|
| 3825 |
*Error conditions:*
|
|
@@ -3830,13 +4101,15 @@ destroyed.
|
|
| 3830 |
|
| 3831 |
``` cpp
|
| 3832 |
void reset();
|
| 3833 |
```
|
| 3834 |
|
| 3835 |
-
*Effects:*
|
| 3836 |
-
task stored in `*this`.
|
| 3837 |
-
|
|
|
|
|
|
|
| 3838 |
|
| 3839 |
*Throws:*
|
| 3840 |
|
| 3841 |
- `bad_alloc` if memory for the new shared state could not be allocated.
|
| 3842 |
- any exception thrown by the move constructor of the task stored in the
|
|
@@ -3849,11 +4122,11 @@ The old state is abandoned ([[futures.state]]).
|
|
| 3849 |
``` cpp
|
| 3850 |
template <class R, class... ArgTypes>
|
| 3851 |
void swap(packaged_task<R(ArgTypes...)>& x, packaged_task<R(ArgTypes...)>& y) noexcept;
|
| 3852 |
```
|
| 3853 |
|
| 3854 |
-
*Effects:* `x.swap(y)`
|
| 3855 |
|
| 3856 |
``` cpp
|
| 3857 |
template <class R, class Alloc>
|
| 3858 |
struct uses_allocator<packaged_task<R>, Alloc>
|
| 3859 |
: true_type { };
|
|
@@ -3867,12 +4140,14 @@ template <class R, class Alloc>
|
|
| 3867 |
[atomics]: atomics.md#atomics
|
| 3868 |
[basic.life]: basic.md#basic.life
|
| 3869 |
[basic.stc.thread]: basic.md#basic.stc.thread
|
| 3870 |
[bitmask.types]: library.md#bitmask.types
|
| 3871 |
[class]: class.md#class
|
|
|
|
| 3872 |
[except.terminate]: except.md#except.terminate
|
| 3873 |
[func.require]: utilities.md#func.require
|
|
|
|
| 3874 |
[futures]: #futures
|
| 3875 |
[futures.async]: #futures.async
|
| 3876 |
[futures.errors]: #futures.errors
|
| 3877 |
[futures.future_error]: #futures.future_error
|
| 3878 |
[futures.overview]: #futures.overview
|
|
@@ -3882,24 +4157,28 @@ template <class R, class Alloc>
|
|
| 3882 |
[futures.task]: #futures.task
|
| 3883 |
[futures.task.members]: #futures.task.members
|
| 3884 |
[futures.task.nonmembers]: #futures.task.nonmembers
|
| 3885 |
[futures.unique_future]: #futures.unique_future
|
| 3886 |
[intro.multithread]: intro.md#intro.multithread
|
|
|
|
| 3887 |
[res.on.data.races]: library.md#res.on.data.races
|
| 3888 |
[res.on.exception.handling]: library.md#res.on.exception.handling
|
|
|
|
| 3889 |
[syserr]: diagnostics.md#syserr
|
| 3890 |
[syserr.syserr]: diagnostics.md#syserr.syserr
|
| 3891 |
[tab:thread.lib.summary]: #tab:thread.lib.summary
|
| 3892 |
[thread]: #thread
|
| 3893 |
[thread.condition]: #thread.condition
|
| 3894 |
[thread.condition.condvar]: #thread.condition.condvar
|
| 3895 |
[thread.condition.condvarany]: #thread.condition.condvarany
|
|
|
|
| 3896 |
[thread.decaycopy]: #thread.decaycopy
|
| 3897 |
[thread.general]: #thread.general
|
| 3898 |
[thread.lock]: #thread.lock
|
| 3899 |
[thread.lock.algorithm]: #thread.lock.algorithm
|
| 3900 |
[thread.lock.guard]: #thread.lock.guard
|
|
|
|
| 3901 |
[thread.lock.shared]: #thread.lock.shared
|
| 3902 |
[thread.lock.shared.cons]: #thread.lock.shared.cons
|
| 3903 |
[thread.lock.shared.locking]: #thread.lock.shared.locking
|
| 3904 |
[thread.lock.shared.mod]: #thread.lock.shared.mod
|
| 3905 |
[thread.lock.shared.obs]: #thread.lock.shared.obs
|
|
@@ -3925,12 +4204,15 @@ template <class R, class Alloc>
|
|
| 3925 |
[thread.req.lockable.req]: #thread.req.lockable.req
|
| 3926 |
[thread.req.lockable.timed]: #thread.req.lockable.timed
|
| 3927 |
[thread.req.native]: #thread.req.native
|
| 3928 |
[thread.req.paramname]: #thread.req.paramname
|
| 3929 |
[thread.req.timing]: #thread.req.timing
|
|
|
|
|
|
|
| 3930 |
[thread.sharedtimedmutex.class]: #thread.sharedtimedmutex.class
|
| 3931 |
[thread.sharedtimedmutex.requirements]: #thread.sharedtimedmutex.requirements
|
|
|
|
| 3932 |
[thread.thread.algorithm]: #thread.thread.algorithm
|
| 3933 |
[thread.thread.assign]: #thread.thread.assign
|
| 3934 |
[thread.thread.class]: #thread.thread.class
|
| 3935 |
[thread.thread.constr]: #thread.thread.constr
|
| 3936 |
[thread.thread.destr]: #thread.thread.destr
|
|
|
|
| 36 |
a call to an operating system or other underlying API results in an
|
| 37 |
error that prevents the library function from meeting its
|
| 38 |
specifications. Failure to allocate storage shall be reported as
|
| 39 |
described in [[res.on.exception.handling]].
|
| 40 |
|
| 41 |
+
[*Example 1*: Consider a function in this clause that is specified to
|
| 42 |
+
throw exceptions of type `system_error` and specifies error conditions
|
| 43 |
+
that include `operation_not_permitted` for a thread that does not have
|
| 44 |
+
the privilege to perform the operation. Assume that, during the
|
| 45 |
+
execution of this function, an `errno` of `EPERM` is reported by a POSIX
|
| 46 |
+
API call used by the implementation. Since POSIX specifies an `errno` of
|
| 47 |
+
`EPERM` when “the caller does not have the privilege to perform the
|
| 48 |
+
operation”, the implementation maps `EPERM` to an `error_condition` of
|
| 49 |
`operation_not_permitted` ([[syserr]]) and an exception of type
|
| 50 |
+
`system_error` is thrown. — *end example*]
|
| 51 |
|
| 52 |
The `error_code` reported by such an exception’s `code()` member
|
| 53 |
function shall compare equal to one of the conditions specified in the
|
| 54 |
function’s error condition element.
|
| 55 |
|
| 56 |
### Native handles <a id="thread.req.native">[[thread.req.native]]</a>
|
| 57 |
|
| 58 |
Several classes described in this Clause have members
|
| 59 |
`native_handle_type` and `native_handle`. The presence of these members
|
| 60 |
+
and their semantics is *implementation-defined*.
|
| 61 |
+
|
| 62 |
+
[*Note 1*: These members allow implementations to provide access to
|
| 63 |
+
implementation details. Their names are specified to facilitate portable
|
| 64 |
+
compile-time detection. Actual use of these members is inherently
|
| 65 |
+
non-portable. — *end note*]
|
| 66 |
|
| 67 |
### Timing specifications <a id="thread.req.timing">[[thread.req.timing]]</a>
|
| 68 |
|
| 69 |
Several functions described in this Clause take an argument to specify a
|
| 70 |
timeout. These timeouts are specified as either a `duration` or a
|
|
|
|
| 92 |
when the clock is not adjusted during the timeout. If the clock is
|
| 93 |
adjusted to the time Cₐ during the timeout, the behavior should be as
|
| 94 |
follows:
|
| 95 |
|
| 96 |
- if Cₐ > Cₜ, the waiting function should wake as soon as possible, i.e.
|
| 97 |
+
Cₐ + Dᵢ + Dₘ, since the timeout is already satisfied. \[*Note 1*: This
|
| 98 |
specification may result in the total duration of the wait decreasing
|
| 99 |
+
when measured against a steady clock. — *end note*]
|
| 100 |
- if Cₐ <= Cₜ, the waiting function should not time out until
|
| 101 |
`Clock::now()` returns a time Cₙ >= Cₜ, i.e. waking at Cₜ + Dᵢ + Dₘ.
|
| 102 |
+
\[*Note 2*: When the clock is adjusted backwards, this specification
|
| 103 |
+
may result in the total duration of the wait increasing when measured
|
| 104 |
+
against a steady clock. When the clock is adjusted forwards, this
|
| 105 |
+
specification may result in the total duration of the wait decreasing
|
| 106 |
+
when measured against a steady clock. — *end note*]
|
| 107 |
|
| 108 |
An implementation shall return from such a timeout at any point from the
|
| 109 |
time specified above to the time it would return from a steady-clock
|
| 110 |
relative timeout on the difference between Cₜ and the time point of the
|
| 111 |
+
call to the `_until` function.
|
|
|
|
| 112 |
|
| 113 |
+
[*Note 3*: Implementations should decrease the duration of the wait
|
| 114 |
+
when the clock is adjusted forwards. — *end note*]
|
| 115 |
+
|
| 116 |
+
[*Note 4*: If the clock is not synchronized with a steady clock, e.g.,
|
| 117 |
+
a CPU time clock, these timeouts might not provide useful
|
| 118 |
+
functionality. — *end note*]
|
| 119 |
|
| 120 |
The resolution of timing provided by an implementation depends on both
|
| 121 |
operating system and hardware. The finest resolution provided by an
|
| 122 |
implementation is called the *native resolution*.
|
| 123 |
|
|
|
|
| 125 |
meet the `TrivialClock` requirements ([[time.clock.req]]).
|
| 126 |
|
| 127 |
A function that takes an argument which specifies a timeout will throw
|
| 128 |
if, during its execution, a clock, time point, or time duration throws
|
| 129 |
an exception. Such exceptions are referred to as *timeout-related
|
| 130 |
+
exceptions*.
|
| 131 |
+
|
| 132 |
+
[*Note 5*: Instantiations of clock, time point and duration types
|
| 133 |
supplied by the implementation as specified in [[time.clock]] do not
|
| 134 |
+
throw exceptions. — *end note*]
|
| 135 |
|
| 136 |
+
### Requirements for `Lockable` types <a id="thread.req.lockable">[[thread.req.lockable]]</a>
|
| 137 |
|
| 138 |
#### In general <a id="thread.req.lockable.general">[[thread.req.lockable.general]]</a>
|
| 139 |
|
| 140 |
An *execution agent* is an entity such as a thread that may perform work
|
| 141 |
+
in parallel with other execution agents.
|
| 142 |
+
|
| 143 |
+
[*Note 1*: Implementations or users may introduce other kinds of agents
|
| 144 |
+
such as processes or thread-pool tasks. — *end note*]
|
| 145 |
+
|
| 146 |
The calling agent is determined by context, e.g. the calling thread that
|
| 147 |
contains the call, and so on.
|
| 148 |
|
| 149 |
+
[*Note 2*: Some lockable objects are “agent oblivious” in that they
|
| 150 |
+
work for any execution agent model because they do not determine or
|
| 151 |
+
store the agent’s ID (e.g., an ordinary spin lock). — *end note*]
|
| 152 |
|
| 153 |
The standard library templates `unique_lock` ([[thread.lock.unique]]),
|
| 154 |
+
`shared_lock` ([[thread.lock.shared]]), `scoped_lock` (
|
| 155 |
+
[[thread.lock.scoped]]), `lock_guard` ([[thread.lock.guard]]), `lock`,
|
| 156 |
+
`try_lock` ([[thread.lock.algorithm]]), and `condition_variable_any` (
|
| 157 |
[[thread.condition.condvarany]]) all operate on user-supplied lockable
|
| 158 |
objects. The `BasicLockable` requirements, the `Lockable` requirements,
|
| 159 |
and the `TimedLockable` requirements list the requirements imposed by
|
| 160 |
these library types in order to acquire or release ownership of a `lock`
|
| 161 |
+
by a given execution agent.
|
| 162 |
+
|
| 163 |
+
[*Note 3*: The nature of any lock ownership and any synchronization it
|
| 164 |
+
may entail are not part of these requirements. — *end note*]
|
| 165 |
|
| 166 |
#### `BasicLockable` requirements <a id="thread.req.lockable.basic">[[thread.req.lockable.basic]]</a>
|
| 167 |
|
| 168 |
A type `L` meets the `BasicLockable` requirements if the following
|
| 169 |
expressions are well-formed and have the specified semantics (`m`
|
|
|
|
| 196 |
|
| 197 |
``` cpp
|
| 198 |
m.try_lock()
|
| 199 |
```
|
| 200 |
|
| 201 |
+
*Effects:* Attempts to acquire a lock for the current execution agent
|
| 202 |
without blocking. If an exception is thrown then a lock shall not have
|
| 203 |
been acquired for the current execution agent.
|
| 204 |
|
| 205 |
*Return type:* `bool`.
|
| 206 |
|
|
|
|
| 217 |
|
| 218 |
``` cpp
|
| 219 |
m.try_lock_for(rel_time)
|
| 220 |
```
|
| 221 |
|
| 222 |
+
*Effects:* Attempts to acquire a lock for the current execution agent
|
| 223 |
within the relative timeout ([[thread.req.timing]]) specified by
|
| 224 |
`rel_time`. The function shall not return within the timeout specified
|
| 225 |
by `rel_time` unless it has obtained a lock on `m` for the current
|
| 226 |
execution agent. If an exception is thrown then a lock shall not have
|
| 227 |
been acquired for the current execution agent.
|
|
|
|
| 232 |
|
| 233 |
``` cpp
|
| 234 |
m.try_lock_until(abs_time)
|
| 235 |
```
|
| 236 |
|
| 237 |
+
*Effects:* Attempts to acquire a lock for the current execution agent
|
| 238 |
before the absolute timeout ([[thread.req.timing]]) specified by
|
| 239 |
`abs_time`. The function shall not return before the timeout specified
|
| 240 |
by `abs_time` unless it has obtained a lock on `m` for the current
|
| 241 |
execution agent. If an exception is thrown then a lock shall not have
|
| 242 |
been acquired for the current execution agent.
|
|
|
|
| 245 |
|
| 246 |
*Returns:* `true` if the lock was acquired, `false` otherwise.
|
| 247 |
|
| 248 |
### `decay_copy` <a id="thread.decaycopy">[[thread.decaycopy]]</a>
|
| 249 |
|
| 250 |
+
In several places in this Clause the operation `DECAY_COPY(x)` is used.
|
| 251 |
All such uses mean call the function `decay_copy(x)` and use the result,
|
| 252 |
where `decay_copy` is defined as follows:
|
| 253 |
|
| 254 |
``` cpp
|
| 255 |
template <class T> decay_t<T> decay_copy(T&& v)
|
|
|
|
| 257 |
```
|
| 258 |
|
| 259 |
## Threads <a id="thread.threads">[[thread.threads]]</a>
|
| 260 |
|
| 261 |
[[thread.threads]] describes components that can be used to create and
|
| 262 |
+
manage threads.
|
| 263 |
+
|
| 264 |
+
[*Note 1*: These threads are intended to map one-to-one with operating
|
| 265 |
+
system threads. — *end note*]
|
| 266 |
+
|
| 267 |
+
### Header `<thread>` synopsis <a id="thread.syn">[[thread.syn]]</a>
|
| 268 |
|
| 269 |
``` cpp
|
| 270 |
namespace std {
|
| 271 |
class thread;
|
| 272 |
|
|
|
|
| 293 |
execution. That representation may be transferred to other `thread`
|
| 294 |
objects in such a way that no two `thread` objects simultaneously
|
| 295 |
represent the same thread of execution. A thread of execution is
|
| 296 |
*detached* when no `thread` object represents that thread. Objects of
|
| 297 |
class `thread` can be in a state that does not represent a thread of
|
| 298 |
+
execution.
|
| 299 |
+
|
| 300 |
+
[*Note 1*: A `thread` object does not represent a thread of execution
|
| 301 |
after default construction, after being moved from, or after a
|
| 302 |
+
successful call to `detach` or `join`. — *end note*]
|
| 303 |
|
| 304 |
``` cpp
|
| 305 |
namespace std {
|
| 306 |
class thread {
|
| 307 |
public:
|
| 308 |
// types:
|
| 309 |
class id;
|
| 310 |
+
using native_handle_type = implementation-defined; // See~[thread.req.native]
|
| 311 |
|
| 312 |
// construct/copy/destroy:
|
| 313 |
thread() noexcept;
|
| 314 |
template <class F, class... Args> explicit thread(F&& f, Args&&... args);
|
| 315 |
~thread();
|
|
|
|
| 361 |
An object of type `thread::id` provides a unique identifier for each
|
| 362 |
thread of execution and a single distinct value for all `thread` objects
|
| 363 |
that do not represent a thread of execution ([[thread.thread.class]]).
|
| 364 |
Each thread of execution has an associated `thread::id` object that is
|
| 365 |
not equal to the `thread::id` object of any other thread of execution
|
| 366 |
+
and that is not equal to the `thread::id` object of any `thread` object
|
| 367 |
+
that does not represent threads of execution.
|
| 368 |
|
| 369 |
`thread::id` shall be a trivially copyable class (Clause [[class]]).
|
| 370 |
The library may reuse the value of a `thread::id` of a terminated thread
|
| 371 |
that can no longer be joined.
|
| 372 |
|
| 373 |
+
[*Note 1*: Relational operators allow `thread::id` objects to be used
|
| 374 |
+
as keys in associative containers. — *end note*]
|
| 375 |
|
| 376 |
``` cpp
|
| 377 |
id() noexcept;
|
| 378 |
```
|
| 379 |
|
|
|
|
| 404 |
|
| 405 |
``` cpp
|
| 406 |
bool operator<=(thread::id x, thread::id y) noexcept;
|
| 407 |
```
|
| 408 |
|
| 409 |
+
*Returns:* `!(y < x)`.
|
| 410 |
|
| 411 |
``` cpp
|
| 412 |
bool operator>(thread::id x, thread::id y) noexcept;
|
| 413 |
```
|
| 414 |
|
| 415 |
+
*Returns:* `y < x`.
|
| 416 |
|
| 417 |
``` cpp
|
| 418 |
bool operator>=(thread::id x, thread::id y) noexcept;
|
| 419 |
```
|
| 420 |
|
| 421 |
+
*Returns:* `!(x < y)`.
|
| 422 |
|
| 423 |
``` cpp
|
| 424 |
template<class charT, class traits>
|
| 425 |
basic_ostream<charT, traits>&
|
| 426 |
+
operator<< (basic_ostream<charT, traits>& out, thread::id id);
|
| 427 |
```
|
| 428 |
|
| 429 |
*Effects:* Inserts an unspecified text representation of `id` into
|
| 430 |
`out`. For two objects of type `thread::id` `x` and `y`, if `x == y` the
|
| 431 |
`thread::id` objects shall have the same text representation and if
|
| 432 |
`x != y` the `thread::id` objects shall have distinct text
|
| 433 |
representations.
|
| 434 |
|
| 435 |
+
*Returns:* `out`.
|
| 436 |
|
| 437 |
``` cpp
|
| 438 |
template <> struct hash<thread::id>;
|
| 439 |
```
|
| 440 |
|
| 441 |
+
The specialization is enabled ([[unord.hash]]).
|
|
|
|
| 442 |
|
| 443 |
#### `thread` constructors <a id="thread.thread.constr">[[thread.thread.constr]]</a>
|
| 444 |
|
| 445 |
``` cpp
|
| 446 |
thread() noexcept;
|
| 447 |
```
|
| 448 |
|
| 449 |
*Effects:* Constructs a `thread` object that does not represent a thread
|
| 450 |
of execution.
|
| 451 |
|
| 452 |
+
*Postconditions:* `get_id() == id()`.
|
| 453 |
|
| 454 |
``` cpp
|
| 455 |
template <class F, class... Args> explicit thread(F&& f, Args&&... args);
|
| 456 |
```
|
| 457 |
|
| 458 |
*Requires:* `F` and each `Ti` in `Args` shall satisfy the
|
| 459 |
+
`MoveConstructible` requirements.
|
| 460 |
+
` `*`INVOKE`*`( `*`DECAY_COPY`*`( std::forward<F>(f)), `*`DECAY_COPY`*`( std::forward<Args>(args))...)` ([[func.require]])
|
| 461 |
shall be a valid expression.
|
| 462 |
|
| 463 |
*Remarks:* This constructor shall not participate in overload resolution
|
| 464 |
if `decay_t<F>` is the same type as `std::thread`.
|
| 465 |
|
| 466 |
*Effects:* Constructs an object of type `thread`. The new thread of
|
| 467 |
execution executes
|
| 468 |
+
` `*`INVOKE`*`( `*`DECAY_COPY`*`( std::forward<F>(f)), `*`DECAY_COPY`*`( std::forward<Args>(args))...)`
|
| 469 |
with the calls to *`DECAY_COPY`* being evaluated in the constructing
|
| 470 |
+
thread. Any return value from this invocation is ignored.
|
| 471 |
+
|
| 472 |
+
[*Note 1*: This implies that any exceptions not thrown from the
|
| 473 |
+
invocation of the copy of `f` will be thrown in the constructing thread,
|
| 474 |
+
not the new thread. — *end note*]
|
| 475 |
+
|
| 476 |
+
If the invocation of
|
| 477 |
+
` `*`INVOKE`*`( `*`DECAY_COPY`*`( std::forward<F>(f)), `*`DECAY_COPY`*`( std::forward<Args>(args))...)`
|
| 478 |
+
terminates with an uncaught exception, `terminate` shall be called.
|
| 479 |
|
| 480 |
*Synchronization:* The completion of the invocation of the constructor
|
| 481 |
synchronizes with the beginning of the invocation of the copy of `f`.
|
| 482 |
|
| 483 |
*Postconditions:* `get_id() != id()`. `*this` represents the newly
|
|
|
|
| 505 |
|
| 506 |
``` cpp
|
| 507 |
~thread();
|
| 508 |
```
|
| 509 |
|
| 510 |
+
If `joinable()`, calls `terminate()`. Otherwise, has no effects.
|
| 511 |
+
|
| 512 |
+
[*Note 1*: Either implicitly detaching or joining a `joinable()` thread
|
| 513 |
+
in its destructor could result in difficult to debug correctness (for
|
| 514 |
+
detach) or performance (for join) bugs encountered only when an
|
| 515 |
+
exception is thrown. Thus the programmer must ensure that the destructor
|
| 516 |
+
is never executed while the thread is still joinable. — *end note*]
|
| 517 |
|
| 518 |
#### `thread` assignment <a id="thread.thread.assign">[[thread.thread.assign]]</a>
|
| 519 |
|
| 520 |
``` cpp
|
| 521 |
thread& operator=(thread&& x) noexcept;
|
| 522 |
```
|
| 523 |
|
| 524 |
+
*Effects:* If `joinable()`, calls `terminate()`. Otherwise, assigns the
|
| 525 |
+
state of `x` to `*this` and sets `x` to a default constructed state.
|
| 526 |
|
| 527 |
*Postconditions:* `x.get_id() == id()` and `get_id()` returns the value
|
| 528 |
of `x.get_id()` prior to the assignment.
|
| 529 |
|
| 530 |
+
*Returns:* `*this`.
|
| 531 |
|
| 532 |
#### `thread` members <a id="thread.thread.member">[[thread.thread.member]]</a>
|
| 533 |
|
| 534 |
``` cpp
|
| 535 |
void swap(thread& x) noexcept;
|
|
|
|
| 539 |
|
| 540 |
``` cpp
|
| 541 |
bool joinable() const noexcept;
|
| 542 |
```
|
| 543 |
|
| 544 |
+
*Returns:* `get_id() != id()`.
|
| 545 |
|
| 546 |
``` cpp
|
| 547 |
void join();
|
| 548 |
```
|
| 549 |
|
|
|
|
|
|
|
| 550 |
*Effects:* Blocks until the thread represented by `*this` has
|
| 551 |
completed.
|
| 552 |
|
| 553 |
*Synchronization:* The completion of the thread represented by `*this`
|
| 554 |
synchronizes with ([[intro.multithread]]) the corresponding successful
|
| 555 |
+
`join()` return.
|
| 556 |
+
|
| 557 |
+
[*Note 1*: Operations on `*this` are not synchronized. — *end note*]
|
| 558 |
|
| 559 |
*Postconditions:* The thread represented by `*this` has completed.
|
| 560 |
`get_id() == id()`.
|
| 561 |
|
| 562 |
*Throws:* `system_error` when an exception is
|
| 563 |
required ([[thread.req.exception]]).
|
| 564 |
|
| 565 |
*Error conditions:*
|
| 566 |
|
| 567 |
- `resource_deadlock_would_occur` — if deadlock is detected or
|
| 568 |
+
`get_id() == this_thread::get_id()`.
|
| 569 |
- `no_such_process` — if the thread is not valid.
|
| 570 |
- `invalid_argument` — if the thread is not joinable.
|
| 571 |
|
| 572 |
``` cpp
|
| 573 |
void detach();
|
| 574 |
```
|
| 575 |
|
|
|
|
|
|
|
| 576 |
*Effects:* The thread represented by `*this` continues execution without
|
| 577 |
the calling thread blocking. When `detach()` returns, `*this` no longer
|
| 578 |
represents the possibly continuing thread of execution. When the thread
|
| 579 |
previously represented by `*this` ends execution, the implementation
|
| 580 |
shall release any owned resources.
|
| 581 |
|
| 582 |
+
*Postconditions:* `get_id() == id()`.
|
| 583 |
|
| 584 |
*Throws:* `system_error` when an exception is
|
| 585 |
required ([[thread.req.exception]]).
|
| 586 |
|
| 587 |
*Error conditions:*
|
|
|
|
| 601 |
|
| 602 |
``` cpp
|
| 603 |
unsigned hardware_concurrency() noexcept;
|
| 604 |
```
|
| 605 |
|
| 606 |
+
*Returns:* The number of hardware thread contexts.
|
| 607 |
+
|
| 608 |
+
[*Note 1*: This value should only be considered to be a
|
| 609 |
+
hint. — *end note*]
|
| 610 |
+
|
| 611 |
+
If this value is not computable or well defined an implementation should
|
| 612 |
+
return 0.
|
| 613 |
|
| 614 |
#### `thread` specialized algorithms <a id="thread.thread.algorithm">[[thread.thread.algorithm]]</a>
|
| 615 |
|
| 616 |
``` cpp
|
| 617 |
void swap(thread& x, thread& y) noexcept;
|
| 618 |
```
|
| 619 |
|
| 620 |
+
*Effects:* As if by `x.swap(y)`.
|
| 621 |
|
| 622 |
### Namespace `this_thread` <a id="thread.thread.this">[[thread.thread.this]]</a>
|
| 623 |
|
| 624 |
``` cpp
|
| 625 |
+
namespace std::this_thread {
|
|
|
|
| 626 |
thread::id get_id() noexcept;
|
| 627 |
|
| 628 |
void yield() noexcept;
|
| 629 |
template <class Clock, class Duration>
|
| 630 |
void sleep_until(const chrono::time_point<Clock, Duration>& abs_time);
|
| 631 |
template <class Rep, class Period>
|
| 632 |
void sleep_for(const chrono::duration<Rep, Period>& rel_time);
|
| 633 |
}
|
|
|
|
| 634 |
```
|
| 635 |
|
| 636 |
``` cpp
|
| 637 |
thread::id this_thread::get_id() noexcept;
|
| 638 |
```
|
|
|
|
| 679 |
|
| 680 |
This section provides mechanisms for mutual exclusion: mutexes, locks,
|
| 681 |
and call once. These mechanisms ease the production of race-free
|
| 682 |
programs ([[intro.multithread]]).
|
| 683 |
|
| 684 |
+
### Header `<mutex>` synopsis <a id="mutex.syn">[[mutex.syn]]</a>
|
| 685 |
+
|
| 686 |
``` cpp
|
| 687 |
namespace std {
|
| 688 |
class mutex;
|
| 689 |
class recursive_mutex;
|
| 690 |
class timed_mutex;
|
| 691 |
class recursive_timed_mutex;
|
| 692 |
|
| 693 |
+
struct defer_lock_t { explicit defer_lock_t() = default; };
|
| 694 |
+
struct try_to_lock_t { explicit try_to_lock_t() = default; };
|
| 695 |
+
struct adopt_lock_t { explicit adopt_lock_t() = default; };
|
| 696 |
|
| 697 |
+
inline constexpr defer_lock_t defer_lock { };
|
| 698 |
+
inline constexpr try_to_lock_t try_to_lock { };
|
| 699 |
+
inline constexpr adopt_lock_t adopt_lock { };
|
| 700 |
|
| 701 |
template <class Mutex> class lock_guard;
|
| 702 |
+
template <class... MutexTypes> class scoped_lock;
|
| 703 |
template <class Mutex> class unique_lock;
|
| 704 |
|
| 705 |
template <class Mutex>
|
| 706 |
void swap(unique_lock<Mutex>& x, unique_lock<Mutex>& y) noexcept;
|
| 707 |
|
| 708 |
template <class L1, class L2, class... L3> int try_lock(L1&, L2&, L3&...);
|
| 709 |
template <class L1, class L2, class... L3> void lock(L1&, L2&, L3&...);
|
| 710 |
|
| 711 |
+
struct once_flag;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 712 |
|
| 713 |
template<class Callable, class... Args>
|
| 714 |
void call_once(once_flag& flag, Callable&& func, Args&&... args);
|
| 715 |
}
|
| 716 |
```
|
| 717 |
|
| 718 |
+
### Header `<shared_mutex>` synopsis <a id="shared_mutex.syn">[[shared_mutex.syn]]</a>
|
| 719 |
+
|
| 720 |
``` cpp
|
| 721 |
namespace std {
|
| 722 |
+
class shared_mutex;
|
| 723 |
class shared_timed_mutex;
|
| 724 |
template <class Mutex> class shared_lock;
|
| 725 |
template <class Mutex>
|
| 726 |
void swap(shared_lock<Mutex>& x, shared_lock<Mutex>& y) noexcept;
|
| 727 |
}
|
|
|
|
| 739 |
simultaneous ownership to one or many execution agents. Both recursive
|
| 740 |
and non-recursive mutexes are supplied.
|
| 741 |
|
| 742 |
#### Mutex types <a id="thread.mutex.requirements.mutex">[[thread.mutex.requirements.mutex]]</a>
|
| 743 |
|
| 744 |
+
The *mutex types* are the standard library types `mutex`,
|
| 745 |
+
`recursive_mutex`, `timed_mutex`, `recursive_timed_mutex`,
|
| 746 |
+
`shared_mutex`, and `shared_timed_mutex`. They shall meet the
|
| 747 |
+
requirements set out in this section. In this description, `m` denotes
|
| 748 |
+
an object of a mutex type.
|
| 749 |
|
| 750 |
The mutex types shall meet the `Lockable` requirements (
|
| 751 |
[[thread.req.lockable.req]]).
|
| 752 |
|
| 753 |
The mutex types shall be `DefaultConstructible` and `Destructible`. If
|
|
|
|
| 760 |
|
| 761 |
- `resource_unavailable_try_again` — if any native handle type
|
| 762 |
manipulated is not available.
|
| 763 |
- `operation_not_permitted` — if the thread does not have the privilege
|
| 764 |
to perform the operation.
|
|
|
|
|
|
|
| 765 |
- `invalid_argument` — if any native handle type manipulated as part of
|
| 766 |
mutex construction is incorrect.
|
| 767 |
|
| 768 |
The implementation shall provide lock and unlock operations, as
|
| 769 |
described below. For purposes of determining the existence of a data
|
| 770 |
race, these behave as atomic operations ([[intro.multithread]]). The
|
| 771 |
lock and unlock operations on a single mutex shall appear to occur in a
|
| 772 |
+
single total order.
|
| 773 |
+
|
| 774 |
+
[*Note 1*: This can be viewed as the modification order (
|
| 775 |
+
[[intro.multithread]]) of the mutex. — *end note*]
|
| 776 |
+
|
| 777 |
+
[*Note 2*: Construction and destruction of an object of a mutex type
|
| 778 |
+
need not be thread-safe; other synchronization should be used to ensure
|
| 779 |
+
that mutex objects are initialized and visible to other
|
| 780 |
+
threads. — *end note*]
|
| 781 |
|
| 782 |
The expression `m.lock()` shall be well-formed and have the following
|
| 783 |
semantics:
|
| 784 |
|
| 785 |
+
*Requires:* If `m` is of type `mutex`, `timed_mutex`, `shared_mutex`, or
|
| 786 |
+
`shared_timed_mutex`, the calling thread does not own the mutex.
|
| 787 |
|
| 788 |
*Effects:* Blocks the calling thread until ownership of the mutex can be
|
| 789 |
obtained for the calling thread.
|
| 790 |
|
| 791 |
+
*Postconditions:* The calling thread owns the mutex.
|
| 792 |
|
| 793 |
+
*Return type:* `void`.
|
| 794 |
|
| 795 |
*Synchronization:* Prior `unlock()` operations on the same object shall
|
| 796 |
*synchronize with* ([[intro.multithread]]) this operation.
|
| 797 |
|
| 798 |
*Throws:* `system_error` when an exception is
|
|
|
|
| 802 |
|
| 803 |
- `operation_not_permitted` — if the thread does not have the privilege
|
| 804 |
to perform the operation.
|
| 805 |
- `resource_deadlock_would_occur` — if the implementation detects that a
|
| 806 |
deadlock would occur.
|
|
|
|
|
|
|
| 807 |
|
| 808 |
The expression `m.try_lock()` shall be well-formed and have the
|
| 809 |
following semantics:
|
| 810 |
|
| 811 |
+
*Requires:* If `m` is of type `mutex`, `timed_mutex`, `shared_mutex`, or
|
| 812 |
+
`shared_timed_mutex`, the calling thread does not own the mutex.
|
| 813 |
|
| 814 |
*Effects:* Attempts to obtain ownership of the mutex for the calling
|
| 815 |
thread without blocking. If ownership is not obtained, there is no
|
| 816 |
effect and `try_lock()` immediately returns. An implementation may fail
|
| 817 |
+
to obtain the lock even if it is not held by any other thread.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 818 |
|
| 819 |
+
[*Note 1*: This spurious failure is normally uncommon, but allows
|
| 820 |
+
interesting implementations based on a simple compare and exchange
|
| 821 |
+
(Clause [[atomics]]). — *end note*]
|
| 822 |
+
|
| 823 |
+
An implementation should ensure that `try_lock()` does not consistently
|
| 824 |
+
return `false` in the absence of contending mutex acquisitions.
|
| 825 |
+
|
| 826 |
+
*Return type:* `bool`.
|
| 827 |
|
| 828 |
*Returns:* `true` if ownership of the mutex was obtained for the calling
|
| 829 |
thread, otherwise `false`.
|
| 830 |
|
| 831 |
*Synchronization:* If `try_lock()` returns `true`, prior `unlock()`
|
| 832 |
operations on the same object *synchronize
|
| 833 |
+
with* ([[intro.multithread]]) this operation.
|
| 834 |
+
|
| 835 |
+
[*Note 2*: Since `lock()` does not synchronize with a failed subsequent
|
| 836 |
+
`try_lock()`, the visibility rules are weak enough that little would be
|
| 837 |
+
known about the state after a failure, even in the absence of spurious
|
| 838 |
+
failures. — *end note*]
|
| 839 |
|
| 840 |
*Throws:* Nothing.
|
| 841 |
|
| 842 |
The expression `m.unlock()` shall be well-formed and have the following
|
| 843 |
semantics:
|
| 844 |
|
| 845 |
+
*Requires:* The calling thread shall own the mutex.
|
| 846 |
|
| 847 |
*Effects:* Releases the calling thread’s ownership of the mutex.
|
| 848 |
|
| 849 |
+
*Return type:* `void`.
|
| 850 |
|
| 851 |
*Synchronization:* This operation synchronizes
|
| 852 |
with ([[intro.multithread]]) subsequent lock operations that obtain
|
| 853 |
ownership on the same object.
|
| 854 |
|
|
|
|
| 868 |
|
| 869 |
void lock();
|
| 870 |
bool try_lock();
|
| 871 |
void unlock();
|
| 872 |
|
| 873 |
+
using native_handle_type = implementation-defined; // See~[thread.req.native]
|
| 874 |
native_handle_type native_handle(); // See~[thread.req.native]
|
| 875 |
};
|
| 876 |
}
|
| 877 |
```
|
| 878 |
|
|
|
|
| 880 |
ownership semantics. If one thread owns a mutex object, attempts by
|
| 881 |
another thread to acquire ownership of that object will fail (for
|
| 882 |
`try_lock()`) or block (for `lock()`) until the owning thread has
|
| 883 |
released ownership with a call to `unlock()`.
|
| 884 |
|
| 885 |
+
[*Note 3*: After a thread `A` has called `unlock()`, releasing a mutex,
|
| 886 |
+
it is possible for another thread `B` to lock the same mutex, observe
|
| 887 |
+
that it is no longer in use, unlock it, and destroy it, before thread
|
| 888 |
+
`A` appears to have returned from its unlock call. Implementations are
|
| 889 |
required to handle such scenarios correctly, as long as thread `A`
|
| 890 |
doesn’t access the mutex after the unlock call returns. These cases
|
| 891 |
typically occur when a reference-counted object contains a mutex that is
|
| 892 |
+
used to protect the reference count. — *end note*]
|
| 893 |
|
| 894 |
+
The class `mutex` shall satisfy all of the mutex requirements (
|
| 895 |
[[thread.mutex.requirements]]). It shall be a standard-layout class
|
| 896 |
(Clause [[class]]).
|
| 897 |
|
| 898 |
+
[*Note 4*: A program may deadlock if the thread that owns a `mutex`
|
| 899 |
+
object calls `lock()` on that object. If the implementation can detect
|
| 900 |
+
the deadlock, a `resource_deadlock_would_occur` error condition may be
|
| 901 |
+
observed. — *end note*]
|
| 902 |
|
| 903 |
The behavior of a program is undefined if it destroys a `mutex` object
|
| 904 |
owned by any thread or a thread terminates while owning a `mutex`
|
| 905 |
object.
|
| 906 |
|
|
|
|
| 918 |
|
| 919 |
void lock();
|
| 920 |
bool try_lock() noexcept;
|
| 921 |
void unlock();
|
| 922 |
|
| 923 |
+
using native_handle_type = implementation-defined; // See~[thread.req.native]
|
| 924 |
native_handle_type native_handle(); // See~[thread.req.native]
|
| 925 |
};
|
| 926 |
}
|
| 927 |
```
|
| 928 |
|
|
|
|
| 930 |
ownership semantics. If one thread owns a `recursive_mutex` object,
|
| 931 |
attempts by another thread to acquire ownership of that object will fail
|
| 932 |
(for `try_lock()`) or block (for `lock()`) until the first thread has
|
| 933 |
completely released ownership.
|
| 934 |
|
| 935 |
+
The class `recursive_mutex` shall satisfy all of the mutex
|
| 936 |
+
requirements ([[thread.mutex.requirements]]). It shall be a
|
| 937 |
+
standard-layout class (Clause [[class]]).
|
| 938 |
|
| 939 |
A thread that owns a `recursive_mutex` object may acquire additional
|
| 940 |
levels of ownership by calling `lock()` or `try_lock()` on that object.
|
| 941 |
It is unspecified how many levels of ownership may be acquired by a
|
| 942 |
single thread. If a thread has already acquired the maximum level of
|
|
|
|
| 952 |
- it destroys a `recursive_mutex` object owned by any thread or
|
| 953 |
- a thread terminates while owning a `recursive_mutex` object.
|
| 954 |
|
| 955 |
#### Timed mutex types <a id="thread.timedmutex.requirements">[[thread.timedmutex.requirements]]</a>
|
| 956 |
|
| 957 |
+
The *timed mutex types* are the standard library types `timed_mutex`,
|
| 958 |
+
`recursive_timed_mutex`, and `shared_timed_mutex`. They shall meet the
|
| 959 |
+
requirements set out below. In this description, `m` denotes an object
|
| 960 |
+
of a mutex type, `rel_time` denotes an object of an instantiation of
|
| 961 |
+
`duration` ([[time.duration]]), and `abs_time` denotes an object of an
|
| 962 |
+
instantiation of `time_point` ([[time.point]]).
|
|
|
|
| 963 |
|
| 964 |
The timed mutex types shall meet the `TimedLockable` requirements (
|
| 965 |
[[thread.req.lockable.timed]]).
|
| 966 |
|
| 967 |
The expression `m.try_lock_for(rel_time)` shall be well-formed and have
|
| 968 |
the following semantics:
|
| 969 |
|
| 970 |
+
*Requires:* If `m` is of type `timed_mutex` or `shared_timed_mutex`, the
|
| 971 |
calling thread does not own the mutex.
|
| 972 |
|
| 973 |
*Effects:* The function attempts to obtain ownership of the mutex within
|
| 974 |
the relative timeout ([[thread.req.timing]]) specified by `rel_time`.
|
| 975 |
If the time specified by `rel_time` is less than or equal to
|
| 976 |
`rel_time.zero()`, the function attempts to obtain ownership without
|
| 977 |
blocking (as if by calling `try_lock()`). The function shall return
|
| 978 |
within the timeout specified by `rel_time` only if it has obtained
|
| 979 |
+
ownership of the mutex object.
|
|
|
|
|
|
|
| 980 |
|
| 981 |
+
[*Note 1*: As with `try_lock()`, there is no guarantee that ownership
|
| 982 |
+
will be obtained if the lock is available, but implementations are
|
| 983 |
+
expected to make a strong effort to do so. — *end note*]
|
| 984 |
+
|
| 985 |
+
*Return type:* `bool`.
|
| 986 |
|
| 987 |
*Returns:* `true` if ownership was obtained, otherwise `false`.
|
| 988 |
|
| 989 |
*Synchronization:* If `try_lock_for()` returns `true`, prior `unlock()`
|
| 990 |
operations on the same object *synchronize
|
|
|
|
| 993 |
*Throws:* Timeout-related exceptions ([[thread.req.timing]]).
|
| 994 |
|
| 995 |
The expression `m.try_lock_until(abs_time)` shall be well-formed and
|
| 996 |
have the following semantics:
|
| 997 |
|
| 998 |
+
*Requires:* If `m` is of type `timed_mutex` or `shared_timed_mutex`, the
|
| 999 |
+
calling thread does not own the mutex.
|
| 1000 |
|
| 1001 |
*Effects:* The function attempts to obtain ownership of the mutex. If
|
| 1002 |
`abs_time` has already passed, the function attempts to obtain ownership
|
| 1003 |
without blocking (as if by calling `try_lock()`). The function shall
|
| 1004 |
return before the absolute timeout ([[thread.req.timing]]) specified by
|
| 1005 |
+
`abs_time` only if it has obtained ownership of the mutex object.
|
|
|
|
|
|
|
|
|
|
| 1006 |
|
| 1007 |
+
[*Note 2*: As with `try_lock()`, there is no guarantee that ownership
|
| 1008 |
+
will be obtained if the lock is available, but implementations are
|
| 1009 |
+
expected to make a strong effort to do so. — *end note*]
|
| 1010 |
+
|
| 1011 |
+
*Return type:* `bool`.
|
| 1012 |
|
| 1013 |
*Returns:* `true` if ownership was obtained, otherwise `false`.
|
| 1014 |
|
| 1015 |
*Synchronization:* If `try_lock_until()` returns `true`, prior
|
| 1016 |
`unlock()` operations on the same object *synchronize
|
|
|
|
| 1036 |
bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
|
| 1037 |
template <class Clock, class Duration>
|
| 1038 |
bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
|
| 1039 |
void unlock();
|
| 1040 |
|
| 1041 |
+
using native_handle_type = implementation-defined; // See~[thread.req.native]
|
| 1042 |
native_handle_type native_handle(); // See~[thread.req.native]
|
| 1043 |
};
|
| 1044 |
}
|
| 1045 |
```
|
| 1046 |
|
|
|
|
| 1050 |
`try_lock()`) or block (for `lock()`, `try_lock_for()`, and
|
| 1051 |
`try_lock_until()`) until the owning thread has released ownership with
|
| 1052 |
a call to `unlock()` or the call to `try_lock_for()` or
|
| 1053 |
`try_lock_until()` times out (having failed to obtain ownership).
|
| 1054 |
|
| 1055 |
+
The class `timed_mutex` shall satisfy all of the timed mutex
|
| 1056 |
requirements ([[thread.timedmutex.requirements]]). It shall be a
|
| 1057 |
standard-layout class (Clause [[class]]).
|
| 1058 |
|
| 1059 |
The behavior of a program is undefined if:
|
| 1060 |
|
|
|
|
| 1082 |
bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
|
| 1083 |
template <class Clock, class Duration>
|
| 1084 |
bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
|
| 1085 |
void unlock();
|
| 1086 |
|
| 1087 |
+
using native_handle_type = implementation-defined; // See~[thread.req.native]
|
| 1088 |
native_handle_type native_handle(); // See~[thread.req.native]
|
| 1089 |
};
|
| 1090 |
}
|
| 1091 |
```
|
| 1092 |
|
|
|
|
| 1096 |
ownership of that object will fail (for `try_lock()`) or block (for
|
| 1097 |
`lock()`, `try_lock_for()`, and `try_lock_until()`) until the owning
|
| 1098 |
thread has completely released ownership or the call to `try_lock_for()`
|
| 1099 |
or `try_lock_until()` times out (having failed to obtain ownership).
|
| 1100 |
|
| 1101 |
+
The class `recursive_timed_mutex` shall satisfy all of the timed mutex
|
| 1102 |
requirements ([[thread.timedmutex.requirements]]). It shall be a
|
| 1103 |
standard-layout class (Clause [[class]]).
|
| 1104 |
|
| 1105 |
A thread that owns a `recursive_timed_mutex` object may acquire
|
| 1106 |
additional levels of ownership by calling `lock()`, `try_lock()`,
|
|
|
|
| 1118 |
The behavior of a program is undefined if:
|
| 1119 |
|
| 1120 |
- it destroys a `recursive_timed_mutex` object owned by any thread, or
|
| 1121 |
- a thread terminates while owning a `recursive_timed_mutex` object.
|
| 1122 |
|
| 1123 |
+
#### Shared mutex types <a id="thread.sharedmutex.requirements">[[thread.sharedmutex.requirements]]</a>
|
| 1124 |
|
| 1125 |
+
The standard library types `shared_mutex` and `shared_timed_mutex` are
|
| 1126 |
+
*shared mutex types*. Shared mutex types shall meet the requirements of
|
| 1127 |
+
mutex types ([[thread.mutex.requirements.mutex]]), and additionally
|
| 1128 |
+
shall meet the requirements set out below. In this description, `m`
|
| 1129 |
+
denotes an object of a shared mutex type.
|
|
|
|
|
|
|
|
|
|
| 1130 |
|
| 1131 |
In addition to the exclusive lock ownership mode specified in
|
| 1132 |
[[thread.mutex.requirements.mutex]], shared mutex types provide a
|
| 1133 |
*shared lock* ownership mode. Multiple execution agents can
|
| 1134 |
simultaneously hold a shared lock ownership of a shared mutex type. But
|
|
|
|
| 1148 |
|
| 1149 |
*Effects:* Blocks the calling thread until shared ownership of the mutex
|
| 1150 |
can be obtained for the calling thread. If an exception is thrown then a
|
| 1151 |
shared lock shall not have been acquired for the current thread.
|
| 1152 |
|
| 1153 |
+
*Postconditions:* The calling thread has a shared lock on the mutex.
|
| 1154 |
|
| 1155 |
*Return type:* `void`.
|
| 1156 |
|
| 1157 |
*Synchronization:* Prior `unlock()` operations on the same object shall
|
| 1158 |
synchronize with ([[intro.multithread]]) this operation.
|
| 1159 |
|
| 1160 |
+
*Throws:* `system_error` when an exception is
|
| 1161 |
+
required ([[thread.req.exception]]).
|
| 1162 |
|
| 1163 |
*Error conditions:*
|
| 1164 |
|
| 1165 |
- `operation_not_permitted` — if the thread does not have the privilege
|
| 1166 |
to perform the operation.
|
| 1167 |
- `resource_deadlock_would_occur` — if the implementation detects that a
|
| 1168 |
deadlock would occur.
|
|
|
|
|
|
|
| 1169 |
|
| 1170 |
The expression `m.unlock_shared()` shall be well-formed and have the
|
| 1171 |
following semantics:
|
| 1172 |
|
| 1173 |
*Requires:* The calling thread shall hold a shared lock on the mutex.
|
|
|
|
| 1198 |
|
| 1199 |
*Returns:* `true` if the shared ownership lock was acquired, `false`
|
| 1200 |
otherwise.
|
| 1201 |
|
| 1202 |
*Synchronization:* If `try_lock_shared()` returns `true`, prior
|
| 1203 |
+
`unlock()` operations on the same object synchronize
|
| 1204 |
+
with ([[intro.multithread]]) this operation.
|
| 1205 |
|
| 1206 |
*Throws:* Nothing.
|
| 1207 |
|
| 1208 |
+
##### Class shared_mutex <a id="thread.sharedmutex.class">[[thread.sharedmutex.class]]</a>
|
| 1209 |
+
|
| 1210 |
+
``` cpp
|
| 1211 |
+
namespace std {
|
| 1212 |
+
class shared_mutex {
|
| 1213 |
+
public:
|
| 1214 |
+
shared_mutex();
|
| 1215 |
+
~shared_mutex();
|
| 1216 |
+
|
| 1217 |
+
shared_mutex(const shared_mutex&) = delete;
|
| 1218 |
+
shared_mutex& operator=(const shared_mutex&) = delete;
|
| 1219 |
+
|
| 1220 |
+
// Exclusive ownership
|
| 1221 |
+
void lock(); // blocking
|
| 1222 |
+
bool try_lock();
|
| 1223 |
+
void unlock();
|
| 1224 |
+
|
| 1225 |
+
// Shared ownership
|
| 1226 |
+
void lock_shared(); // blocking
|
| 1227 |
+
bool try_lock_shared();
|
| 1228 |
+
void unlock_shared();
|
| 1229 |
+
|
| 1230 |
+
using native_handle_type = implementation-defined; // See~[thread.req.native]
|
| 1231 |
+
native_handle_type native_handle(); // See~[thread.req.native]
|
| 1232 |
+
};
|
| 1233 |
+
}
|
| 1234 |
+
```
|
| 1235 |
+
|
| 1236 |
+
The class `shared_mutex` provides a non-recursive mutex with shared
|
| 1237 |
+
ownership semantics.
|
| 1238 |
+
|
| 1239 |
+
The class `shared_mutex` shall satisfy all of the shared mutex
|
| 1240 |
+
requirements ([[thread.sharedmutex.requirements]]). It shall be a
|
| 1241 |
+
standard-layout class (Clause [[class]]).
|
| 1242 |
+
|
| 1243 |
+
The behavior of a program is undefined if:
|
| 1244 |
+
|
| 1245 |
+
- it destroys a `shared_mutex` object owned by any thread,
|
| 1246 |
+
- a thread attempts to recursively gain any ownership of a
|
| 1247 |
+
`shared_mutex`, or
|
| 1248 |
+
- a thread terminates while possessing any ownership of a
|
| 1249 |
+
`shared_mutex`.
|
| 1250 |
+
|
| 1251 |
+
`shared_mutex` may be a synonym for `shared_timed_mutex`.
|
| 1252 |
+
|
| 1253 |
+
#### Shared timed mutex types <a id="thread.sharedtimedmutex.requirements">[[thread.sharedtimedmutex.requirements]]</a>
|
| 1254 |
+
|
| 1255 |
+
The standard library type `shared_timed_mutex` is a *shared timed mutex
|
| 1256 |
+
type*. Shared timed mutex types shall meet the requirements of timed
|
| 1257 |
+
mutex types ([[thread.timedmutex.requirements]]), shared mutex types (
|
| 1258 |
+
[[thread.sharedmutex.requirements]]), and additionally shall meet the
|
| 1259 |
+
requirements set out below. In this description, `m` denotes an object
|
| 1260 |
+
of a shared timed mutex type, `rel_type` denotes an object of an
|
| 1261 |
+
instantiation of `duration` ([[time.duration]]), and `abs_time` denotes
|
| 1262 |
+
an object of an instantiation of `time_point` ([[time.point]]).
|
| 1263 |
+
|
| 1264 |
The expression `m.try_lock_shared_for(rel_time)` shall be well-formed
|
| 1265 |
and have the following semantics:
|
| 1266 |
|
| 1267 |
*Requires:* The calling thread has no ownership of the mutex.
|
| 1268 |
|
|
|
|
| 1270 |
thread within the relative timeout ([[thread.req.timing]]) specified by
|
| 1271 |
`rel_time`. If the time specified by `rel_time` is less than or equal to
|
| 1272 |
`rel_time.zero()`, the function attempts to obtain ownership without
|
| 1273 |
blocking (as if by calling `try_lock_shared()`). The function shall
|
| 1274 |
return within the timeout specified by `rel_time` only if it has
|
| 1275 |
+
obtained shared ownership of the mutex object.
|
| 1276 |
+
|
| 1277 |
+
[*Note 1*: As with `try_lock()`, there is no guarantee that ownership
|
| 1278 |
+
will be obtained if the lock is available, but implementations are
|
| 1279 |
+
expected to make a strong effort to do so. — *end note*]
|
| 1280 |
+
|
| 1281 |
+
If an exception is thrown then a shared lock shall not have been
|
| 1282 |
acquired for the current thread.
|
| 1283 |
|
| 1284 |
*Return type:* `bool`.
|
| 1285 |
|
| 1286 |
*Returns:* `true` if the shared lock was acquired, `false` otherwise.
|
|
|
|
| 1299 |
*Effects:* The function attempts to obtain shared ownership of the
|
| 1300 |
mutex. If `abs_time` has already passed, the function attempts to obtain
|
| 1301 |
shared ownership without blocking (as if by calling
|
| 1302 |
`try_lock_shared()`). The function shall return before the absolute
|
| 1303 |
timeout ([[thread.req.timing]]) specified by `abs_time` only if it has
|
| 1304 |
+
obtained shared ownership of the mutex object.
|
| 1305 |
+
|
| 1306 |
+
[*Note 2*: As with `try_lock()`, there is no guarantee that ownership
|
| 1307 |
+
will be obtained if the lock is available, but implementations are
|
| 1308 |
+
expected to make a strong effort to do so. — *end note*]
|
| 1309 |
+
|
| 1310 |
+
If an exception is thrown then a shared lock shall not have been
|
| 1311 |
acquired for the current thread.
|
| 1312 |
|
| 1313 |
*Return type:* `bool`.
|
| 1314 |
|
| 1315 |
*Returns:* `true` if the shared lock was acquired, `false` otherwise.
|
|
|
|
| 1356 |
```
|
| 1357 |
|
| 1358 |
The class `shared_timed_mutex` provides a non-recursive mutex with
|
| 1359 |
shared ownership semantics.
|
| 1360 |
|
| 1361 |
+
The class `shared_timed_mutex` shall satisfy all of the shared timed
|
| 1362 |
+
mutex requirements ([[thread.sharedtimedmutex.requirements]]). It shall
|
| 1363 |
+
be a standard-layout class (Clause [[class]]).
|
|
|
|
| 1364 |
|
| 1365 |
The behavior of a program is undefined if:
|
| 1366 |
|
| 1367 |
- it destroys a `shared_timed_mutex` object owned by any thread,
|
| 1368 |
- a thread attempts to recursively gain any ownership of a
|
|
|
|
| 1376 |
may unlock the lockable object during the lock’s destruction (such as
|
| 1377 |
when leaving block scope). An execution agent may use a lock to aid in
|
| 1378 |
managing ownership of a lockable object in an exception safe manner. A
|
| 1379 |
lock is said to *own* a lockable object if it is currently managing the
|
| 1380 |
ownership of that lockable object for an execution agent. A lock does
|
| 1381 |
+
not manage the lifetime of the lockable object it references.
|
| 1382 |
+
|
| 1383 |
+
[*Note 1*: Locks are intended to ease the burden of unlocking the
|
| 1384 |
+
lockable object under both normal and exceptional
|
| 1385 |
+
circumstances. — *end note*]
|
| 1386 |
|
| 1387 |
Some lock constructors take tag types which describe what should be done
|
| 1388 |
with the lockable object during the lock’s construction.
|
| 1389 |
|
| 1390 |
``` cpp
|
|
|
|
| 1393 |
struct try_to_lock_t { }; // try to acquire ownership of the mutex
|
| 1394 |
// without blocking
|
| 1395 |
struct adopt_lock_t { }; // assume the calling thread has already
|
| 1396 |
// obtained mutex ownership and manage it
|
| 1397 |
|
| 1398 |
+
inline constexpr defer_lock_t defer_lock { };
|
| 1399 |
+
inline constexpr try_to_lock_t try_to_lock { };
|
| 1400 |
+
inline constexpr adopt_lock_t adopt_lock { };
|
| 1401 |
}
|
| 1402 |
```
|
| 1403 |
|
| 1404 |
#### Class template `lock_guard` <a id="thread.lock.guard">[[thread.lock.guard]]</a>
|
| 1405 |
|
| 1406 |
``` cpp
|
| 1407 |
namespace std {
|
| 1408 |
template <class Mutex>
|
| 1409 |
class lock_guard {
|
| 1410 |
public:
|
| 1411 |
+
using mutex_type = Mutex;
|
| 1412 |
|
| 1413 |
explicit lock_guard(mutex_type& m);
|
| 1414 |
lock_guard(mutex_type& m, adopt_lock_t);
|
| 1415 |
~lock_guard();
|
| 1416 |
|
| 1417 |
+
lock_guard(const lock_guard&) = delete;
|
| 1418 |
+
lock_guard& operator=(const lock_guard&) = delete;
|
| 1419 |
|
| 1420 |
private:
|
| 1421 |
mutex_type& pm; // exposition only
|
| 1422 |
};
|
| 1423 |
+
|
| 1424 |
+
template<class Mutex> lock_guard(lock_guard<Mutex>) -> lock_guard<Mutex>;
|
| 1425 |
}
|
| 1426 |
```
|
| 1427 |
|
| 1428 |
An object of type `lock_guard` controls the ownership of a lockable
|
| 1429 |
object within a scope. A `lock_guard` object maintains ownership of a
|
|
|
|
| 1435 |
|
| 1436 |
``` cpp
|
| 1437 |
explicit lock_guard(mutex_type& m);
|
| 1438 |
```
|
| 1439 |
|
| 1440 |
+
*Requires:* If `mutex_type` is not a recursive mutex, the calling thread
|
| 1441 |
+
does not own the mutex `m`.
|
| 1442 |
|
| 1443 |
+
*Effects:* As if by `m.lock()`.
|
| 1444 |
|
| 1445 |
+
*Postconditions:* `&pm == &m`
|
| 1446 |
|
| 1447 |
``` cpp
|
| 1448 |
lock_guard(mutex_type& m, adopt_lock_t);
|
| 1449 |
```
|
| 1450 |
|
| 1451 |
+
*Requires:* The calling thread owns the mutex `m`.
|
| 1452 |
|
| 1453 |
+
*Postconditions:* `&pm == &m`
|
| 1454 |
|
| 1455 |
*Throws:* Nothing.
|
| 1456 |
|
| 1457 |
``` cpp
|
| 1458 |
~lock_guard();
|
| 1459 |
```
|
| 1460 |
|
| 1461 |
+
*Effects:* As if by `pm.unlock()`.
|
| 1462 |
+
|
| 1463 |
+
#### Class template `scoped_lock` <a id="thread.lock.scoped">[[thread.lock.scoped]]</a>
|
| 1464 |
+
|
| 1465 |
+
``` cpp
|
| 1466 |
+
namespace std {
|
| 1467 |
+
template <class... MutexTypes>
|
| 1468 |
+
class scoped_lock {
|
| 1469 |
+
public:
|
| 1470 |
+
using mutex_type = Mutex; // If MutexTypes... consists of the single type Mutex
|
| 1471 |
+
|
| 1472 |
+
explicit scoped_lock(MutexTypes&... m);
|
| 1473 |
+
explicit scoped_lock(MutexTypes&... m, adopt_lock_t);
|
| 1474 |
+
~scoped_lock();
|
| 1475 |
+
|
| 1476 |
+
scoped_lock(const scoped_lock&) = delete;
|
| 1477 |
+
scoped_lock& operator=(const scoped_lock&) = delete;
|
| 1478 |
+
|
| 1479 |
+
private:
|
| 1480 |
+
tuple<MutexTypes&...> pm; // exposition only
|
| 1481 |
+
};
|
| 1482 |
+
|
| 1483 |
+
template<class... MutexTypes>
|
| 1484 |
+
scoped_lock(scoped_lock<MutexTypes...>) -> scoped_lock<MutexTypes...>;
|
| 1485 |
+
}
|
| 1486 |
+
```
|
| 1487 |
+
|
| 1488 |
+
An object of type `scoped_lock` controls the ownership of lockable
|
| 1489 |
+
objects within a scope. A `scoped_lock` object maintains ownership of
|
| 1490 |
+
lockable objects throughout the `scoped_lock` object’s lifetime (
|
| 1491 |
+
[[basic.life]]). The behavior of a program is undefined if the lockable
|
| 1492 |
+
objects referenced by `pm` do not exist for the entire lifetime of the
|
| 1493 |
+
`scoped_lock` object. When `sizeof...(MutexTypes)` is `1`, the supplied
|
| 1494 |
+
`Mutex` type shall meet the `BasicLockable` requirements (
|
| 1495 |
+
[[thread.req.lockable.basic]]). Otherwise, each of the mutex types shall
|
| 1496 |
+
meet the `Lockable` requirements ([[thread.req.lockable.req]]).
|
| 1497 |
+
|
| 1498 |
+
``` cpp
|
| 1499 |
+
explicit scoped_lock(MutexTypes&... m);
|
| 1500 |
+
```
|
| 1501 |
+
|
| 1502 |
+
*Requires:* If a `MutexTypes` type is not a recursive mutex, the calling
|
| 1503 |
+
thread does not own the corresponding mutex element of `m`.
|
| 1504 |
+
|
| 1505 |
+
*Effects:* Initializes `pm` with `tie(m...)`. Then if
|
| 1506 |
+
`sizeof...(MutexTypes)` is `0`, no effects. Otherwise if
|
| 1507 |
+
`sizeof...(MutexTypes)` is `1`, then `m.lock()`. Otherwise,
|
| 1508 |
+
`lock(m...)`.
|
| 1509 |
+
|
| 1510 |
+
``` cpp
|
| 1511 |
+
explicit scoped_lock(MutexTypes&... m, adopt_lock_t);
|
| 1512 |
+
```
|
| 1513 |
+
|
| 1514 |
+
*Requires:* The calling thread owns all the mutexes in `m`.
|
| 1515 |
+
|
| 1516 |
+
*Effects:* Initializes `pm` with `tie(m...)`.
|
| 1517 |
+
|
| 1518 |
+
*Throws:* Nothing.
|
| 1519 |
+
|
| 1520 |
+
``` cpp
|
| 1521 |
+
~scoped_lock();
|
| 1522 |
+
```
|
| 1523 |
+
|
| 1524 |
+
*Effects:* For all `i` in \[`0`, `sizeof...(MutexTypes)`),
|
| 1525 |
+
`get<i>(pm).unlock()`.
|
| 1526 |
|
| 1527 |
#### Class template `unique_lock` <a id="thread.lock.unique">[[thread.lock.unique]]</a>
|
| 1528 |
|
| 1529 |
``` cpp
|
| 1530 |
namespace std {
|
| 1531 |
template <class Mutex>
|
| 1532 |
class unique_lock {
|
| 1533 |
public:
|
| 1534 |
+
using mutex_type = Mutex;
|
| 1535 |
|
| 1536 |
+
// [thread.lock.unique.cons], construct/copy/destroy
|
| 1537 |
unique_lock() noexcept;
|
| 1538 |
explicit unique_lock(mutex_type& m);
|
| 1539 |
unique_lock(mutex_type& m, defer_lock_t) noexcept;
|
| 1540 |
unique_lock(mutex_type& m, try_to_lock_t);
|
| 1541 |
unique_lock(mutex_type& m, adopt_lock_t);
|
|
|
|
| 1543 |
unique_lock(mutex_type& m, const chrono::time_point<Clock, Duration>& abs_time);
|
| 1544 |
template <class Rep, class Period>
|
| 1545 |
unique_lock(mutex_type& m, const chrono::duration<Rep, Period>& rel_time);
|
| 1546 |
~unique_lock();
|
| 1547 |
|
| 1548 |
+
unique_lock(const unique_lock&) = delete;
|
| 1549 |
+
unique_lock& operator=(const unique_lock&) = delete;
|
| 1550 |
|
| 1551 |
unique_lock(unique_lock&& u) noexcept;
|
| 1552 |
unique_lock& operator=(unique_lock&& u);
|
| 1553 |
|
| 1554 |
+
// [thread.lock.unique.locking], locking
|
| 1555 |
void lock();
|
| 1556 |
bool try_lock();
|
| 1557 |
|
| 1558 |
template <class Rep, class Period>
|
| 1559 |
bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
|
| 1560 |
template <class Clock, class Duration>
|
| 1561 |
bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
|
| 1562 |
|
| 1563 |
void unlock();
|
| 1564 |
|
| 1565 |
+
// [thread.lock.unique.mod], modifiers
|
| 1566 |
void swap(unique_lock& u) noexcept;
|
| 1567 |
mutex_type* release() noexcept;
|
| 1568 |
|
| 1569 |
+
// [thread.lock.unique.obs], observers
|
| 1570 |
bool owns_lock() const noexcept;
|
| 1571 |
explicit operator bool () const noexcept;
|
| 1572 |
mutex_type* mutex() const noexcept;
|
| 1573 |
|
| 1574 |
private:
|
| 1575 |
mutex_type* pm; // exposition only
|
| 1576 |
bool owns; // exposition only
|
| 1577 |
};
|
| 1578 |
|
| 1579 |
+
template<class Mutex> unique_lock(unique_lock<Mutex>) -> unique_lock<Mutex>;
|
| 1580 |
+
|
| 1581 |
template <class Mutex>
|
| 1582 |
void swap(unique_lock<Mutex>& x, unique_lock<Mutex>& y) noexcept;
|
| 1583 |
}
|
| 1584 |
```
|
| 1585 |
|
|
|
|
| 1592 |
lockable object pointed to by `pm` does not exist for the entire
|
| 1593 |
remaining lifetime ([[basic.life]]) of the `unique_lock` object. The
|
| 1594 |
supplied `Mutex` type shall meet the `BasicLockable` requirements (
|
| 1595 |
[[thread.req.lockable.basic]]).
|
| 1596 |
|
| 1597 |
+
[*Note 1*: `unique_lock<Mutex>` meets the `BasicLockable` requirements.
|
| 1598 |
+
If `Mutex` meets the `Lockable` requirements (
|
| 1599 |
+
[[thread.req.lockable.req]]), `unique_lock<Mutex>` also meets the
|
| 1600 |
+
`Lockable` requirements; if `Mutex` meets the `TimedLockable`
|
| 1601 |
+
requirements ([[thread.req.lockable.timed]]), `unique_lock<Mutex>` also
|
| 1602 |
+
meets the `TimedLockable` requirements. — *end note*]
|
| 1603 |
|
| 1604 |
##### `unique_lock` constructors, destructor, and assignment <a id="thread.lock.unique.cons">[[thread.lock.unique.cons]]</a>
|
| 1605 |
|
| 1606 |
``` cpp
|
| 1607 |
unique_lock() noexcept;
|
|
|
|
| 1613 |
|
| 1614 |
``` cpp
|
| 1615 |
explicit unique_lock(mutex_type& m);
|
| 1616 |
```
|
| 1617 |
|
| 1618 |
+
*Requires:* If `mutex_type` is not a recursive mutex the calling thread
|
| 1619 |
+
does not own the mutex.
|
| 1620 |
|
| 1621 |
*Effects:* Constructs an object of type `unique_lock` and calls
|
| 1622 |
`m.lock()`.
|
| 1623 |
|
| 1624 |
+
*Postconditions:* `pm == addressof(m)` and `owns == true`.
|
| 1625 |
|
| 1626 |
``` cpp
|
| 1627 |
unique_lock(mutex_type& m, defer_lock_t) noexcept;
|
| 1628 |
```
|
| 1629 |
|
| 1630 |
*Effects:* Constructs an object of type `unique_lock`.
|
| 1631 |
|
| 1632 |
+
*Postconditions:* `pm == addressof(m)` and `owns == false`.
|
| 1633 |
|
| 1634 |
``` cpp
|
| 1635 |
unique_lock(mutex_type& m, try_to_lock_t);
|
| 1636 |
```
|
| 1637 |
|
| 1638 |
+
*Requires:* The supplied `Mutex` type shall meet the `Lockable`
|
| 1639 |
requirements ([[thread.req.lockable.req]]). If `mutex_type` is not a
|
| 1640 |
recursive mutex the calling thread does not own the mutex.
|
| 1641 |
|
| 1642 |
*Effects:* Constructs an object of type `unique_lock` and calls
|
| 1643 |
`m.try_lock()`.
|
| 1644 |
|
| 1645 |
+
*Postconditions:* `pm == addressof(m)` and `owns == res`, where `res` is
|
| 1646 |
+
the value returned by the call to `m.try_lock()`.
|
| 1647 |
|
| 1648 |
``` cpp
|
| 1649 |
unique_lock(mutex_type& m, adopt_lock_t);
|
| 1650 |
```
|
| 1651 |
|
| 1652 |
+
*Requires:* The calling thread owns the mutex.
|
| 1653 |
|
| 1654 |
*Effects:* Constructs an object of type `unique_lock`.
|
| 1655 |
|
| 1656 |
+
*Postconditions:* `pm == addressof(m)` and `owns == true`.
|
| 1657 |
|
| 1658 |
*Throws:* Nothing.
|
| 1659 |
|
| 1660 |
``` cpp
|
| 1661 |
template <class Clock, class Duration>
|
| 1662 |
unique_lock(mutex_type& m, const chrono::time_point<Clock, Duration>& abs_time);
|
| 1663 |
```
|
| 1664 |
|
| 1665 |
+
*Requires:* If `mutex_type` is not a recursive mutex the calling thread
|
| 1666 |
+
does not own the mutex. The supplied `Mutex` type shall meet the
|
| 1667 |
+
`TimedLockable` requirements ([[thread.req.lockable.timed]]).
|
| 1668 |
|
| 1669 |
*Effects:* Constructs an object of type `unique_lock` and calls
|
| 1670 |
`m.try_lock_until(abs_time)`.
|
| 1671 |
|
| 1672 |
+
*Postconditions:* `pm == addressof(m)` and `owns == res`, where `res` is
|
| 1673 |
+
the value returned by the call to `m.try_lock_until(abs_time)`.
|
| 1674 |
|
| 1675 |
``` cpp
|
| 1676 |
template <class Rep, class Period>
|
| 1677 |
unique_lock(mutex_type& m, const chrono::duration<Rep, Period>& rel_time);
|
| 1678 |
```
|
| 1679 |
|
| 1680 |
+
*Requires:* If `mutex_type` is not a recursive mutex the calling thread
|
| 1681 |
+
does not own the mutex. The supplied `Mutex` type shall meet the
|
| 1682 |
+
`TimedLockable` requirements ([[thread.req.lockable.timed]]).
|
| 1683 |
|
| 1684 |
*Effects:* Constructs an object of type `unique_lock` and calls
|
| 1685 |
`m.try_lock_for(rel_time)`.
|
| 1686 |
|
| 1687 |
+
*Postconditions:* `pm == addressof(m)` and `owns == res`, where `res` is
|
| 1688 |
+
the value returned by the call to `m.try_lock_for(rel_time)`.
|
| 1689 |
|
| 1690 |
``` cpp
|
| 1691 |
unique_lock(unique_lock&& u) noexcept;
|
| 1692 |
```
|
| 1693 |
|
|
|
|
| 1703 |
|
| 1704 |
*Postconditions:* `pm == u_p.pm` and `owns == u_p.owns` (where `u_p` is
|
| 1705 |
the state of `u` just prior to this construction), `u.pm == 0` and
|
| 1706 |
`u.owns == false`.
|
| 1707 |
|
| 1708 |
+
[*Note 1*: With a recursive mutex it is possible for both `*this` and
|
| 1709 |
+
`u` to own the same mutex before the assignment. In this case, `*this`
|
| 1710 |
+
will own the mutex after the assignment and `u` will not. — *end note*]
|
| 1711 |
|
| 1712 |
*Throws:* Nothing.
|
| 1713 |
|
| 1714 |
``` cpp
|
| 1715 |
~unique_lock();
|
|
|
|
| 1721 |
|
| 1722 |
``` cpp
|
| 1723 |
void lock();
|
| 1724 |
```
|
| 1725 |
|
| 1726 |
+
*Effects:* As if by `pm->lock()`.
|
| 1727 |
|
| 1728 |
+
*Postconditions:* `owns == true`.
|
| 1729 |
|
| 1730 |
+
*Throws:* Any exception thrown by `pm->lock()`. `system_error` when an
|
| 1731 |
+
exception is required ([[thread.req.exception]]).
|
| 1732 |
+
|
| 1733 |
+
*Error conditions:*
|
| 1734 |
+
|
| 1735 |
+
- `operation_not_permitted` — if `pm` is `nullptr`.
|
| 1736 |
+
- `resource_deadlock_would_occur` — if on entry `owns` is `true`.
|
| 1737 |
|
| 1738 |
``` cpp
|
| 1739 |
bool try_lock();
|
| 1740 |
```
|
| 1741 |
|
| 1742 |
+
*Requires:* The supplied `Mutex` shall meet the `Lockable`
|
| 1743 |
requirements ([[thread.req.lockable.req]]).
|
| 1744 |
|
| 1745 |
+
*Effects:* As if by `pm->try_lock()`.
|
| 1746 |
|
| 1747 |
*Returns:* The value returned by the call to `try_lock()`.
|
| 1748 |
|
| 1749 |
+
*Postconditions:* `owns == res`, where `res` is the value returned by
|
| 1750 |
+
the call to `try_lock()`.
|
| 1751 |
|
| 1752 |
+
*Throws:* Any exception thrown by `pm->try_lock()`. `system_error` when
|
| 1753 |
+
an exception is required ([[thread.req.exception]]).
|
| 1754 |
+
|
| 1755 |
+
*Error conditions:*
|
| 1756 |
+
|
| 1757 |
+
- `operation_not_permitted` — if `pm` is `nullptr`.
|
| 1758 |
+
- `resource_deadlock_would_occur` — if on entry `owns` is `true`.
|
| 1759 |
|
| 1760 |
``` cpp
|
| 1761 |
template <class Clock, class Duration>
|
| 1762 |
bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
|
| 1763 |
```
|
| 1764 |
|
| 1765 |
*Requires:* The supplied `Mutex` type shall meet the `TimedLockable`
|
| 1766 |
requirements ([[thread.req.lockable.timed]]).
|
| 1767 |
|
| 1768 |
+
*Effects:* As if by `pm->try_lock_until(abs_time)`.
|
| 1769 |
|
| 1770 |
*Returns:* The value returned by the call to `try_lock_until(abs_time)`.
|
| 1771 |
|
| 1772 |
+
*Postconditions:* `owns == res`, where `res` is the value returned by
|
| 1773 |
+
the call to `try_lock_until(abs_time)`.
|
| 1774 |
|
| 1775 |
*Throws:* Any exception thrown by `pm->try_lock_until()`. `system_error`
|
| 1776 |
+
when an exception is required ([[thread.req.exception]]).
|
| 1777 |
+
|
| 1778 |
+
*Error conditions:*
|
| 1779 |
+
|
| 1780 |
+
- `operation_not_permitted` — if `pm` is `nullptr`.
|
| 1781 |
+
- `resource_deadlock_would_occur` — if on entry `owns` is `true`.
|
| 1782 |
|
| 1783 |
``` cpp
|
| 1784 |
template <class Rep, class Period>
|
| 1785 |
bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
|
| 1786 |
```
|
| 1787 |
|
| 1788 |
*Requires:* The supplied `Mutex` type shall meet the `TimedLockable`
|
| 1789 |
requirements ([[thread.req.lockable.timed]]).
|
| 1790 |
|
| 1791 |
+
*Effects:* As if by `pm->try_lock_for(rel_time)`.
|
| 1792 |
|
| 1793 |
*Returns:* The value returned by the call to `try_lock_until(rel_time)`.
|
| 1794 |
|
| 1795 |
+
*Postconditions:* `owns == res`, where `res` is the value returned by
|
| 1796 |
+
the call to `try_lock_for(rel_time)`.
|
| 1797 |
|
| 1798 |
*Throws:* Any exception thrown by `pm->try_lock_for()`. `system_error`
|
| 1799 |
+
when an exception is required ([[thread.req.exception]]).
|
| 1800 |
+
|
| 1801 |
+
*Error conditions:*
|
| 1802 |
+
|
| 1803 |
+
- `operation_not_permitted` — if `pm` is `nullptr`.
|
| 1804 |
+
- `resource_deadlock_would_occur` — if on entry `owns` is `true`.
|
| 1805 |
|
| 1806 |
``` cpp
|
| 1807 |
void unlock();
|
| 1808 |
```
|
| 1809 |
|
| 1810 |
+
*Effects:* As if by `pm->unlock()`.
|
| 1811 |
|
| 1812 |
+
*Postconditions:* `owns == false`.
|
| 1813 |
|
| 1814 |
*Throws:* `system_error` when an exception is
|
| 1815 |
required ([[thread.req.exception]]).
|
| 1816 |
|
| 1817 |
*Error conditions:*
|
| 1818 |
|
| 1819 |
+
- `operation_not_permitted` — if on entry `owns` is `false`.
|
| 1820 |
|
| 1821 |
##### `unique_lock` modifiers <a id="thread.lock.unique.mod">[[thread.lock.unique.mod]]</a>
|
| 1822 |
|
| 1823 |
``` cpp
|
| 1824 |
void swap(unique_lock& u) noexcept;
|
|
|
|
| 1837 |
``` cpp
|
| 1838 |
template <class Mutex>
|
| 1839 |
void swap(unique_lock<Mutex>& x, unique_lock<Mutex>& y) noexcept;
|
| 1840 |
```
|
| 1841 |
|
| 1842 |
+
*Effects:* As if by `x.swap(y)`.
|
| 1843 |
|
| 1844 |
##### `unique_lock` observers <a id="thread.lock.unique.obs">[[thread.lock.unique.obs]]</a>
|
| 1845 |
|
| 1846 |
``` cpp
|
| 1847 |
bool owns_lock() const noexcept;
|
| 1848 |
```
|
| 1849 |
|
| 1850 |
+
*Returns:* `owns`.
|
| 1851 |
|
| 1852 |
``` cpp
|
| 1853 |
explicit operator bool() const noexcept;
|
| 1854 |
```
|
| 1855 |
|
| 1856 |
+
*Returns:* `owns`.
|
| 1857 |
|
| 1858 |
``` cpp
|
| 1859 |
mutex_type *mutex() const noexcept;
|
| 1860 |
```
|
| 1861 |
|
| 1862 |
+
*Returns:* `pm`.
|
| 1863 |
|
| 1864 |
#### Class template `shared_lock` <a id="thread.lock.shared">[[thread.lock.shared]]</a>
|
| 1865 |
|
| 1866 |
``` cpp
|
| 1867 |
namespace std {
|
|
|
|
| 1868 |
template <class Mutex>
|
| 1869 |
class shared_lock {
|
| 1870 |
public:
|
| 1871 |
+
using mutex_type = Mutex;
|
| 1872 |
|
| 1873 |
+
// [thread.lock.shared.cons], construct/copy/destroy
|
| 1874 |
shared_lock() noexcept;
|
| 1875 |
explicit shared_lock(mutex_type& m); // blocking
|
| 1876 |
shared_lock(mutex_type& m, defer_lock_t) noexcept;
|
| 1877 |
shared_lock(mutex_type& m, try_to_lock_t);
|
| 1878 |
shared_lock(mutex_type& m, adopt_lock_t);
|
|
|
|
| 1882 |
template <class Rep, class Period>
|
| 1883 |
shared_lock(mutex_type& m,
|
| 1884 |
const chrono::duration<Rep, Period>& rel_time);
|
| 1885 |
~shared_lock();
|
| 1886 |
|
| 1887 |
+
shared_lock(const shared_lock&) = delete;
|
| 1888 |
+
shared_lock& operator=(const shared_lock&) = delete;
|
| 1889 |
|
| 1890 |
shared_lock(shared_lock&& u) noexcept;
|
| 1891 |
shared_lock& operator=(shared_lock&& u) noexcept;
|
| 1892 |
|
| 1893 |
+
// [thread.lock.shared.locking], locking
|
| 1894 |
void lock(); // blocking
|
| 1895 |
bool try_lock();
|
| 1896 |
template <class Rep, class Period>
|
| 1897 |
bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
|
| 1898 |
template <class Clock, class Duration>
|
| 1899 |
bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
|
| 1900 |
void unlock();
|
| 1901 |
|
| 1902 |
+
// [thread.lock.shared.mod], modifiers
|
| 1903 |
void swap(shared_lock& u) noexcept;
|
| 1904 |
mutex_type* release() noexcept;
|
| 1905 |
|
| 1906 |
+
// [thread.lock.shared.obs], observers
|
| 1907 |
bool owns_lock() const noexcept;
|
| 1908 |
explicit operator bool () const noexcept;
|
| 1909 |
mutex_type* mutex() const noexcept;
|
| 1910 |
|
| 1911 |
private:
|
| 1912 |
mutex_type* pm; // exposition only
|
| 1913 |
bool owns; // exposition only
|
| 1914 |
};
|
| 1915 |
|
| 1916 |
+
template<class Mutex> shared_lock(shared_lock<Mutex>) -> shared_lock<Mutex>;
|
| 1917 |
+
|
| 1918 |
template <class Mutex>
|
| 1919 |
void swap(shared_lock<Mutex>& x, shared_lock<Mutex>& y) noexcept;
|
| 1920 |
+
}
|
|
|
|
| 1921 |
```
|
| 1922 |
|
| 1923 |
An object of type `shared_lock` controls the shared ownership of a
|
| 1924 |
lockable object within a scope. Shared ownership of the lockable object
|
| 1925 |
may be acquired at construction or after construction, and may be
|
|
|
|
| 1929 |
lockable object pointed to by `pm` does not exist for the entire
|
| 1930 |
remaining lifetime ([[basic.life]]) of the `shared_lock` object. The
|
| 1931 |
supplied `Mutex` type shall meet the shared mutex requirements (
|
| 1932 |
[[thread.sharedtimedmutex.requirements]]).
|
| 1933 |
|
| 1934 |
+
[*Note 1*: `shared_lock<Mutex>` meets the `TimedLockable`
|
| 1935 |
+
requirements ([[thread.req.lockable.timed]]). — *end note*]
|
| 1936 |
|
| 1937 |
##### `shared_lock` constructors, destructor, and assignment <a id="thread.lock.shared.cons">[[thread.lock.shared.cons]]</a>
|
| 1938 |
|
| 1939 |
``` cpp
|
| 1940 |
shared_lock() noexcept;
|
|
|
|
| 1952 |
mode.
|
| 1953 |
|
| 1954 |
*Effects:* Constructs an object of type `shared_lock` and calls
|
| 1955 |
`m.lock_shared()`.
|
| 1956 |
|
| 1957 |
+
*Postconditions:* `pm == addressof(m)` and `owns == true`.
|
| 1958 |
|
| 1959 |
``` cpp
|
| 1960 |
shared_lock(mutex_type& m, defer_lock_t) noexcept;
|
| 1961 |
```
|
| 1962 |
|
| 1963 |
*Effects:* Constructs an object of type `shared_lock`.
|
| 1964 |
|
| 1965 |
+
*Postconditions:* `pm == addressof(m)` and `owns == false`.
|
| 1966 |
|
| 1967 |
``` cpp
|
| 1968 |
shared_lock(mutex_type& m, try_to_lock_t);
|
| 1969 |
```
|
| 1970 |
|
|
|
|
| 1972 |
mode.
|
| 1973 |
|
| 1974 |
*Effects:* Constructs an object of type `shared_lock` and calls
|
| 1975 |
`m.try_lock_shared()`.
|
| 1976 |
|
| 1977 |
+
*Postconditions:* `pm == addressof(m)` and `owns == res` where `res` is
|
| 1978 |
+
the value returned by the call to `m.try_lock_shared()`.
|
| 1979 |
|
| 1980 |
``` cpp
|
| 1981 |
shared_lock(mutex_type& m, adopt_lock_t);
|
| 1982 |
```
|
| 1983 |
|
| 1984 |
*Requires:* The calling thread has shared ownership of the mutex.
|
| 1985 |
|
| 1986 |
*Effects:* Constructs an object of type `shared_lock`.
|
| 1987 |
|
| 1988 |
+
*Postconditions:* `pm == addressof(m)` and `owns == true`.
|
| 1989 |
|
| 1990 |
``` cpp
|
| 1991 |
template <class Clock, class Duration>
|
| 1992 |
shared_lock(mutex_type& m,
|
| 1993 |
const chrono::time_point<Clock, Duration>& abs_time);
|
|
|
|
| 1997 |
mode.
|
| 1998 |
|
| 1999 |
*Effects:* Constructs an object of type `shared_lock` and calls
|
| 2000 |
`m.try_lock_shared_until(abs_time)`.
|
| 2001 |
|
| 2002 |
+
*Postconditions:* `pm == addressof(m)` and `owns == res` where `res` is
|
| 2003 |
+
the value returned by the call to `m.try_lock_shared_until(abs_time)`.
|
| 2004 |
|
| 2005 |
``` cpp
|
| 2006 |
template <class Rep, class Period>
|
| 2007 |
shared_lock(mutex_type& m,
|
| 2008 |
const chrono::duration<Rep, Period>& rel_time);
|
|
|
|
| 2012 |
mode.
|
| 2013 |
|
| 2014 |
*Effects:* Constructs an object of type `shared_lock` and calls
|
| 2015 |
`m.try_lock_shared_for(rel_time)`.
|
| 2016 |
|
| 2017 |
+
*Postconditions:* `pm == addressof(m)` and `owns == res` where `res` is
|
| 2018 |
+
the value returned by the call to `m.try_lock_shared_for(rel_time)`.
|
| 2019 |
|
| 2020 |
``` cpp
|
| 2021 |
~shared_lock();
|
| 2022 |
```
|
| 2023 |
|
|
|
|
| 2025 |
|
| 2026 |
``` cpp
|
| 2027 |
shared_lock(shared_lock&& sl) noexcept;
|
| 2028 |
```
|
| 2029 |
|
| 2030 |
+
*Postconditions:* `pm == sl_p.pm` and `owns == sl_p.owns` (where `sl_p`
|
| 2031 |
is the state of `sl` just prior to this construction),
|
| 2032 |
`sl.pm == nullptr` and `sl.owns == false`.
|
| 2033 |
|
| 2034 |
``` cpp
|
| 2035 |
shared_lock& operator=(shared_lock&& sl) noexcept;
|
| 2036 |
```
|
| 2037 |
|
| 2038 |
*Effects:* If `owns` calls `pm->unlock_shared()`.
|
| 2039 |
|
| 2040 |
+
*Postconditions:* `pm == sl_p.pm` and `owns == sl_p.owns` (where `sl_p`
|
| 2041 |
is the state of `sl` just prior to this assignment), `sl.pm == nullptr`
|
| 2042 |
and `sl.owns == false`.
|
| 2043 |
|
| 2044 |
##### `shared_lock` locking <a id="thread.lock.shared.locking">[[thread.lock.shared.locking]]</a>
|
| 2045 |
|
| 2046 |
``` cpp
|
| 2047 |
void lock();
|
| 2048 |
```
|
| 2049 |
|
| 2050 |
+
*Effects:* As if by `pm->lock_shared()`.
|
| 2051 |
|
| 2052 |
*Postconditions:* `owns == true`.
|
| 2053 |
|
| 2054 |
+
*Throws:* Any exception thrown by `pm->lock_shared()`. `system_error`
|
| 2055 |
+
when an exception is required ([[thread.req.exception]]).
|
| 2056 |
+
|
| 2057 |
+
*Error conditions:*
|
| 2058 |
+
|
| 2059 |
+
- `operation_not_permitted` — if `pm` is `nullptr`.
|
| 2060 |
+
- `resource_deadlock_would_occur` — if on entry `owns` is `true`.
|
| 2061 |
|
| 2062 |
``` cpp
|
| 2063 |
bool try_lock();
|
| 2064 |
```
|
| 2065 |
|
| 2066 |
+
*Effects:* As if by `pm->try_lock_shared()`.
|
| 2067 |
|
| 2068 |
*Returns:* The value returned by the call to `pm->try_lock_shared()`.
|
| 2069 |
|
| 2070 |
*Postconditions:* `owns == res`, where `res` is the value returned by
|
| 2071 |
the call to `pm->try_lock_shared()`.
|
| 2072 |
|
| 2073 |
*Throws:* Any exception thrown by `pm->try_lock_shared()`.
|
| 2074 |
+
`system_error` when an exception is
|
| 2075 |
+
required ([[thread.req.exception]]).
|
| 2076 |
+
|
| 2077 |
+
*Error conditions:*
|
| 2078 |
+
|
| 2079 |
+
- `operation_not_permitted` — if `pm` is `nullptr`.
|
| 2080 |
+
- `resource_deadlock_would_occur` — if on entry `owns` is `true`.
|
| 2081 |
|
| 2082 |
``` cpp
|
| 2083 |
template <class Clock, class Duration>
|
| 2084 |
bool
|
| 2085 |
try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
|
| 2086 |
```
|
| 2087 |
|
| 2088 |
+
*Effects:* As if by `pm->try_lock_shared_until(abs_time)`.
|
| 2089 |
|
| 2090 |
*Returns:* The value returned by the call to
|
| 2091 |
`pm->try_lock_shared_until(abs_time)`.
|
| 2092 |
|
| 2093 |
*Postconditions:* `owns == res`, where `res` is the value returned by
|
| 2094 |
the call to `pm->try_lock_shared_until(abs_time)`.
|
| 2095 |
|
| 2096 |
*Throws:* Any exception thrown by `pm->try_lock_shared_until(abs_time)`.
|
| 2097 |
+
`system_error` when an exception is
|
| 2098 |
+
required ([[thread.req.exception]]).
|
| 2099 |
+
|
| 2100 |
+
*Error conditions:*
|
| 2101 |
+
|
| 2102 |
+
- `operation_not_permitted` — if `pm` is `nullptr`.
|
| 2103 |
+
- `resource_deadlock_would_occur` — if on entry `owns` is `true`.
|
| 2104 |
|
| 2105 |
``` cpp
|
| 2106 |
template <class Rep, class Period>
|
| 2107 |
bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
|
| 2108 |
```
|
| 2109 |
|
| 2110 |
+
*Effects:* As if by `pm->try_lock_shared_for(rel_time)`.
|
| 2111 |
|
| 2112 |
*Returns:* The value returned by the call to
|
| 2113 |
`pm->try_lock_shared_for(rel_time)`.
|
| 2114 |
|
| 2115 |
*Postconditions:* `owns == res`, where `res` is the value returned by
|
| 2116 |
the call to `pm->try_lock_shared_for(rel_time)`.
|
| 2117 |
|
| 2118 |
*Throws:* Any exception thrown by `pm->try_lock_shared_for(rel_time)`.
|
| 2119 |
+
`system_error` when an exception is
|
| 2120 |
+
required ([[thread.req.exception]]).
|
| 2121 |
+
|
| 2122 |
+
*Error conditions:*
|
| 2123 |
+
|
| 2124 |
+
- `operation_not_permitted` — if `pm` is `nullptr`.
|
| 2125 |
+
- `resource_deadlock_would_occur` — if on entry `owns` is `true`.
|
| 2126 |
|
| 2127 |
``` cpp
|
| 2128 |
void unlock();
|
| 2129 |
```
|
| 2130 |
|
| 2131 |
+
*Effects:* As if by `pm->unlock_shared()`.
|
| 2132 |
|
| 2133 |
*Postconditions:* `owns == false`.
|
| 2134 |
|
| 2135 |
+
*Throws:* `system_error` when an exception is
|
| 2136 |
+
required ([[thread.req.exception]]).
|
| 2137 |
|
| 2138 |
*Error conditions:*
|
| 2139 |
|
| 2140 |
- `operation_not_permitted` — if on entry `owns` is `false`.
|
| 2141 |
|
|
|
|
| 2158 |
``` cpp
|
| 2159 |
template <class Mutex>
|
| 2160 |
void swap(shared_lock<Mutex>& x, shared_lock<Mutex>& y) noexcept;
|
| 2161 |
```
|
| 2162 |
|
| 2163 |
+
*Effects:* As if by `x.swap(y)`.
|
| 2164 |
|
| 2165 |
##### `shared_lock` observers <a id="thread.lock.shared.obs">[[thread.lock.shared.obs]]</a>
|
| 2166 |
|
| 2167 |
``` cpp
|
| 2168 |
bool owns_lock() const noexcept;
|
|
|
|
| 2187 |
``` cpp
|
| 2188 |
template <class L1, class L2, class... L3> int try_lock(L1&, L2&, L3&...);
|
| 2189 |
```
|
| 2190 |
|
| 2191 |
*Requires:* Each template parameter type shall meet the `Lockable`
|
| 2192 |
+
requirements.
|
| 2193 |
+
|
| 2194 |
+
[*Note 1*: The `unique_lock` class template meets these requirements
|
| 2195 |
+
when suitably instantiated. — *end note*]
|
| 2196 |
|
| 2197 |
*Effects:* Calls `try_lock()` for each argument in order beginning with
|
| 2198 |
the first until all arguments have been processed or a call to
|
| 2199 |
`try_lock()` fails, either by returning `false` or by throwing an
|
| 2200 |
exception. If a call to `try_lock()` fails, `unlock()` shall be called
|
| 2201 |
for all prior arguments and there shall be no further calls to
|
| 2202 |
`try_lock()`.
|
| 2203 |
|
| 2204 |
*Returns:* `-1` if all calls to `try_lock()` returned `true`, otherwise
|
| 2205 |
+
a zero-based index value that indicates the argument for which
|
| 2206 |
+
`try_lock()` returned `false`.
|
| 2207 |
|
| 2208 |
``` cpp
|
| 2209 |
template <class L1, class L2, class... L3> void lock(L1&, L2&, L3&...);
|
| 2210 |
```
|
| 2211 |
|
| 2212 |
*Requires:* Each template parameter type shall meet the `Lockable`
|
| 2213 |
+
requirements,
|
| 2214 |
+
|
| 2215 |
+
[*Note 2*: The `unique_lock` class template meets these requirements
|
| 2216 |
+
when suitably instantiated. — *end note*]
|
| 2217 |
|
| 2218 |
*Effects:* All arguments are locked via a sequence of calls to `lock()`,
|
| 2219 |
`try_lock()`, or `unlock()` on each argument. The sequence of calls
|
| 2220 |
+
shall not result in deadlock, but is otherwise unspecified.
|
| 2221 |
+
|
| 2222 |
+
[*Note 3*: A deadlock avoidance algorithm such as try-and-back-off must
|
| 2223 |
+
be used, but the specific algorithm is not specified to avoid
|
| 2224 |
+
over-constraining implementations. — *end note*]
|
| 2225 |
+
|
| 2226 |
+
If a call to `lock()` or `try_lock()` throws an exception, `unlock()`
|
| 2227 |
+
shall be called for any argument that had been locked by a call to
|
| 2228 |
+
`lock()` or `try_lock()`.
|
| 2229 |
|
| 2230 |
### Call once <a id="thread.once">[[thread.once]]</a>
|
| 2231 |
|
| 2232 |
+
#### Struct `once_flag` <a id="thread.once.onceflag">[[thread.once.onceflag]]</a>
|
| 2233 |
+
|
| 2234 |
+
``` cpp
|
| 2235 |
+
namespace std {
|
| 2236 |
+
struct once_flag {
|
| 2237 |
+
constexpr once_flag() noexcept;
|
| 2238 |
+
|
| 2239 |
+
once_flag(const once_flag&) = delete;
|
| 2240 |
+
once_flag& operator=(const once_flag&) = delete;
|
| 2241 |
+
};
|
| 2242 |
+
}
|
| 2243 |
+
```
|
| 2244 |
+
|
| 2245 |
The class `once_flag` is an opaque data structure that `call_once` uses
|
| 2246 |
to initialize data without causing a data race or deadlock.
|
| 2247 |
|
|
|
|
|
|
|
| 2248 |
``` cpp
|
| 2249 |
constexpr once_flag() noexcept;
|
| 2250 |
```
|
| 2251 |
|
| 2252 |
*Effects:* Constructs an object of type `once_flag`.
|
| 2253 |
|
| 2254 |
*Synchronization:* The construction of a `once_flag` object is not
|
| 2255 |
synchronized.
|
| 2256 |
|
| 2257 |
+
*Postconditions:* The object’s internal state is set to indicate to an
|
| 2258 |
+
invocation of `call_once` with the object as its initial argument that
|
| 2259 |
+
no function has been called.
|
| 2260 |
|
| 2261 |
#### Function `call_once` <a id="thread.once.callonce">[[thread.once.callonce]]</a>
|
| 2262 |
|
| 2263 |
``` cpp
|
| 2264 |
template<class Callable, class... Args>
|
| 2265 |
void call_once(once_flag& flag, Callable&& func, Args&&... args);
|
| 2266 |
```
|
| 2267 |
|
| 2268 |
+
*Requires:*
|
| 2269 |
+
|
| 2270 |
+
``` cpp
|
| 2271 |
+
INVOKE(std::forward<Callable>(func), std::forward<Args>(args)...)
|
| 2272 |
+
```
|
| 2273 |
+
|
| 2274 |
+
(see [[func.require]]) shall be a valid expression.
|
| 2275 |
|
| 2276 |
*Effects:* An execution of `call_once` that does not call its `func` is
|
| 2277 |
a *passive* execution. An execution of `call_once` that calls its `func`
|
| 2278 |
+
is an *active* execution. An active execution shall call *INVOKE*(
|
| 2279 |
+
std::forward\<Callable\>(func), std::forward\<Args\>(args)...). If such
|
| 2280 |
+
a call to `func` throws an exception the execution is *exceptional*,
|
| 2281 |
+
otherwise it is *returning*. An exceptional execution shall propagate
|
| 2282 |
+
the exception to the caller of `call_once`. Among all executions of
|
| 2283 |
+
`call_once` for any given `once_flag`: at most one shall be a returning
|
| 2284 |
+
execution; if there is a returning execution, it shall be the last
|
| 2285 |
+
active execution; and there are passive executions only if there is a
|
| 2286 |
+
returning execution.
|
| 2287 |
+
|
| 2288 |
+
[*Note 1*: Passive executions allow other threads to reliably observe
|
| 2289 |
+
the results produced by the earlier returning execution. — *end note*]
|
| 2290 |
|
| 2291 |
*Synchronization:* For any given `once_flag`: all active executions
|
| 2292 |
occur in a total order; completion of an active execution synchronizes
|
| 2293 |
with ([[intro.multithread]]) the start of the next one in this total
|
| 2294 |
order; and the returning execution synchronizes with the return from all
|
| 2295 |
passive executions.
|
| 2296 |
|
| 2297 |
*Throws:* `system_error` when an exception is
|
| 2298 |
required ([[thread.req.exception]]), or any exception thrown by `func`.
|
| 2299 |
|
| 2300 |
+
[*Example 1*:
|
| 2301 |
+
|
| 2302 |
``` cpp
|
| 2303 |
// global flag, regular function
|
| 2304 |
void init();
|
| 2305 |
std::once_flag flag;
|
| 2306 |
|
|
|
|
| 2325 |
public:
|
| 2326 |
void verify() { std::call_once(verified, &information::verifier, *this); }
|
| 2327 |
};
|
| 2328 |
```
|
| 2329 |
|
| 2330 |
+
— *end example*]
|
| 2331 |
+
|
| 2332 |
## Condition variables <a id="thread.condition">[[thread.condition]]</a>
|
| 2333 |
|
| 2334 |
Condition variables provide synchronization primitives used to block a
|
| 2335 |
thread until notified by some other thread that some condition is met or
|
| 2336 |
until a system time is reached. Class `condition_variable` provides a
|
|
|
|
| 2357 |
with the "happens before" order.
|
| 2358 |
|
| 2359 |
Condition variable construction and destruction need not be
|
| 2360 |
synchronized.
|
| 2361 |
|
| 2362 |
+
### Header `<condition_variable>` synopsis <a id="condition_variable.syn">[[condition_variable.syn]]</a>
|
| 2363 |
+
|
| 2364 |
``` cpp
|
| 2365 |
namespace std {
|
| 2366 |
class condition_variable;
|
| 2367 |
class condition_variable_any;
|
| 2368 |
|
|
|
|
| 2370 |
|
| 2371 |
enum class cv_status { no_timeout, timeout };
|
| 2372 |
}
|
| 2373 |
```
|
| 2374 |
|
| 2375 |
+
### Non-member functions <a id="thread.condition.nonmember">[[thread.condition.nonmember]]</a>
|
| 2376 |
+
|
| 2377 |
``` cpp
|
| 2378 |
void notify_all_at_thread_exit(condition_variable& cond, unique_lock<mutex> lk);
|
| 2379 |
```
|
| 2380 |
|
| 2381 |
*Requires:* `lk` is locked by the calling thread and either
|
|
|
|
| 2383 |
- no other thread is waiting on `cond`, or
|
| 2384 |
- `lk.mutex()` returns the same value for each of the lock arguments
|
| 2385 |
supplied by all concurrently waiting (via `wait`, `wait_for`, or
|
| 2386 |
`wait_until`) threads.
|
| 2387 |
|
| 2388 |
+
*Effects:* Transfers ownership of the lock associated with `lk` into
|
| 2389 |
internal storage and schedules `cond` to be notified when the current
|
| 2390 |
thread exits, after all objects of thread storage duration associated
|
| 2391 |
with the current thread have been destroyed. This notification shall be
|
| 2392 |
+
as if:
|
| 2393 |
|
| 2394 |
``` cpp
|
| 2395 |
lk.unlock();
|
| 2396 |
cond.notify_all();
|
| 2397 |
```
|
| 2398 |
|
| 2399 |
*Synchronization:* The implied `lk.unlock()` call is sequenced after the
|
| 2400 |
destruction of all objects with thread storage duration associated with
|
| 2401 |
the current thread.
|
| 2402 |
|
| 2403 |
+
[*Note 1*: The supplied lock will be held until the thread exits, and
|
| 2404 |
+
care must be taken to ensure that this does not cause deadlock due to
|
| 2405 |
+
lock ordering issues. After calling `notify_all_at_thread_exit` it is
|
| 2406 |
recommended that the thread should be exited as soon as possible, and
|
| 2407 |
+
that no blocking or time-consuming tasks are run on that
|
| 2408 |
+
thread. — *end note*]
|
| 2409 |
|
| 2410 |
+
[*Note 2*: It is the user’s responsibility to ensure that waiting
|
| 2411 |
+
threads do not erroneously assume that the thread has finished if they
|
| 2412 |
experience spurious wakeups. This typically requires that the condition
|
| 2413 |
being waited for is satisfied while holding the lock on `lk`, and that
|
| 2414 |
this lock is not released and reacquired prior to calling
|
| 2415 |
+
`notify_all_at_thread_exit`. — *end note*]
|
| 2416 |
|
| 2417 |
### Class `condition_variable` <a id="thread.condition.condvar">[[thread.condition.condvar]]</a>
|
| 2418 |
|
| 2419 |
``` cpp
|
| 2420 |
namespace std {
|
|
|
|
| 2446 |
template <class Rep, class Period, class Predicate>
|
| 2447 |
bool wait_for(unique_lock<mutex>& lock,
|
| 2448 |
const chrono::duration<Rep, Period>& rel_time,
|
| 2449 |
Predicate pred);
|
| 2450 |
|
| 2451 |
+
using native_handle_type = implementation-defined; // See~[thread.req.native]
|
| 2452 |
native_handle_type native_handle(); // See~[thread.req.native]
|
| 2453 |
};
|
| 2454 |
}
|
| 2455 |
```
|
| 2456 |
|
|
|
|
| 2473 |
|
| 2474 |
``` cpp
|
| 2475 |
~condition_variable();
|
| 2476 |
```
|
| 2477 |
|
| 2478 |
+
*Requires:* There shall be no thread blocked on `*this`.
|
| 2479 |
+
|
| 2480 |
+
[*Note 1*: That is, all threads shall have been notified; they may
|
| 2481 |
+
subsequently block on the lock specified in the wait. This relaxes the
|
| 2482 |
+
usual rules, which would have required all wait calls to happen before
|
| 2483 |
+
destruction. Only the notification to unblock the wait must happen
|
| 2484 |
+
before destruction. The user must take care to ensure that no threads
|
| 2485 |
+
wait on `*this` once the destructor has been started, especially when
|
| 2486 |
+
the waiting threads are calling the wait functions in a loop or using
|
| 2487 |
+
the overloads of `wait`, `wait_for`, or `wait_until` that take a
|
| 2488 |
+
predicate. — *end note*]
|
| 2489 |
|
| 2490 |
*Effects:* Destroys the object.
|
| 2491 |
|
| 2492 |
``` cpp
|
| 2493 |
void notify_one() noexcept;
|
|
|
|
| 2504 |
|
| 2505 |
``` cpp
|
| 2506 |
void wait(unique_lock<mutex>& lock);
|
| 2507 |
```
|
| 2508 |
|
| 2509 |
+
*Requires:* `lock.owns_lock()` is `true` and `lock.mutex()` is locked by
|
| 2510 |
+
the calling thread, and either
|
| 2511 |
|
| 2512 |
- no other thread is waiting on this `condition_variable` object or
|
| 2513 |
- `lock.mutex()` returns the same value for each of the `lock` arguments
|
| 2514 |
supplied by all concurrently waiting (via `wait`, `wait_for`, or
|
| 2515 |
`wait_until`) threads.
|
|
|
|
| 2521 |
then returns.
|
| 2522 |
- The function will unblock when signaled by a call to `notify_one()` or
|
| 2523 |
a call to `notify_all()`, or spuriously.
|
| 2524 |
|
| 2525 |
*Remarks:* If the function fails to meet the postcondition,
|
| 2526 |
+
`terminate()` shall be called ([[except.terminate]]).
|
|
|
|
| 2527 |
|
| 2528 |
+
[*Note 2*: This can happen if the re-locking of the mutex throws an
|
| 2529 |
+
exception. — *end note*]
|
| 2530 |
+
|
| 2531 |
+
*Postconditions:* `lock.owns_lock()` is `true` and `lock.mutex()` is
|
| 2532 |
+
locked by the calling thread.
|
| 2533 |
|
| 2534 |
*Throws:* Nothing.
|
| 2535 |
|
| 2536 |
``` cpp
|
| 2537 |
template <class Predicate>
|
|
|
|
| 2552 |
while (!pred())
|
| 2553 |
wait(lock);
|
| 2554 |
```
|
| 2555 |
|
| 2556 |
*Remarks:* If the function fails to meet the postcondition,
|
| 2557 |
+
`terminate()` shall be called ([[except.terminate]]).
|
|
|
|
| 2558 |
|
| 2559 |
+
[*Note 3*: This can happen if the re-locking of the mutex throws an
|
| 2560 |
+
exception. — *end note*]
|
| 2561 |
|
| 2562 |
+
*Postconditions:* `lock.owns_lock()` is `true` and `lock.mutex()` is
|
| 2563 |
+
locked by the calling thread.
|
| 2564 |
+
|
| 2565 |
+
*Throws:* Any exception thrown by `pred`.
|
| 2566 |
|
| 2567 |
``` cpp
|
| 2568 |
template <class Clock, class Duration>
|
| 2569 |
cv_status wait_until(unique_lock<mutex>& lock,
|
| 2570 |
const chrono::time_point<Clock, Duration>& abs_time);
|
| 2571 |
```
|
| 2572 |
|
| 2573 |
+
*Requires:* `lock.owns_lock()` is `true` and `lock.mutex()` is locked by
|
| 2574 |
+
the calling thread, and either
|
| 2575 |
|
| 2576 |
- no other thread is waiting on this `condition_variable` object or
|
| 2577 |
- `lock.mutex()` returns the same value for each of the `lock` arguments
|
| 2578 |
supplied by all concurrently waiting (via `wait`, `wait_for`, or
|
| 2579 |
`wait_until`) threads.
|
|
|
|
| 2589 |
spuriously.
|
| 2590 |
- If the function exits via an exception, `lock.lock()` shall be called
|
| 2591 |
prior to exiting the function.
|
| 2592 |
|
| 2593 |
*Remarks:* If the function fails to meet the postcondition,
|
| 2594 |
+
`terminate()` shall be called ([[except.terminate]]).
|
|
|
|
| 2595 |
|
| 2596 |
+
[*Note 4*: This can happen if the re-locking of the mutex throws an
|
| 2597 |
+
exception. — *end note*]
|
| 2598 |
+
|
| 2599 |
+
*Postconditions:* `lock.owns_lock()` is `true` and `lock.mutex()` is
|
| 2600 |
+
locked by the calling thread.
|
| 2601 |
|
| 2602 |
*Returns:* `cv_status::timeout` if the absolute
|
| 2603 |
timeout ([[thread.req.timing]]) specified by `abs_time` expired,
|
| 2604 |
otherwise `cv_status::no_timeout`.
|
| 2605 |
|
|
|
|
| 2609 |
template <class Rep, class Period>
|
| 2610 |
cv_status wait_for(unique_lock<mutex>& lock,
|
| 2611 |
const chrono::duration<Rep, Period>& rel_time);
|
| 2612 |
```
|
| 2613 |
|
| 2614 |
+
*Requires:* `lock.owns_lock()` is `true` and `lock.mutex()` is locked by
|
| 2615 |
+
the calling thread, and either
|
| 2616 |
|
| 2617 |
- no other thread is waiting on this `condition_variable` object or
|
| 2618 |
- `lock.mutex()` returns the same value for each of the `lock` arguments
|
| 2619 |
supplied by all concurrently waiting (via `wait`, `wait_for`, or
|
| 2620 |
`wait_until`) threads.
|
|
|
|
| 2628 |
*Returns:* `cv_status::timeout` if the relative
|
| 2629 |
timeout ([[thread.req.timing]]) specified by `rel_time` expired,
|
| 2630 |
otherwise `cv_status::no_timeout`.
|
| 2631 |
|
| 2632 |
*Remarks:* If the function fails to meet the postcondition,
|
| 2633 |
+
`terminate()` shall be called ([[except.terminate]]).
|
|
|
|
| 2634 |
|
| 2635 |
+
[*Note 5*: This can happen if the re-locking of the mutex throws an
|
| 2636 |
+
exception. — *end note*]
|
| 2637 |
+
|
| 2638 |
+
*Postconditions:* `lock.owns_lock()` is `true` and `lock.mutex()` is
|
| 2639 |
+
locked by the calling thread.
|
| 2640 |
|
| 2641 |
*Throws:* Timeout-related exceptions ([[thread.req.timing]]).
|
| 2642 |
|
| 2643 |
``` cpp
|
| 2644 |
template <class Clock, class Duration, class Predicate>
|
|
|
|
| 2663 |
return pred();
|
| 2664 |
return true;
|
| 2665 |
```
|
| 2666 |
|
| 2667 |
*Remarks:* If the function fails to meet the postcondition,
|
| 2668 |
+
`terminate()` shall be called ([[except.terminate]]).
|
|
|
|
| 2669 |
|
| 2670 |
+
[*Note 6*: This can happen if the re-locking of the mutex throws an
|
| 2671 |
+
exception. — *end note*]
|
| 2672 |
|
| 2673 |
+
*Postconditions:* `lock.owns_lock()` is `true` and `lock.mutex()` is
|
| 2674 |
+
locked by the calling thread.
|
| 2675 |
+
|
| 2676 |
+
[*Note 7*: The returned value indicates whether the predicate evaluated
|
| 2677 |
+
to `true` regardless of whether the timeout was
|
| 2678 |
+
triggered. — *end note*]
|
| 2679 |
|
| 2680 |
*Throws:* Timeout-related exceptions ([[thread.req.timing]]) or any
|
| 2681 |
exception thrown by `pred`.
|
| 2682 |
|
| 2683 |
``` cpp
|
|
|
|
| 2685 |
bool wait_for(unique_lock<mutex>& lock,
|
| 2686 |
const chrono::duration<Rep, Period>& rel_time,
|
| 2687 |
Predicate pred);
|
| 2688 |
```
|
| 2689 |
|
| 2690 |
+
*Requires:* `lock.owns_lock()` is `true` and `lock.mutex()` is locked by
|
| 2691 |
+
the calling thread, and either
|
| 2692 |
|
| 2693 |
- no other thread is waiting on this `condition_variable` object or
|
| 2694 |
- `lock.mutex()` returns the same value for each of the `lock` arguments
|
| 2695 |
supplied by all concurrently waiting (via `wait`, `wait_for`, or
|
| 2696 |
`wait_until`) threads.
|
|
|
|
| 2699 |
|
| 2700 |
``` cpp
|
| 2701 |
return wait_until(lock, chrono::steady_clock::now() + rel_time, std::move(pred));
|
| 2702 |
```
|
| 2703 |
|
| 2704 |
+
[*Note 8*: There is no blocking if `pred()` is initially `true`, even
|
| 2705 |
+
if the timeout has already expired. — *end note*]
|
| 2706 |
|
| 2707 |
*Remarks:* If the function fails to meet the postcondition,
|
| 2708 |
+
`terminate()` shall be called ([[except.terminate]]).
|
|
|
|
| 2709 |
|
| 2710 |
+
[*Note 9*: This can happen if the re-locking of the mutex throws an
|
| 2711 |
+
exception. — *end note*]
|
| 2712 |
|
| 2713 |
+
*Postconditions:* `lock.owns_lock()` is `true` and `lock.mutex()` is
|
| 2714 |
+
locked by the calling thread.
|
| 2715 |
+
|
| 2716 |
+
[*Note 10*: The returned value indicates whether the predicate
|
| 2717 |
+
evaluates to `true` regardless of whether the timeout was
|
| 2718 |
+
triggered. — *end note*]
|
| 2719 |
|
| 2720 |
*Throws:* Timeout-related exceptions ([[thread.req.timing]]) or any
|
| 2721 |
exception thrown by `pred`.
|
| 2722 |
|
| 2723 |
### Class `condition_variable_any` <a id="thread.condition.condvarany">[[thread.condition.condvarany]]</a>
|
| 2724 |
|
| 2725 |
A `Lock` type shall meet the `BasicLockable` requirements (
|
| 2726 |
+
[[thread.req.lockable.basic]]).
|
| 2727 |
+
|
| 2728 |
+
[*Note 1*: All of the standard mutex types meet this requirement. If a
|
| 2729 |
+
`Lock` type other than one of the standard mutex types or a
|
| 2730 |
+
`unique_lock` wrapper for a standard mutex type is used with
|
| 2731 |
`condition_variable_any`, the user must ensure that any necessary
|
| 2732 |
synchronization is in place with respect to the predicate associated
|
| 2733 |
+
with the `condition_variable_any` instance. — *end note*]
|
| 2734 |
|
| 2735 |
``` cpp
|
| 2736 |
namespace std {
|
| 2737 |
class condition_variable_any {
|
| 2738 |
public:
|
|
|
|
| 2781 |
|
| 2782 |
``` cpp
|
| 2783 |
~condition_variable_any();
|
| 2784 |
```
|
| 2785 |
|
| 2786 |
+
*Requires:* There shall be no thread blocked on `*this`.
|
| 2787 |
+
|
| 2788 |
+
[*Note 1*: That is, all threads shall have been notified; they may
|
| 2789 |
+
subsequently block on the lock specified in the wait. This relaxes the
|
| 2790 |
+
usual rules, which would have required all wait calls to happen before
|
| 2791 |
+
destruction. Only the notification to unblock the wait must happen
|
| 2792 |
+
before destruction. The user must take care to ensure that no threads
|
| 2793 |
+
wait on `*this` once the destructor has been started, especially when
|
| 2794 |
+
the waiting threads are calling the wait functions in a loop or using
|
| 2795 |
+
the overloads of `wait`, `wait_for`, or `wait_until` that take a
|
| 2796 |
+
predicate. — *end note*]
|
| 2797 |
|
| 2798 |
*Effects:* Destroys the object.
|
| 2799 |
|
| 2800 |
``` cpp
|
| 2801 |
void notify_one() noexcept;
|
|
|
|
| 2813 |
``` cpp
|
| 2814 |
template <class Lock>
|
| 2815 |
void wait(Lock& lock);
|
| 2816 |
```
|
| 2817 |
|
| 2818 |
+
[*Note 2*: If any of the `wait` functions exits via an exception, it is
|
| 2819 |
unspecified whether the `Lock` is held. One can use a `Lock` type that
|
| 2820 |
+
allows to query that, such as the `unique_lock` wrapper. — *end note*]
|
| 2821 |
|
| 2822 |
*Effects:*
|
| 2823 |
|
| 2824 |
- Atomically calls `lock.unlock()` and blocks on `*this`.
|
| 2825 |
- When unblocked, calls `lock.lock()` (possibly blocking on the lock)
|
| 2826 |
and returns.
|
| 2827 |
- The function will unblock when signaled by a call to `notify_one()`, a
|
| 2828 |
call to `notify_all()`, or spuriously.
|
| 2829 |
|
| 2830 |
*Remarks:* If the function fails to meet the postcondition,
|
| 2831 |
+
`terminate()` shall be called ([[except.terminate]]).
|
|
|
|
| 2832 |
|
| 2833 |
+
[*Note 3*: This can happen if the re-locking of the mutex throws an
|
| 2834 |
+
exception. — *end note*]
|
| 2835 |
+
|
| 2836 |
+
*Postconditions:* `lock` is locked by the calling thread.
|
| 2837 |
|
| 2838 |
*Throws:* Nothing.
|
| 2839 |
|
| 2840 |
``` cpp
|
| 2841 |
template <class Lock, class Predicate>
|
|
|
|
| 2865 |
spuriously.
|
| 2866 |
- If the function exits via an exception, `lock.lock()` shall be called
|
| 2867 |
prior to exiting the function.
|
| 2868 |
|
| 2869 |
*Remarks:* If the function fails to meet the postcondition,
|
| 2870 |
+
`terminate()` shall be called ([[except.terminate]]).
|
|
|
|
| 2871 |
|
| 2872 |
+
[*Note 4*: This can happen if the re-locking of the mutex throws an
|
| 2873 |
+
exception. — *end note*]
|
| 2874 |
+
|
| 2875 |
+
*Postconditions:* `lock` is locked by the calling thread.
|
| 2876 |
|
| 2877 |
*Returns:* `cv_status::timeout` if the absolute
|
| 2878 |
timeout ([[thread.req.timing]]) specified by `abs_time` expired,
|
| 2879 |
otherwise `cv_status::no_timeout`.
|
| 2880 |
|
|
|
|
| 2894 |
*Returns:* `cv_status::timeout` if the relative
|
| 2895 |
timeout ([[thread.req.timing]]) specified by `rel_time` expired,
|
| 2896 |
otherwise `cv_status::no_timeout`.
|
| 2897 |
|
| 2898 |
*Remarks:* If the function fails to meet the postcondition,
|
| 2899 |
+
`terminate()` shall be called ([[except.terminate]]).
|
|
|
|
| 2900 |
|
| 2901 |
+
[*Note 5*: This can happen if the re-locking of the mutex throws an
|
| 2902 |
+
exception. — *end note*]
|
| 2903 |
+
|
| 2904 |
+
*Postconditions:* `lock` is locked by the calling thread.
|
| 2905 |
|
| 2906 |
*Throws:* Timeout-related exceptions ([[thread.req.timing]]).
|
| 2907 |
|
| 2908 |
``` cpp
|
| 2909 |
template <class Lock, class Clock, class Duration, class Predicate>
|
|
|
|
| 2917 |
if (wait_until(lock, abs_time) == cv_status::timeout)
|
| 2918 |
return pred();
|
| 2919 |
return true;
|
| 2920 |
```
|
| 2921 |
|
| 2922 |
+
[*Note 6*: There is no blocking if `pred()` is initially `true`, or if
|
| 2923 |
+
the timeout has already expired. — *end note*]
|
| 2924 |
|
| 2925 |
+
[*Note 7*: The returned value indicates whether the predicate evaluates
|
| 2926 |
+
to `true` regardless of whether the timeout was
|
| 2927 |
+
triggered. — *end note*]
|
| 2928 |
|
| 2929 |
``` cpp
|
| 2930 |
template <class Lock, class Rep, class Period, class Predicate>
|
| 2931 |
bool wait_for(Lock& lock, const chrono::duration<Rep, Period>& rel_time, Predicate pred);
|
| 2932 |
```
|
|
|
|
| 2941 |
|
| 2942 |
### Overview <a id="futures.overview">[[futures.overview]]</a>
|
| 2943 |
|
| 2944 |
[[futures]] describes components that a C++program can use to retrieve
|
| 2945 |
in one thread the result (value or exception) from a function that has
|
| 2946 |
+
run in the same thread or another thread.
|
| 2947 |
+
|
| 2948 |
+
[*Note 1*: These components are not restricted to multi-threaded
|
| 2949 |
+
programs but can be useful in single-threaded programs as
|
| 2950 |
+
well. — *end note*]
|
| 2951 |
+
|
| 2952 |
+
### Header `<future>` synopsis <a id="future.syn">[[future.syn]]</a>
|
| 2953 |
|
| 2954 |
``` cpp
|
| 2955 |
namespace std {
|
| 2956 |
enum class future_errc {
|
| 2957 |
broken_promise = implementation-defined,
|
|
|
|
| 2996 |
|
| 2997 |
template <class R> class shared_future;
|
| 2998 |
template <class R> class shared_future<R&>;
|
| 2999 |
template <> class shared_future<void>;
|
| 3000 |
|
| 3001 |
+
template <class> class packaged_task; // not defined
|
| 3002 |
template <class R, class... ArgTypes>
|
| 3003 |
class packaged_task<R(ArgTypes...)>;
|
| 3004 |
|
| 3005 |
template <class R, class... ArgTypes>
|
| 3006 |
void swap(packaged_task<R(ArgTypes...)>&, packaged_task<R(ArgTypes...)>&) noexcept;
|
| 3007 |
|
| 3008 |
template <class R, class Alloc>
|
| 3009 |
struct uses_allocator<packaged_task<R>, Alloc>;
|
| 3010 |
|
| 3011 |
template <class F, class... Args>
|
| 3012 |
+
future<invoke_result_t<decay_t<F>, decay_t<Args>...>>
|
| 3013 |
async(F&& f, Args&&... args);
|
| 3014 |
template <class F, class... Args>
|
| 3015 |
+
future<invoke_result_t<decay_t<F>, decay_t<Args>...>>
|
| 3016 |
async(launch policy, F&& f, Args&&... args);
|
| 3017 |
}
|
| 3018 |
```
|
| 3019 |
|
| 3020 |
The `enum` type `launch` is a bitmask type ([[bitmask.types]]) with
|
| 3021 |
+
elements `launch::async` and `launch::deferred`.
|
| 3022 |
+
|
| 3023 |
+
[*Note 1*: Implementations can provide bitmasks to specify restrictions
|
| 3024 |
+
on task interaction by functions launched by `async()` applicable to a
|
| 3025 |
corresponding subset of available launch policies. Implementations can
|
| 3026 |
extend the behavior of the first overload of `async()` by adding their
|
| 3027 |
+
extensions to the launch policy under the “as if” rule. — *end note*]
|
| 3028 |
|
| 3029 |
The enum values of `future_errc` are distinct and not zero.
|
| 3030 |
|
| 3031 |
### Error handling <a id="futures.errors">[[futures.errors]]</a>
|
| 3032 |
|
|
|
|
| 3057 |
|
| 3058 |
``` cpp
|
| 3059 |
namespace std {
|
| 3060 |
class future_error : public logic_error {
|
| 3061 |
public:
|
| 3062 |
+
explicit future_error(future_errc e);
|
| 3063 |
|
| 3064 |
const error_code& code() const noexcept;
|
| 3065 |
const char* what() const noexcept;
|
| 3066 |
+
private:
|
| 3067 |
+
error_code ec_; // exposition only
|
| 3068 |
};
|
| 3069 |
}
|
| 3070 |
```
|
| 3071 |
|
| 3072 |
+
``` cpp
|
| 3073 |
+
explicit future_error(future_errc e);
|
| 3074 |
+
```
|
| 3075 |
+
|
| 3076 |
+
*Effects:* Constructs an object of class `future_error` and initializes
|
| 3077 |
+
`ec_` with `make_error_code(e)`.
|
| 3078 |
+
|
| 3079 |
``` cpp
|
| 3080 |
const error_code& code() const noexcept;
|
| 3081 |
```
|
| 3082 |
|
| 3083 |
+
*Returns:* `ec_`.
|
|
|
|
| 3084 |
|
| 3085 |
``` cpp
|
| 3086 |
const char* what() const noexcept;
|
| 3087 |
```
|
| 3088 |
|
| 3089 |
*Returns:* An NTBSincorporating `code().message()`.
|
| 3090 |
|
| 3091 |
### Shared state <a id="futures.state">[[futures.state]]</a>
|
| 3092 |
|
| 3093 |
+
Many of the classes introduced in this subclause use some state to
|
| 3094 |
communicate results. This *shared state* consists of some state
|
| 3095 |
information and some (possibly not yet evaluated) *result*, which can be
|
| 3096 |
+
a (possibly void) value or an exception.
|
|
|
|
| 3097 |
|
| 3098 |
+
[*Note 1*: Futures, promises, and tasks defined in this clause
|
| 3099 |
+
reference such shared state. — *end note*]
|
| 3100 |
+
|
| 3101 |
+
[*Note 2*: The result can be any kind of object including a function to
|
| 3102 |
+
compute that result, as used by `async` when `policy` is
|
| 3103 |
+
`launch::deferred`. — *end note*]
|
| 3104 |
|
| 3105 |
An *asynchronous return object* is an object that reads results from a
|
| 3106 |
shared state. A *waiting function* of an asynchronous return object is
|
| 3107 |
one that potentially blocks to wait for the shared state to be made
|
| 3108 |
ready. If a waiting function can return before the state is made ready
|
| 3109 |
because of a timeout ([[thread.req.lockable]]), then it is a *timed
|
| 3110 |
waiting function*, otherwise it is a *non-timed waiting function*.
|
| 3111 |
|
| 3112 |
An *asynchronous provider* is an object that provides a result to a
|
| 3113 |
shared state. The result of a shared state is set by respective
|
| 3114 |
+
functions on the asynchronous provider.
|
| 3115 |
+
|
| 3116 |
+
[*Note 3*: Such as promises or tasks. — *end note*]
|
| 3117 |
+
|
| 3118 |
+
The means of setting the result of a shared state is specified in the
|
| 3119 |
description of those classes and functions that create such a state
|
| 3120 |
object.
|
| 3121 |
|
| 3122 |
When an asynchronous return object or an asynchronous provider is said
|
| 3123 |
to release its shared state, it means:
|
|
|
|
| 3164 |
of each of that thread’s objects with thread storage duration (
|
| 3165 |
[[basic.stc.thread]]) is sequenced before making that shared state
|
| 3166 |
ready.
|
| 3167 |
|
| 3168 |
Access to the result of the same shared state may conflict (
|
| 3169 |
+
[[intro.multithread]]).
|
| 3170 |
+
|
| 3171 |
+
[*Note 4*: This explicitly specifies that the result of the shared
|
| 3172 |
+
state is visible in the objects that reference this state in the sense
|
| 3173 |
+
of data race avoidance ([[res.on.data.races]]). For example, concurrent
|
| 3174 |
+
accesses through references returned by `shared_future::get()` (
|
| 3175 |
+
[[futures.shared_future]]) must either use read-only operations or
|
| 3176 |
+
provide additional synchronization. — *end note*]
|
| 3177 |
|
| 3178 |
### Class template `promise` <a id="futures.promise">[[futures.promise]]</a>
|
| 3179 |
|
| 3180 |
``` cpp
|
| 3181 |
namespace std {
|
|
|
|
| 3200 |
// setting the result
|
| 3201 |
void set_value(see below);
|
| 3202 |
void set_exception(exception_ptr p);
|
| 3203 |
|
| 3204 |
// setting the result with deferred notification
|
|
|
|
| 3205 |
void set_value_at_thread_exit(see below);
|
| 3206 |
void set_exception_at_thread_exit(exception_ptr p);
|
| 3207 |
};
|
| 3208 |
template <class R>
|
| 3209 |
void swap(promise<R>& x, promise<R>& y) noexcept;
|
|
|
|
| 3212 |
}
|
| 3213 |
```
|
| 3214 |
|
| 3215 |
The implementation shall provide the template `promise` and two
|
| 3216 |
specializations, `promise<R&>` and `promise<{}void>`. These differ only
|
| 3217 |
+
in the argument type of the member functions `set_value` and
|
| 3218 |
+
`set_value_at_thread_exit`, as set out in their descriptions, below.
|
| 3219 |
|
| 3220 |
The `set_value`, `set_exception`, `set_value_at_thread_exit`, and
|
| 3221 |
`set_exception_at_thread_exit` member functions behave as though they
|
| 3222 |
acquire a single mutex associated with the promise object while updating
|
| 3223 |
the promise object.
|
|
|
|
| 3245 |
```
|
| 3246 |
|
| 3247 |
*Effects:* constructs a new `promise` object and transfers ownership of
|
| 3248 |
the shared state of `rhs` (if any) to the newly-constructed object.
|
| 3249 |
|
| 3250 |
+
*Postconditions:* `rhs` has no shared state.
|
| 3251 |
|
| 3252 |
``` cpp
|
| 3253 |
~promise();
|
| 3254 |
```
|
| 3255 |
|
|
|
|
| 3268 |
void swap(promise& other) noexcept;
|
| 3269 |
```
|
| 3270 |
|
| 3271 |
*Effects:* Exchanges the shared state of `*this` and `other`.
|
| 3272 |
|
| 3273 |
+
*Postconditions:* `*this` has the shared state (if any) that `other` had
|
| 3274 |
+
prior to the call to `swap`. `other` has the shared state (if any) that
|
| 3275 |
+
`*this` had prior to the call to `swap`.
|
| 3276 |
|
| 3277 |
``` cpp
|
| 3278 |
future<R> get_future();
|
| 3279 |
```
|
| 3280 |
|
|
|
|
| 3295 |
void promise::set_value(R&& r);
|
| 3296 |
void promise<R&>::set_value(R& r);
|
| 3297 |
void promise<void>::set_value();
|
| 3298 |
```
|
| 3299 |
|
| 3300 |
+
*Effects:* Atomically stores the value `r` in the shared state and makes
|
| 3301 |
that state ready ([[futures.state]]).
|
| 3302 |
|
| 3303 |
*Throws:*
|
| 3304 |
|
| 3305 |
- `future_error` if its shared state already has a stored value or
|
|
|
|
| 3317 |
|
| 3318 |
``` cpp
|
| 3319 |
void set_exception(exception_ptr p);
|
| 3320 |
```
|
| 3321 |
|
| 3322 |
+
*Requires:* `p` is not null.
|
| 3323 |
+
|
| 3324 |
+
*Effects:* Atomically stores the exception pointer `p` in the shared
|
| 3325 |
state and makes that state ready ([[futures.state]]).
|
| 3326 |
|
| 3327 |
*Throws:* `future_error` if its shared state already has a stored value
|
| 3328 |
or exception.
|
| 3329 |
|
|
|
|
| 3362 |
|
| 3363 |
``` cpp
|
| 3364 |
void set_exception_at_thread_exit(exception_ptr p);
|
| 3365 |
```
|
| 3366 |
|
| 3367 |
+
*Requires:* `p` is not null.
|
| 3368 |
+
|
| 3369 |
*Effects:* Stores the exception pointer `p` in the shared state without
|
| 3370 |
making that state ready immediately. Schedules that state to be made
|
| 3371 |
ready when the current thread exits, after all objects of thread storage
|
| 3372 |
duration associated with the current thread have been destroyed.
|
| 3373 |
|
|
|
|
| 3379 |
value or exception.
|
| 3380 |
- `no_state` if `*this` has no shared state.
|
| 3381 |
|
| 3382 |
``` cpp
|
| 3383 |
template <class R>
|
| 3384 |
+
void swap(promise<R>& x, promise<R>& y) noexcept;
|
| 3385 |
```
|
| 3386 |
|
| 3387 |
+
*Effects:* As if by `x.swap(y)`.
|
| 3388 |
|
| 3389 |
### Class template `future` <a id="futures.unique_future">[[futures.unique_future]]</a>
|
| 3390 |
|
| 3391 |
The class template `future` defines a type for asynchronous return
|
| 3392 |
objects which do not share their shared state with other asynchronous
|
|
|
|
| 3396 |
constructor and shares its shared state with the original asynchronous
|
| 3397 |
provider. The result (value or exception) of a `future` object can be
|
| 3398 |
set by calling a respective function on an object that shares the same
|
| 3399 |
shared state.
|
| 3400 |
|
| 3401 |
+
[*Note 1*: Member functions of `future` do not synchronize with
|
| 3402 |
+
themselves or with member functions of `shared_future`. — *end note*]
|
| 3403 |
|
| 3404 |
The effect of calling any member function other than the destructor, the
|
| 3405 |
+
move-assignment operator, `share`, or `valid` on a `future` object for
|
| 3406 |
+
which `valid() == false` is undefined.
|
| 3407 |
+
|
| 3408 |
+
[*Note 2*: It is valid to move from a future object for which
|
| 3409 |
+
`valid() == false`. — *end note*]
|
| 3410 |
+
|
| 3411 |
+
[*Note 3*: Implementations are encouraged to detect this case and throw
|
| 3412 |
+
an object of type `future_error` with an error condition of
|
| 3413 |
+
`future_errc::no_state`. — *end note*]
|
| 3414 |
|
| 3415 |
``` cpp
|
| 3416 |
namespace std {
|
| 3417 |
template <class R>
|
| 3418 |
class future {
|
|
|
|
| 3421 |
future(future&&) noexcept;
|
| 3422 |
future(const future& rhs) = delete;
|
| 3423 |
~future();
|
| 3424 |
future& operator=(const future& rhs) = delete;
|
| 3425 |
future& operator=(future&&) noexcept;
|
| 3426 |
+
shared_future<R> share() noexcept;
|
| 3427 |
|
| 3428 |
// retrieving the value
|
| 3429 |
see below get();
|
| 3430 |
|
| 3431 |
// functions to check state
|
|
|
|
| 3447 |
|
| 3448 |
``` cpp
|
| 3449 |
future() noexcept;
|
| 3450 |
```
|
| 3451 |
|
| 3452 |
+
*Effects:* Constructs an *empty* `future` object that does not refer to
|
| 3453 |
a shared state.
|
| 3454 |
|
| 3455 |
+
*Postconditions:* `valid() == false`.
|
| 3456 |
|
| 3457 |
``` cpp
|
| 3458 |
future(future&& rhs) noexcept;
|
| 3459 |
```
|
| 3460 |
|
| 3461 |
+
*Effects:* Move constructs a `future` object that refers to the shared
|
| 3462 |
state that was originally referred to by `rhs` (if any).
|
| 3463 |
|
| 3464 |
*Postconditions:*
|
| 3465 |
|
| 3466 |
- `valid()` returns the same value as `rhs.valid()` prior to the
|
|
|
|
| 3471 |
~future();
|
| 3472 |
```
|
| 3473 |
|
| 3474 |
*Effects:*
|
| 3475 |
|
| 3476 |
+
- Releases any shared state ([[futures.state]]);
|
| 3477 |
- destroys `*this`.
|
| 3478 |
|
| 3479 |
``` cpp
|
| 3480 |
future& operator=(future&& rhs) noexcept;
|
| 3481 |
```
|
| 3482 |
|
| 3483 |
*Effects:*
|
| 3484 |
|
| 3485 |
+
- Releases any shared state ([[futures.state]]).
|
| 3486 |
- move assigns the contents of `rhs` to `*this`.
|
| 3487 |
|
| 3488 |
*Postconditions:*
|
| 3489 |
|
| 3490 |
- `valid()` returns the same value as `rhs.valid()` prior to the
|
| 3491 |
assignment.
|
| 3492 |
- `rhs.valid() == false`.
|
| 3493 |
|
| 3494 |
``` cpp
|
| 3495 |
+
shared_future<R> share() noexcept;
|
| 3496 |
```
|
| 3497 |
|
| 3498 |
*Returns:* `shared_future<R>(std::move(*this))`.
|
| 3499 |
|
| 3500 |
+
*Postconditions:* `valid() == false`.
|
| 3501 |
|
| 3502 |
``` cpp
|
| 3503 |
R future::get();
|
| 3504 |
R& future<R&>::get();
|
| 3505 |
void future<void>::get();
|
| 3506 |
```
|
| 3507 |
|
| 3508 |
+
[*Note 1*: As described above, the template and its two required
|
| 3509 |
specializations differ only in the return type and return value of the
|
| 3510 |
+
member function `get`. — *end note*]
|
| 3511 |
|
| 3512 |
+
*Effects:*
|
| 3513 |
+
|
| 3514 |
+
- `wait()`s until the shared state is ready, then retrieves the value
|
| 3515 |
+
stored in the shared state;
|
| 3516 |
+
- releases any shared state ([[futures.state]]).
|
| 3517 |
|
| 3518 |
*Returns:*
|
| 3519 |
|
| 3520 |
- `future::get()` returns the value `v` stored in the object’s shared
|
| 3521 |
state as `std::move(v)`.
|
|
|
|
| 3524 |
- `future<void>::get()` returns nothing.
|
| 3525 |
|
| 3526 |
*Throws:* the stored exception, if an exception was stored in the shared
|
| 3527 |
state.
|
| 3528 |
|
| 3529 |
+
*Postconditions:* `valid() == false`.
|
| 3530 |
|
| 3531 |
``` cpp
|
| 3532 |
bool valid() const noexcept;
|
| 3533 |
```
|
| 3534 |
|
|
|
|
| 3536 |
|
| 3537 |
``` cpp
|
| 3538 |
void wait() const;
|
| 3539 |
```
|
| 3540 |
|
| 3541 |
+
*Effects:* Blocks until the shared state is ready.
|
| 3542 |
|
| 3543 |
``` cpp
|
| 3544 |
template <class Rep, class Period>
|
| 3545 |
future_status wait_for(const chrono::duration<Rep, Period>& rel_time) const;
|
| 3546 |
```
|
| 3547 |
|
| 3548 |
+
*Effects:* None if the shared state contains a deferred
|
| 3549 |
function ([[futures.async]]), otherwise blocks until the shared state
|
| 3550 |
is ready or until the relative timeout ([[thread.req.timing]])
|
| 3551 |
specified by `rel_time` has expired.
|
| 3552 |
|
| 3553 |
*Returns:*
|
|
|
|
| 3564 |
``` cpp
|
| 3565 |
template <class Clock, class Duration>
|
| 3566 |
future_status wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
|
| 3567 |
```
|
| 3568 |
|
| 3569 |
+
*Effects:* None if the shared state contains a deferred
|
| 3570 |
function ([[futures.async]]), otherwise blocks until the shared state
|
| 3571 |
is ready or until the absolute timeout ([[thread.req.timing]])
|
| 3572 |
specified by `abs_time` has expired.
|
| 3573 |
|
| 3574 |
*Returns:*
|
|
|
|
| 3592 |
shared state with the original asynchronous provider (
|
| 3593 |
[[futures.state]]) of the shared state. The result (value or exception)
|
| 3594 |
of a `shared_future` object can be set by calling a respective function
|
| 3595 |
on an object that shares the same shared state.
|
| 3596 |
|
| 3597 |
+
[*Note 1*: Member functions of `shared_future` do not synchronize with
|
| 3598 |
+
themselves, but they synchronize with the shared state. — *end note*]
|
| 3599 |
|
| 3600 |
The effect of calling any member function other than the destructor, the
|
| 3601 |
+
move-assignment operator, the copy-assignment operator, or `valid()` on
|
| 3602 |
+
a `shared_future` object for which `valid() == false` is undefined.
|
| 3603 |
+
|
| 3604 |
+
[*Note 2*: It is valid to copy or move from a `shared_future` object
|
| 3605 |
+
for which `valid()` is `false`. — *end note*]
|
| 3606 |
+
|
| 3607 |
+
[*Note 3*: Implementations are encouraged to detect this case and throw
|
| 3608 |
+
an object of type `future_error` with an error condition of
|
| 3609 |
+
`future_errc::no_state`. — *end note*]
|
| 3610 |
|
| 3611 |
``` cpp
|
| 3612 |
namespace std {
|
| 3613 |
template <class R>
|
| 3614 |
class shared_future {
|
| 3615 |
public:
|
| 3616 |
shared_future() noexcept;
|
| 3617 |
+
shared_future(const shared_future& rhs) noexcept;
|
| 3618 |
shared_future(future<R>&&) noexcept;
|
| 3619 |
shared_future(shared_future&& rhs) noexcept;
|
| 3620 |
~shared_future();
|
| 3621 |
+
shared_future& operator=(const shared_future& rhs) noexcept;
|
| 3622 |
shared_future& operator=(shared_future&& rhs) noexcept;
|
| 3623 |
|
| 3624 |
// retrieving the value
|
| 3625 |
see below get() const;
|
| 3626 |
|
|
|
|
| 3643 |
|
| 3644 |
``` cpp
|
| 3645 |
shared_future() noexcept;
|
| 3646 |
```
|
| 3647 |
|
| 3648 |
+
*Effects:* Constructs an *empty* `shared_future` object that does not
|
| 3649 |
refer to a shared state.
|
| 3650 |
|
| 3651 |
+
*Postconditions:* `valid() == false`.
|
| 3652 |
|
| 3653 |
``` cpp
|
| 3654 |
+
shared_future(const shared_future& rhs) noexcept;
|
| 3655 |
```
|
| 3656 |
|
| 3657 |
+
*Effects:* Constructs a `shared_future` object that refers to the same
|
| 3658 |
shared state as `rhs` (if any).
|
| 3659 |
|
| 3660 |
+
*Postconditions:* `valid()` returns the same value as `rhs.valid()`.
|
| 3661 |
|
| 3662 |
``` cpp
|
| 3663 |
shared_future(future<R>&& rhs) noexcept;
|
| 3664 |
shared_future(shared_future&& rhs) noexcept;
|
| 3665 |
```
|
| 3666 |
|
| 3667 |
+
*Effects:* Move constructs a `shared_future` object that refers to the
|
| 3668 |
shared state that was originally referred to by `rhs` (if any).
|
| 3669 |
|
| 3670 |
*Postconditions:*
|
| 3671 |
|
| 3672 |
- `valid()` returns the same value as `rhs.valid()` returned prior to
|
|
|
|
| 3677 |
~shared_future();
|
| 3678 |
```
|
| 3679 |
|
| 3680 |
*Effects:*
|
| 3681 |
|
| 3682 |
+
- Releases any shared state ([[futures.state]]);
|
| 3683 |
- destroys `*this`.
|
| 3684 |
|
| 3685 |
``` cpp
|
| 3686 |
shared_future& operator=(shared_future&& rhs) noexcept;
|
| 3687 |
```
|
| 3688 |
|
| 3689 |
*Effects:*
|
| 3690 |
|
| 3691 |
+
- Releases any shared state ([[futures.state]]);
|
| 3692 |
- move assigns the contents of `rhs` to `*this`.
|
| 3693 |
|
| 3694 |
*Postconditions:*
|
| 3695 |
|
| 3696 |
- `valid()` returns the same value as `rhs.valid()` returned prior to
|
| 3697 |
the assignment.
|
| 3698 |
- `rhs.valid() == false`.
|
| 3699 |
|
| 3700 |
``` cpp
|
| 3701 |
+
shared_future& operator=(const shared_future& rhs) noexcept;
|
| 3702 |
```
|
| 3703 |
|
| 3704 |
*Effects:*
|
| 3705 |
|
| 3706 |
+
- Releases any shared state ([[futures.state]]);
|
| 3707 |
+
- assigns the contents of `rhs` to `*this`. \[*Note 4*: As a result,
|
| 3708 |
+
`*this` refers to the same shared state as `rhs` (if
|
| 3709 |
+
any). — *end note*]
|
| 3710 |
|
| 3711 |
*Postconditions:* `valid() == rhs.valid()`.
|
| 3712 |
|
| 3713 |
``` cpp
|
| 3714 |
const R& shared_future::get() const;
|
| 3715 |
R& shared_future<R&>::get() const;
|
| 3716 |
void shared_future<void>::get() const;
|
| 3717 |
```
|
| 3718 |
|
| 3719 |
+
[*Note 1*: As described above, the template and its two required
|
| 3720 |
specializations differ only in the return type and return value of the
|
| 3721 |
+
member function `get`. — *end note*]
|
| 3722 |
|
| 3723 |
+
[*Note 2*: Access to a value object stored in the shared state is
|
| 3724 |
unsynchronized, so programmers should apply only those operations on `R`
|
| 3725 |
+
that do not introduce a data
|
| 3726 |
+
race ([[intro.multithread]]). — *end note*]
|
| 3727 |
|
| 3728 |
*Effects:* `wait()`s until the shared state is ready, then retrieves the
|
| 3729 |
value stored in the shared state.
|
| 3730 |
|
| 3731 |
*Returns:*
|
| 3732 |
|
| 3733 |
- `shared_future::get()` returns a const reference to the value stored
|
| 3734 |
+
in the object’s shared state. \[*Note 5*: Access through that
|
| 3735 |
+
reference after the shared state has been destroyed produces undefined
|
| 3736 |
+
behavior; this can be avoided by not storing the reference in any
|
| 3737 |
+
storage with a greater lifetime than the `shared_future` object that
|
| 3738 |
+
returned the reference. — *end note*]
|
| 3739 |
- `shared_future<R&>::get()` returns the reference stored as value in
|
| 3740 |
the object’s shared state.
|
| 3741 |
- `shared_future<void>::get()` returns nothing.
|
| 3742 |
|
| 3743 |
*Throws:* the stored exception, if an exception was stored in the shared
|
|
|
|
| 3751 |
|
| 3752 |
``` cpp
|
| 3753 |
void wait() const;
|
| 3754 |
```
|
| 3755 |
|
| 3756 |
+
*Effects:* Blocks until the shared state is ready.
|
| 3757 |
|
| 3758 |
``` cpp
|
| 3759 |
template <class Rep, class Period>
|
| 3760 |
future_status wait_for(const chrono::duration<Rep, Period>& rel_time) const;
|
| 3761 |
```
|
| 3762 |
|
| 3763 |
+
*Effects:* None if the shared state contains a deferred
|
| 3764 |
function ([[futures.async]]), otherwise blocks until the shared state
|
| 3765 |
is ready or until the relative timeout ([[thread.req.timing]])
|
| 3766 |
specified by `rel_time` has expired.
|
| 3767 |
|
| 3768 |
*Returns:*
|
|
|
|
| 3779 |
``` cpp
|
| 3780 |
template <class Clock, class Duration>
|
| 3781 |
future_status wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
|
| 3782 |
```
|
| 3783 |
|
| 3784 |
+
*Effects:* None if the shared state contains a deferred
|
| 3785 |
function ([[futures.async]]), otherwise blocks until the shared state
|
| 3786 |
is ready or until the absolute timeout ([[thread.req.timing]])
|
| 3787 |
specified by `abs_time` has expired.
|
| 3788 |
|
| 3789 |
*Returns:*
|
|
|
|
| 3803 |
potentially in a new thread and provides the result of the function in a
|
| 3804 |
`future` object with which it shares a shared state.
|
| 3805 |
|
| 3806 |
``` cpp
|
| 3807 |
template <class F, class... Args>
|
| 3808 |
+
future<invoke_result_t<decay_t<F>, decay_t<Args>...>>
|
| 3809 |
+
async(F&& f, Args&&... args);
|
| 3810 |
template <class F, class... Args>
|
| 3811 |
+
future<invoke_result_t<decay_t<F>, decay_t<Args>...>>
|
| 3812 |
+
async(launch policy, F&& f, Args&&... args);
|
| 3813 |
```
|
| 3814 |
|
| 3815 |
*Requires:* `F` and each `Ti` in `Args` shall satisfy the
|
| 3816 |
+
`MoveConstructible` requirements, and
|
| 3817 |
+
|
| 3818 |
+
``` cpp
|
| 3819 |
+
INVOKE(DECAY_COPY(std::forward<F>(f)),
|
| 3820 |
+
DECAY_COPY(std::forward<Args>(args))...) // see [func.require] [thread.thread.constr]
|
| 3821 |
+
```
|
| 3822 |
+
|
| 3823 |
+
shall be a valid expression.
|
| 3824 |
|
| 3825 |
*Effects:* The first function behaves the same as a call to the second
|
| 3826 |
function with a `policy` argument of `launch::async | launch::deferred`
|
| 3827 |
and the same arguments for `F` and `Args`. The second function creates a
|
| 3828 |
shared state that is associated with the returned `future` object. The
|
| 3829 |
further behavior of the second function depends on the `policy` argument
|
| 3830 |
as follows (if more than one of these conditions applies, the
|
| 3831 |
implementation may choose any of the corresponding policies):
|
| 3832 |
|
| 3833 |
+
- If `launch::async` is set in `policy`, calls
|
| 3834 |
+
*INVOKE*(*DECAY_COPY*(std::forward\<F\>(f)),
|
| 3835 |
+
*DECAY_COPY*(std::forward\<Args\>(args))...) ([[func.require]],
|
| 3836 |
[[thread.thread.constr]]) as if in a new thread of execution
|
| 3837 |
+
represented by a `thread` object with the calls to *DECAY_COPY*()
|
| 3838 |
being evaluated in the thread that called `async`. Any return value is
|
| 3839 |
stored as the result in the shared state. Any exception propagated
|
| 3840 |
+
from the execution of *INVOKE*(*DECAY_COPY*(std::forward\<F\>(f)),
|
| 3841 |
+
*DECAY_COPY*(std::forward\<Args\>(args))...) is stored as the
|
| 3842 |
+
exceptional result in the shared state. The `thread` object is stored
|
| 3843 |
+
in the shared state and affects the behavior of any asynchronous
|
| 3844 |
+
return objects that reference that state.
|
| 3845 |
+
- If `launch::deferred` is set in `policy`, stores
|
| 3846 |
+
*DECAY_COPY*(std::forward\<F\>(f)) and
|
| 3847 |
+
*DECAY_COPY*(std::forward\<Args\>(args))... in the shared state. These
|
| 3848 |
+
copies of `f` and `args` constitute a *deferred function*. Invocation
|
| 3849 |
+
of the deferred function evaluates *INVOKE*(std::move(g),
|
| 3850 |
+
std::move(xyz)) where `g` is the stored value of
|
| 3851 |
+
*DECAY_COPY*(std::forward\<F\>(f)) and `xyz` is the stored copy of
|
| 3852 |
+
*DECAY_COPY*(std::forward\<Args\>(args)).... Any return value is
|
| 3853 |
+
stored as the result in the shared state. Any exception propagated
|
| 3854 |
+
from the execution of the deferred function is stored as the
|
| 3855 |
+
exceptional result in the shared state. The shared state is not made
|
| 3856 |
+
ready until the function has completed. The first call to a non-timed
|
| 3857 |
+
waiting function ([[futures.state]]) on an asynchronous return object
|
| 3858 |
+
referring to this shared state shall invoke the deferred function in
|
| 3859 |
+
the thread that called the waiting function. Once evaluation of
|
| 3860 |
+
*INVOKE*(std::move(g), std::move(xyz)) begins, the function is no
|
| 3861 |
+
longer considered deferred. \[*Note 1*: If this policy is specified
|
| 3862 |
together with other policies, such as when using a `policy` value of
|
| 3863 |
`launch::async | launch::deferred`, implementations should defer
|
| 3864 |
invocation or the selection of the policy when no more concurrency can
|
| 3865 |
+
be effectively exploited. — *end note*]
|
| 3866 |
- If no value is set in the launch policy, or a value is set that is
|
| 3867 |
+
neither specified in this International Standard nor by the
|
| 3868 |
+
implementation, the behavior is undefined.
|
| 3869 |
|
| 3870 |
*Returns:* An object of type
|
| 3871 |
+
`future<invoke_result_t<decay_t<F>, decay_t<Args>...>``>` that refers to
|
| 3872 |
+
the shared state created by this call to `async`.
|
| 3873 |
+
|
| 3874 |
+
[*Note 1*: If a future obtained from `async` is moved outside the local
|
| 3875 |
+
scope, other code that uses the future must be aware that the future’s
|
| 3876 |
+
destructor may block for the shared state to become
|
| 3877 |
+
ready. — *end note*]
|
| 3878 |
|
| 3879 |
*Synchronization:* Regardless of the provided `policy` argument,
|
| 3880 |
|
| 3881 |
- the invocation of `async` synchronizes with ([[intro.multithread]])
|
| 3882 |
+
the invocation of `f`. \[*Note 2*: This statement applies even when
|
| 3883 |
+
the corresponding `future` object is moved to another
|
| 3884 |
+
thread. — *end note*] ; and
|
| 3885 |
- the completion of the function `f` is sequenced
|
| 3886 |
+
before ([[intro.multithread]]) the shared state is made ready.
|
| 3887 |
+
\[*Note 3*: `f` might not be called at all, so its completion might
|
| 3888 |
+
never happen. — *end note*]
|
| 3889 |
|
| 3890 |
If the implementation chooses the `launch::async` policy,
|
| 3891 |
|
| 3892 |
- a call to a waiting function on an asynchronous return object that
|
| 3893 |
shares the shared state created by this `async` call shall block until
|
|
|
|
| 3898 |
successfully detects the ready status of the shared state or with the
|
| 3899 |
return from the last function that releases the shared state,
|
| 3900 |
whichever happens first.
|
| 3901 |
|
| 3902 |
*Throws:* `system_error` if `policy == launch::async` and the
|
| 3903 |
+
implementation is unable to start a new thread, or `std::bad_alloc` if
|
| 3904 |
+
memory for the internal data structures could not be allocated.
|
| 3905 |
|
| 3906 |
*Error conditions:*
|
| 3907 |
|
| 3908 |
- `resource_unavailable_try_again` — if `policy == launch::async` and
|
| 3909 |
the system is unable to start a new thread.
|
| 3910 |
|
| 3911 |
+
[*Note 4*: Line \#1 might not result in concurrency because the `async`
|
| 3912 |
+
call uses the default policy, which may use `launch::deferred`, in which
|
| 3913 |
+
case the lambda might not be invoked until the `get()` call; in that
|
| 3914 |
+
case, `work1` and `work2` are called on the same thread and there is no
|
| 3915 |
+
concurrency. — *end note*]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3916 |
|
| 3917 |
### Class template `packaged_task` <a id="futures.task">[[futures.task]]</a>
|
| 3918 |
|
| 3919 |
The class template `packaged_task` defines a type for wrapping a
|
| 3920 |
function or callable object so that the return value of the function or
|
|
|
|
| 3925 |
state. Any futures that share the shared state will then be able to
|
| 3926 |
access the stored result.
|
| 3927 |
|
| 3928 |
``` cpp
|
| 3929 |
namespace std {
|
| 3930 |
+
template<class> class packaged_task; // not defined
|
| 3931 |
|
| 3932 |
template<class R, class... ArgTypes>
|
| 3933 |
class packaged_task<R(ArgTypes...)> {
|
| 3934 |
public:
|
| 3935 |
// construction and destruction
|
| 3936 |
packaged_task() noexcept;
|
| 3937 |
template <class F>
|
| 3938 |
explicit packaged_task(F&& f);
|
|
|
|
|
|
|
| 3939 |
~packaged_task();
|
| 3940 |
|
| 3941 |
// no copy
|
| 3942 |
packaged_task(const packaged_task&) = delete;
|
| 3943 |
packaged_task& operator=(const packaged_task&) = delete;
|
|
|
|
| 3969 |
|
| 3970 |
``` cpp
|
| 3971 |
packaged_task() noexcept;
|
| 3972 |
```
|
| 3973 |
|
| 3974 |
+
*Effects:* Constructs a `packaged_task` object with no shared state and
|
| 3975 |
no stored task.
|
| 3976 |
|
| 3977 |
``` cpp
|
| 3978 |
template <class F>
|
| 3979 |
packaged_task(F&& f);
|
|
|
|
|
|
|
| 3980 |
```
|
| 3981 |
|
| 3982 |
+
*Requires:* *INVOKE*\<R\>(f, t1, t2, ..., tN), where `t1, t2, ..., tN`
|
| 3983 |
are values of the corresponding types in `ArgTypes...`, shall be a valid
|
| 3984 |
expression. Invoking a copy of `f` shall behave the same as invoking
|
| 3985 |
`f`.
|
| 3986 |
|
| 3987 |
+
*Remarks:* This constructor shall not participate in overload resolution
|
| 3988 |
+
if `decay_t<F>` is the same type as `packaged_task<R(ArgTypes...)>`.
|
|
|
|
| 3989 |
|
| 3990 |
+
*Effects:* Constructs a new `packaged_task` object with a shared state
|
| 3991 |
+
and initializes the object’s stored task with `std::forward<F>(f)`.
|
|
|
|
|
|
|
| 3992 |
|
| 3993 |
+
*Throws:*
|
| 3994 |
+
|
| 3995 |
+
- Any exceptions thrown by the copy or move constructor of `f`.
|
| 3996 |
+
- For the first version, `bad_alloc` if memory for the internal data
|
| 3997 |
+
structures could not be allocated.
|
| 3998 |
+
- For the second version, any exceptions thrown by
|
| 3999 |
+
`allocator_traits<Allocator>::template`
|
| 4000 |
+
`rebind_traits<`*`unspecified`*`>::allocate`.
|
| 4001 |
|
| 4002 |
``` cpp
|
| 4003 |
packaged_task(packaged_task&& rhs) noexcept;
|
| 4004 |
```
|
| 4005 |
|
| 4006 |
+
*Effects:* Constructs a new `packaged_task` object and transfers
|
| 4007 |
ownership of `rhs`’s shared state to `*this`, leaving `rhs` with no
|
| 4008 |
shared state. Moves the stored task from `rhs` to `*this`.
|
| 4009 |
|
| 4010 |
+
*Postconditions:* `rhs` has no shared state.
|
| 4011 |
|
| 4012 |
``` cpp
|
| 4013 |
packaged_task& operator=(packaged_task&& rhs) noexcept;
|
| 4014 |
```
|
| 4015 |
|
| 4016 |
*Effects:*
|
| 4017 |
|
| 4018 |
+
- Releases any shared state ([[futures.state]]);
|
| 4019 |
+
- calls `packaged_task(std::move(rhs)).swap(*this)`.
|
| 4020 |
|
| 4021 |
``` cpp
|
| 4022 |
~packaged_task();
|
| 4023 |
```
|
| 4024 |
|
| 4025 |
+
*Effects:* Abandons any shared state ([[futures.state]]).
|
| 4026 |
|
| 4027 |
``` cpp
|
| 4028 |
void swap(packaged_task& other) noexcept;
|
| 4029 |
```
|
| 4030 |
|
| 4031 |
+
*Effects:* Exchanges the shared states and stored tasks of `*this` and
|
| 4032 |
`other`.
|
| 4033 |
|
| 4034 |
+
*Postconditions:* `*this` has the same shared state and stored task (if
|
| 4035 |
+
any) as `other` prior to the call to `swap`. `other` has the same shared
|
| 4036 |
+
state and stored task (if any) as `*this` prior to the call to `swap`.
|
| 4037 |
|
| 4038 |
``` cpp
|
| 4039 |
bool valid() const noexcept;
|
| 4040 |
```
|
| 4041 |
|
|
|
|
| 4058 |
|
| 4059 |
``` cpp
|
| 4060 |
void operator()(ArgTypes... args);
|
| 4061 |
```
|
| 4062 |
|
| 4063 |
+
*Effects:* As if by *INVOKE*\<R\>(f, t1, t2, ..., tN), where `f` is the
|
| 4064 |
+
stored task of `*this` and `t1, t2, ..., tN` are the values in
|
| 4065 |
+
`args...`. If the task returns normally, the return value is stored as
|
| 4066 |
+
the asynchronous result in the shared state of `*this`, otherwise the
|
| 4067 |
exception thrown by the task is stored. The shared state of `*this` is
|
| 4068 |
made ready, and any threads blocked in a function waiting for the shared
|
| 4069 |
state of `*this` to become ready are unblocked.
|
| 4070 |
|
| 4071 |
*Throws:* a `future_error` exception object if there is no shared state
|
|
|
|
| 4079 |
|
| 4080 |
``` cpp
|
| 4081 |
void make_ready_at_thread_exit(ArgTypes... args);
|
| 4082 |
```
|
| 4083 |
|
| 4084 |
+
*Effects:* As if by *INVOKE*\<R\>(f, t1, t2, ..., tN), where `f` is the
|
| 4085 |
+
stored task and `t1, t2, ..., tN` are the values in `args...`. If the
|
| 4086 |
+
task returns normally, the return value is stored as the asynchronous
|
| 4087 |
+
result in the shared state of `*this`, otherwise the exception thrown by
|
| 4088 |
+
the task is stored. In either case, this shall be done without making
|
| 4089 |
+
that state ready ([[futures.state]]) immediately. Schedules the shared
|
| 4090 |
+
state to be made ready when the current thread exits, after all objects
|
| 4091 |
+
of thread storage duration associated with the current thread have been
|
| 4092 |
destroyed.
|
| 4093 |
|
| 4094 |
*Throws:* `future_error` if an error condition occurs.
|
| 4095 |
|
| 4096 |
*Error conditions:*
|
|
|
|
| 4101 |
|
| 4102 |
``` cpp
|
| 4103 |
void reset();
|
| 4104 |
```
|
| 4105 |
|
| 4106 |
+
*Effects:* As if `*this = packaged_task(std::move(f))`, where `f` is the
|
| 4107 |
+
task stored in `*this`.
|
| 4108 |
+
|
| 4109 |
+
[*Note 1*: This constructs a new shared state for `*this`. The old
|
| 4110 |
+
state is abandoned ([[futures.state]]). — *end note*]
|
| 4111 |
|
| 4112 |
*Throws:*
|
| 4113 |
|
| 4114 |
- `bad_alloc` if memory for the new shared state could not be allocated.
|
| 4115 |
- any exception thrown by the move constructor of the task stored in the
|
|
|
|
| 4122 |
``` cpp
|
| 4123 |
template <class R, class... ArgTypes>
|
| 4124 |
void swap(packaged_task<R(ArgTypes...)>& x, packaged_task<R(ArgTypes...)>& y) noexcept;
|
| 4125 |
```
|
| 4126 |
|
| 4127 |
+
*Effects:* As if by `x.swap(y)`.
|
| 4128 |
|
| 4129 |
``` cpp
|
| 4130 |
template <class R, class Alloc>
|
| 4131 |
struct uses_allocator<packaged_task<R>, Alloc>
|
| 4132 |
: true_type { };
|
|
|
|
| 4140 |
[atomics]: atomics.md#atomics
|
| 4141 |
[basic.life]: basic.md#basic.life
|
| 4142 |
[basic.stc.thread]: basic.md#basic.stc.thread
|
| 4143 |
[bitmask.types]: library.md#bitmask.types
|
| 4144 |
[class]: class.md#class
|
| 4145 |
+
[condition_variable.syn]: #condition_variable.syn
|
| 4146 |
[except.terminate]: except.md#except.terminate
|
| 4147 |
[func.require]: utilities.md#func.require
|
| 4148 |
+
[future.syn]: #future.syn
|
| 4149 |
[futures]: #futures
|
| 4150 |
[futures.async]: #futures.async
|
| 4151 |
[futures.errors]: #futures.errors
|
| 4152 |
[futures.future_error]: #futures.future_error
|
| 4153 |
[futures.overview]: #futures.overview
|
|
|
|
| 4157 |
[futures.task]: #futures.task
|
| 4158 |
[futures.task.members]: #futures.task.members
|
| 4159 |
[futures.task.nonmembers]: #futures.task.nonmembers
|
| 4160 |
[futures.unique_future]: #futures.unique_future
|
| 4161 |
[intro.multithread]: intro.md#intro.multithread
|
| 4162 |
+
[mutex.syn]: #mutex.syn
|
| 4163 |
[res.on.data.races]: library.md#res.on.data.races
|
| 4164 |
[res.on.exception.handling]: library.md#res.on.exception.handling
|
| 4165 |
+
[shared_mutex.syn]: #shared_mutex.syn
|
| 4166 |
[syserr]: diagnostics.md#syserr
|
| 4167 |
[syserr.syserr]: diagnostics.md#syserr.syserr
|
| 4168 |
[tab:thread.lib.summary]: #tab:thread.lib.summary
|
| 4169 |
[thread]: #thread
|
| 4170 |
[thread.condition]: #thread.condition
|
| 4171 |
[thread.condition.condvar]: #thread.condition.condvar
|
| 4172 |
[thread.condition.condvarany]: #thread.condition.condvarany
|
| 4173 |
+
[thread.condition.nonmember]: #thread.condition.nonmember
|
| 4174 |
[thread.decaycopy]: #thread.decaycopy
|
| 4175 |
[thread.general]: #thread.general
|
| 4176 |
[thread.lock]: #thread.lock
|
| 4177 |
[thread.lock.algorithm]: #thread.lock.algorithm
|
| 4178 |
[thread.lock.guard]: #thread.lock.guard
|
| 4179 |
+
[thread.lock.scoped]: #thread.lock.scoped
|
| 4180 |
[thread.lock.shared]: #thread.lock.shared
|
| 4181 |
[thread.lock.shared.cons]: #thread.lock.shared.cons
|
| 4182 |
[thread.lock.shared.locking]: #thread.lock.shared.locking
|
| 4183 |
[thread.lock.shared.mod]: #thread.lock.shared.mod
|
| 4184 |
[thread.lock.shared.obs]: #thread.lock.shared.obs
|
|
|
|
| 4204 |
[thread.req.lockable.req]: #thread.req.lockable.req
|
| 4205 |
[thread.req.lockable.timed]: #thread.req.lockable.timed
|
| 4206 |
[thread.req.native]: #thread.req.native
|
| 4207 |
[thread.req.paramname]: #thread.req.paramname
|
| 4208 |
[thread.req.timing]: #thread.req.timing
|
| 4209 |
+
[thread.sharedmutex.class]: #thread.sharedmutex.class
|
| 4210 |
+
[thread.sharedmutex.requirements]: #thread.sharedmutex.requirements
|
| 4211 |
[thread.sharedtimedmutex.class]: #thread.sharedtimedmutex.class
|
| 4212 |
[thread.sharedtimedmutex.requirements]: #thread.sharedtimedmutex.requirements
|
| 4213 |
+
[thread.syn]: #thread.syn
|
| 4214 |
[thread.thread.algorithm]: #thread.thread.algorithm
|
| 4215 |
[thread.thread.assign]: #thread.thread.assign
|
| 4216 |
[thread.thread.class]: #thread.thread.class
|
| 4217 |
[thread.thread.constr]: #thread.thread.constr
|
| 4218 |
[thread.thread.destr]: #thread.thread.destr
|