- tmp/tmptklwi16k/{from.md → to.md} +118 -87
tmp/tmptklwi16k/{from.md → to.md}
RENAMED
|
@@ -26,10 +26,12 @@ executions are executed in a single unspecified total order consistent
|
|
| 26 |
with the "happens before" order.
|
| 27 |
|
| 28 |
Condition variable construction and destruction need not be
|
| 29 |
synchronized.
|
| 30 |
|
|
|
|
|
|
|
| 31 |
``` cpp
|
| 32 |
namespace std {
|
| 33 |
class condition_variable;
|
| 34 |
class condition_variable_any;
|
| 35 |
|
|
@@ -37,10 +39,12 @@ namespace std {
|
|
| 37 |
|
| 38 |
enum class cv_status { no_timeout, timeout };
|
| 39 |
}
|
| 40 |
```
|
| 41 |
|
|
|
|
|
|
|
| 42 |
``` cpp
|
| 43 |
void notify_all_at_thread_exit(condition_variable& cond, unique_lock<mutex> lk);
|
| 44 |
```
|
| 45 |
|
| 46 |
*Requires:* `lk` is locked by the calling thread and either
|
|
@@ -48,37 +52,38 @@ void notify_all_at_thread_exit(condition_variable& cond, unique_lock<mutex> lk);
|
|
| 48 |
- no other thread is waiting on `cond`, or
|
| 49 |
- `lk.mutex()` returns the same value for each of the lock arguments
|
| 50 |
supplied by all concurrently waiting (via `wait`, `wait_for`, or
|
| 51 |
`wait_until`) threads.
|
| 52 |
|
| 53 |
-
*Effects:*
|
| 54 |
internal storage and schedules `cond` to be notified when the current
|
| 55 |
thread exits, after all objects of thread storage duration associated
|
| 56 |
with the current thread have been destroyed. This notification shall be
|
| 57 |
-
as if
|
| 58 |
|
| 59 |
``` cpp
|
| 60 |
lk.unlock();
|
| 61 |
cond.notify_all();
|
| 62 |
```
|
| 63 |
|
| 64 |
*Synchronization:* The implied `lk.unlock()` call is sequenced after the
|
| 65 |
destruction of all objects with thread storage duration associated with
|
| 66 |
the current thread.
|
| 67 |
|
| 68 |
-
*Note
|
| 69 |
-
must be taken to ensure that this does not cause deadlock due to
|
| 70 |
-
ordering issues. After calling `notify_all_at_thread_exit` it is
|
| 71 |
recommended that the thread should be exited as soon as possible, and
|
| 72 |
-
that no blocking or time-consuming tasks are run on that
|
|
|
|
| 73 |
|
| 74 |
-
*Note
|
| 75 |
-
do not erroneously assume that the thread has finished if they
|
| 76 |
experience spurious wakeups. This typically requires that the condition
|
| 77 |
being waited for is satisfied while holding the lock on `lk`, and that
|
| 78 |
this lock is not released and reacquired prior to calling
|
| 79 |
-
`notify_all_at_thread_exit`.
|
| 80 |
|
| 81 |
### Class `condition_variable` <a id="thread.condition.condvar">[[thread.condition.condvar]]</a>
|
| 82 |
|
| 83 |
``` cpp
|
| 84 |
namespace std {
|
|
@@ -110,11 +115,11 @@ namespace std {
|
|
| 110 |
template <class Rep, class Period, class Predicate>
|
| 111 |
bool wait_for(unique_lock<mutex>& lock,
|
| 112 |
const chrono::duration<Rep, Period>& rel_time,
|
| 113 |
Predicate pred);
|
| 114 |
|
| 115 |
-
|
| 116 |
native_handle_type native_handle(); // See~[thread.req.native]
|
| 117 |
};
|
| 118 |
}
|
| 119 |
```
|
| 120 |
|
|
@@ -137,19 +142,21 @@ required ([[thread.req.exception]]).
|
|
| 137 |
|
| 138 |
``` cpp
|
| 139 |
~condition_variable();
|
| 140 |
```
|
| 141 |
|
| 142 |
-
There shall be no thread blocked on `*this`.
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
|
|
|
|
|
|
| 151 |
|
| 152 |
*Effects:* Destroys the object.
|
| 153 |
|
| 154 |
``` cpp
|
| 155 |
void notify_one() noexcept;
|
|
@@ -166,12 +173,12 @@ void notify_all() noexcept;
|
|
| 166 |
|
| 167 |
``` cpp
|
| 168 |
void wait(unique_lock<mutex>& lock);
|
| 169 |
```
|
| 170 |
|
| 171 |
-
`lock.owns_lock()` is `true` and `lock.mutex()` is locked by
|
| 172 |
-
thread, and either
|
| 173 |
|
| 174 |
- no other thread is waiting on this `condition_variable` object or
|
| 175 |
- `lock.mutex()` returns the same value for each of the `lock` arguments
|
| 176 |
supplied by all concurrently waiting (via `wait`, `wait_for`, or
|
| 177 |
`wait_until`) threads.
|
|
@@ -183,15 +190,17 @@ thread, and either
|
|
| 183 |
then returns.
|
| 184 |
- The function will unblock when signaled by a call to `notify_one()` or
|
| 185 |
a call to `notify_all()`, or spuriously.
|
| 186 |
|
| 187 |
*Remarks:* If the function fails to meet the postcondition,
|
| 188 |
-
`
|
| 189 |
-
happen if the re-locking of the mutex throws an exception.
|
| 190 |
|
| 191 |
-
|
| 192 |
-
|
|
|
|
|
|
|
|
|
|
| 193 |
|
| 194 |
*Throws:* Nothing.
|
| 195 |
|
| 196 |
``` cpp
|
| 197 |
template <class Predicate>
|
|
@@ -212,27 +221,28 @@ the calling thread, and either
|
|
| 212 |
while (!pred())
|
| 213 |
wait(lock);
|
| 214 |
```
|
| 215 |
|
| 216 |
*Remarks:* If the function fails to meet the postcondition,
|
| 217 |
-
`
|
| 218 |
-
happen if the re-locking of the mutex throws an exception.
|
| 219 |
|
| 220 |
-
|
| 221 |
-
|
| 222 |
|
| 223 |
-
*
|
| 224 |
-
|
|
|
|
|
|
|
| 225 |
|
| 226 |
``` cpp
|
| 227 |
template <class Clock, class Duration>
|
| 228 |
cv_status wait_until(unique_lock<mutex>& lock,
|
| 229 |
const chrono::time_point<Clock, Duration>& abs_time);
|
| 230 |
```
|
| 231 |
|
| 232 |
-
`lock.owns_lock()` is `true` and `lock.mutex()` is locked by
|
| 233 |
-
thread, and either
|
| 234 |
|
| 235 |
- no other thread is waiting on this `condition_variable` object or
|
| 236 |
- `lock.mutex()` returns the same value for each of the `lock` arguments
|
| 237 |
supplied by all concurrently waiting (via `wait`, `wait_for`, or
|
| 238 |
`wait_until`) threads.
|
|
@@ -248,15 +258,17 @@ thread, and either
|
|
| 248 |
spuriously.
|
| 249 |
- If the function exits via an exception, `lock.lock()` shall be called
|
| 250 |
prior to exiting the function.
|
| 251 |
|
| 252 |
*Remarks:* If the function fails to meet the postcondition,
|
| 253 |
-
`
|
| 254 |
-
happen if the re-locking of the mutex throws an exception.
|
| 255 |
|
| 256 |
-
|
| 257 |
-
|
|
|
|
|
|
|
|
|
|
| 258 |
|
| 259 |
*Returns:* `cv_status::timeout` if the absolute
|
| 260 |
timeout ([[thread.req.timing]]) specified by `abs_time` expired,
|
| 261 |
otherwise `cv_status::no_timeout`.
|
| 262 |
|
|
@@ -266,12 +278,12 @@ otherwise `cv_status::no_timeout`.
|
|
| 266 |
template <class Rep, class Period>
|
| 267 |
cv_status wait_for(unique_lock<mutex>& lock,
|
| 268 |
const chrono::duration<Rep, Period>& rel_time);
|
| 269 |
```
|
| 270 |
|
| 271 |
-
`lock.owns_lock()` is `true` and `lock.mutex()` is locked by
|
| 272 |
-
thread, and either
|
| 273 |
|
| 274 |
- no other thread is waiting on this `condition_variable` object or
|
| 275 |
- `lock.mutex()` returns the same value for each of the `lock` arguments
|
| 276 |
supplied by all concurrently waiting (via `wait`, `wait_for`, or
|
| 277 |
`wait_until`) threads.
|
|
@@ -285,15 +297,17 @@ return wait_until(lock, chrono::steady_clock::now() + rel_time);
|
|
| 285 |
*Returns:* `cv_status::timeout` if the relative
|
| 286 |
timeout ([[thread.req.timing]]) specified by `rel_time` expired,
|
| 287 |
otherwise `cv_status::no_timeout`.
|
| 288 |
|
| 289 |
*Remarks:* If the function fails to meet the postcondition,
|
| 290 |
-
`
|
| 291 |
-
happen if the re-locking of the mutex throws an exception.
|
| 292 |
|
| 293 |
-
|
| 294 |
-
|
|
|
|
|
|
|
|
|
|
| 295 |
|
| 296 |
*Throws:* Timeout-related exceptions ([[thread.req.timing]]).
|
| 297 |
|
| 298 |
``` cpp
|
| 299 |
template <class Clock, class Duration, class Predicate>
|
|
@@ -318,18 +332,21 @@ while (!pred())
|
|
| 318 |
return pred();
|
| 319 |
return true;
|
| 320 |
```
|
| 321 |
|
| 322 |
*Remarks:* If the function fails to meet the postcondition,
|
| 323 |
-
`
|
| 324 |
-
happen if the re-locking of the mutex throws an exception.
|
| 325 |
|
| 326 |
-
|
| 327 |
-
|
| 328 |
|
| 329 |
-
|
| 330 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 331 |
|
| 332 |
*Throws:* Timeout-related exceptions ([[thread.req.timing]]) or any
|
| 333 |
exception thrown by `pred`.
|
| 334 |
|
| 335 |
``` cpp
|
|
@@ -337,12 +354,12 @@ template <class Rep, class Period, class Predicate>
|
|
| 337 |
bool wait_for(unique_lock<mutex>& lock,
|
| 338 |
const chrono::duration<Rep, Period>& rel_time,
|
| 339 |
Predicate pred);
|
| 340 |
```
|
| 341 |
|
| 342 |
-
`lock.owns_lock()` is `true` and `lock.mutex()` is locked by
|
| 343 |
-
thread, and either
|
| 344 |
|
| 345 |
- no other thread is waiting on this `condition_variable` object or
|
| 346 |
- `lock.mutex()` returns the same value for each of the `lock` arguments
|
| 347 |
supplied by all concurrently waiting (via `wait`, `wait_for`, or
|
| 348 |
`wait_until`) threads.
|
|
@@ -351,35 +368,40 @@ thread, and either
|
|
| 351 |
|
| 352 |
``` cpp
|
| 353 |
return wait_until(lock, chrono::steady_clock::now() + rel_time, std::move(pred));
|
| 354 |
```
|
| 355 |
|
| 356 |
-
There is no blocking if `pred()` is initially `true`, even
|
| 357 |
-
timeout has already expired.
|
| 358 |
|
| 359 |
*Remarks:* If the function fails to meet the postcondition,
|
| 360 |
-
`
|
| 361 |
-
happen if the re-locking of the mutex throws an exception.
|
| 362 |
|
| 363 |
-
|
| 364 |
-
|
| 365 |
|
| 366 |
-
|
| 367 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 368 |
|
| 369 |
*Throws:* Timeout-related exceptions ([[thread.req.timing]]) or any
|
| 370 |
exception thrown by `pred`.
|
| 371 |
|
| 372 |
### Class `condition_variable_any` <a id="thread.condition.condvarany">[[thread.condition.condvarany]]</a>
|
| 373 |
|
| 374 |
A `Lock` type shall meet the `BasicLockable` requirements (
|
| 375 |
-
[[thread.req.lockable.basic]]).
|
| 376 |
-
|
| 377 |
-
|
|
|
|
|
|
|
| 378 |
`condition_variable_any`, the user must ensure that any necessary
|
| 379 |
synchronization is in place with respect to the predicate associated
|
| 380 |
-
with the `condition_variable_any` instance.
|
| 381 |
|
| 382 |
``` cpp
|
| 383 |
namespace std {
|
| 384 |
class condition_variable_any {
|
| 385 |
public:
|
|
@@ -428,19 +450,21 @@ required ([[thread.req.exception]]).
|
|
| 428 |
|
| 429 |
``` cpp
|
| 430 |
~condition_variable_any();
|
| 431 |
```
|
| 432 |
|
| 433 |
-
There shall be no thread blocked on `*this`.
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
|
|
|
|
|
|
|
| 442 |
|
| 443 |
*Effects:* Destroys the object.
|
| 444 |
|
| 445 |
``` cpp
|
| 446 |
void notify_one() noexcept;
|
|
@@ -458,27 +482,29 @@ void notify_all() noexcept;
|
|
| 458 |
``` cpp
|
| 459 |
template <class Lock>
|
| 460 |
void wait(Lock& lock);
|
| 461 |
```
|
| 462 |
|
| 463 |
-
*Note
|
| 464 |
unspecified whether the `Lock` is held. One can use a `Lock` type that
|
| 465 |
-
allows to query that, such as the `unique_lock` wrapper.
|
| 466 |
|
| 467 |
*Effects:*
|
| 468 |
|
| 469 |
- Atomically calls `lock.unlock()` and blocks on `*this`.
|
| 470 |
- When unblocked, calls `lock.lock()` (possibly blocking on the lock)
|
| 471 |
and returns.
|
| 472 |
- The function will unblock when signaled by a call to `notify_one()`, a
|
| 473 |
call to `notify_all()`, or spuriously.
|
| 474 |
|
| 475 |
*Remarks:* If the function fails to meet the postcondition,
|
| 476 |
-
`
|
| 477 |
-
happen if the re-locking of the mutex throws an exception.
|
| 478 |
|
| 479 |
-
|
|
|
|
|
|
|
|
|
|
| 480 |
|
| 481 |
*Throws:* Nothing.
|
| 482 |
|
| 483 |
``` cpp
|
| 484 |
template <class Lock, class Predicate>
|
|
@@ -508,14 +534,16 @@ template <class Lock, class Clock, class Duration>
|
|
| 508 |
spuriously.
|
| 509 |
- If the function exits via an exception, `lock.lock()` shall be called
|
| 510 |
prior to exiting the function.
|
| 511 |
|
| 512 |
*Remarks:* If the function fails to meet the postcondition,
|
| 513 |
-
`
|
| 514 |
-
happen if the re-locking of the mutex throws an exception.
|
| 515 |
|
| 516 |
-
|
|
|
|
|
|
|
|
|
|
| 517 |
|
| 518 |
*Returns:* `cv_status::timeout` if the absolute
|
| 519 |
timeout ([[thread.req.timing]]) specified by `abs_time` expired,
|
| 520 |
otherwise `cv_status::no_timeout`.
|
| 521 |
|
|
@@ -535,14 +563,16 @@ return wait_until(lock, chrono::steady_clock::now() + rel_time);
|
|
| 535 |
*Returns:* `cv_status::timeout` if the relative
|
| 536 |
timeout ([[thread.req.timing]]) specified by `rel_time` expired,
|
| 537 |
otherwise `cv_status::no_timeout`.
|
| 538 |
|
| 539 |
*Remarks:* If the function fails to meet the postcondition,
|
| 540 |
-
`
|
| 541 |
-
happen if the re-locking of the mutex throws an exception.
|
| 542 |
|
| 543 |
-
|
|
|
|
|
|
|
|
|
|
| 544 |
|
| 545 |
*Throws:* Timeout-related exceptions ([[thread.req.timing]]).
|
| 546 |
|
| 547 |
``` cpp
|
| 548 |
template <class Lock, class Clock, class Duration, class Predicate>
|
|
@@ -556,15 +586,16 @@ while (!pred())
|
|
| 556 |
if (wait_until(lock, abs_time) == cv_status::timeout)
|
| 557 |
return pred();
|
| 558 |
return true;
|
| 559 |
```
|
| 560 |
|
| 561 |
-
There is no blocking if `pred()` is initially `true`, or if
|
| 562 |
-
has already expired.
|
| 563 |
|
| 564 |
-
The returned value indicates whether the predicate evaluates
|
| 565 |
-
regardless of whether the timeout was
|
|
|
|
| 566 |
|
| 567 |
``` cpp
|
| 568 |
template <class Lock, class Rep, class Period, class Predicate>
|
| 569 |
bool wait_for(Lock& lock, const chrono::duration<Rep, Period>& rel_time, Predicate pred);
|
| 570 |
```
|
|
|
|
| 26 |
with the "happens before" order.
|
| 27 |
|
| 28 |
Condition variable construction and destruction need not be
|
| 29 |
synchronized.
|
| 30 |
|
| 31 |
+
### Header `<condition_variable>` synopsis <a id="condition_variable.syn">[[condition_variable.syn]]</a>
|
| 32 |
+
|
| 33 |
``` cpp
|
| 34 |
namespace std {
|
| 35 |
class condition_variable;
|
| 36 |
class condition_variable_any;
|
| 37 |
|
|
|
|
| 39 |
|
| 40 |
enum class cv_status { no_timeout, timeout };
|
| 41 |
}
|
| 42 |
```
|
| 43 |
|
| 44 |
+
### Non-member functions <a id="thread.condition.nonmember">[[thread.condition.nonmember]]</a>
|
| 45 |
+
|
| 46 |
``` cpp
|
| 47 |
void notify_all_at_thread_exit(condition_variable& cond, unique_lock<mutex> lk);
|
| 48 |
```
|
| 49 |
|
| 50 |
*Requires:* `lk` is locked by the calling thread and either
|
|
|
|
| 52 |
- no other thread is waiting on `cond`, or
|
| 53 |
- `lk.mutex()` returns the same value for each of the lock arguments
|
| 54 |
supplied by all concurrently waiting (via `wait`, `wait_for`, or
|
| 55 |
`wait_until`) threads.
|
| 56 |
|
| 57 |
+
*Effects:* Transfers ownership of the lock associated with `lk` into
|
| 58 |
internal storage and schedules `cond` to be notified when the current
|
| 59 |
thread exits, after all objects of thread storage duration associated
|
| 60 |
with the current thread have been destroyed. This notification shall be
|
| 61 |
+
as if:
|
| 62 |
|
| 63 |
``` cpp
|
| 64 |
lk.unlock();
|
| 65 |
cond.notify_all();
|
| 66 |
```
|
| 67 |
|
| 68 |
*Synchronization:* The implied `lk.unlock()` call is sequenced after the
|
| 69 |
destruction of all objects with thread storage duration associated with
|
| 70 |
the current thread.
|
| 71 |
|
| 72 |
+
[*Note 1*: The supplied lock will be held until the thread exits, and
|
| 73 |
+
care must be taken to ensure that this does not cause deadlock due to
|
| 74 |
+
lock ordering issues. After calling `notify_all_at_thread_exit` it is
|
| 75 |
recommended that the thread should be exited as soon as possible, and
|
| 76 |
+
that no blocking or time-consuming tasks are run on that
|
| 77 |
+
thread. — *end note*]
|
| 78 |
|
| 79 |
+
[*Note 2*: It is the user’s responsibility to ensure that waiting
|
| 80 |
+
threads do not erroneously assume that the thread has finished if they
|
| 81 |
experience spurious wakeups. This typically requires that the condition
|
| 82 |
being waited for is satisfied while holding the lock on `lk`, and that
|
| 83 |
this lock is not released and reacquired prior to calling
|
| 84 |
+
`notify_all_at_thread_exit`. — *end note*]
|
| 85 |
|
| 86 |
### Class `condition_variable` <a id="thread.condition.condvar">[[thread.condition.condvar]]</a>
|
| 87 |
|
| 88 |
``` cpp
|
| 89 |
namespace std {
|
|
|
|
| 115 |
template <class Rep, class Period, class Predicate>
|
| 116 |
bool wait_for(unique_lock<mutex>& lock,
|
| 117 |
const chrono::duration<Rep, Period>& rel_time,
|
| 118 |
Predicate pred);
|
| 119 |
|
| 120 |
+
using native_handle_type = implementation-defined; // See~[thread.req.native]
|
| 121 |
native_handle_type native_handle(); // See~[thread.req.native]
|
| 122 |
};
|
| 123 |
}
|
| 124 |
```
|
| 125 |
|
|
|
|
| 142 |
|
| 143 |
``` cpp
|
| 144 |
~condition_variable();
|
| 145 |
```
|
| 146 |
|
| 147 |
+
*Requires:* There shall be no thread blocked on `*this`.
|
| 148 |
+
|
| 149 |
+
[*Note 1*: That is, all threads shall have been notified; they may
|
| 150 |
+
subsequently block on the lock specified in the wait. This relaxes the
|
| 151 |
+
usual rules, which would have required all wait calls to happen before
|
| 152 |
+
destruction. Only the notification to unblock the wait must happen
|
| 153 |
+
before destruction. The user must take care to ensure that no threads
|
| 154 |
+
wait on `*this` once the destructor has been started, especially when
|
| 155 |
+
the waiting threads are calling the wait functions in a loop or using
|
| 156 |
+
the overloads of `wait`, `wait_for`, or `wait_until` that take a
|
| 157 |
+
predicate. — *end note*]
|
| 158 |
|
| 159 |
*Effects:* Destroys the object.
|
| 160 |
|
| 161 |
``` cpp
|
| 162 |
void notify_one() noexcept;
|
|
|
|
| 173 |
|
| 174 |
``` cpp
|
| 175 |
void wait(unique_lock<mutex>& lock);
|
| 176 |
```
|
| 177 |
|
| 178 |
+
*Requires:* `lock.owns_lock()` is `true` and `lock.mutex()` is locked by
|
| 179 |
+
the calling thread, and either
|
| 180 |
|
| 181 |
- no other thread is waiting on this `condition_variable` object or
|
| 182 |
- `lock.mutex()` returns the same value for each of the `lock` arguments
|
| 183 |
supplied by all concurrently waiting (via `wait`, `wait_for`, or
|
| 184 |
`wait_until`) threads.
|
|
|
|
| 190 |
then returns.
|
| 191 |
- The function will unblock when signaled by a call to `notify_one()` or
|
| 192 |
a call to `notify_all()`, or spuriously.
|
| 193 |
|
| 194 |
*Remarks:* If the function fails to meet the postcondition,
|
| 195 |
+
`terminate()` shall be called ([[except.terminate]]).
|
|
|
|
| 196 |
|
| 197 |
+
[*Note 2*: This can happen if the re-locking of the mutex throws an
|
| 198 |
+
exception. — *end note*]
|
| 199 |
+
|
| 200 |
+
*Postconditions:* `lock.owns_lock()` is `true` and `lock.mutex()` is
|
| 201 |
+
locked by the calling thread.
|
| 202 |
|
| 203 |
*Throws:* Nothing.
|
| 204 |
|
| 205 |
``` cpp
|
| 206 |
template <class Predicate>
|
|
|
|
| 221 |
while (!pred())
|
| 222 |
wait(lock);
|
| 223 |
```
|
| 224 |
|
| 225 |
*Remarks:* If the function fails to meet the postcondition,
|
| 226 |
+
`terminate()` shall be called ([[except.terminate]]).
|
|
|
|
| 227 |
|
| 228 |
+
[*Note 3*: This can happen if the re-locking of the mutex throws an
|
| 229 |
+
exception. — *end note*]
|
| 230 |
|
| 231 |
+
*Postconditions:* `lock.owns_lock()` is `true` and `lock.mutex()` is
|
| 232 |
+
locked by the calling thread.
|
| 233 |
+
|
| 234 |
+
*Throws:* Any exception thrown by `pred`.
|
| 235 |
|
| 236 |
``` cpp
|
| 237 |
template <class Clock, class Duration>
|
| 238 |
cv_status wait_until(unique_lock<mutex>& lock,
|
| 239 |
const chrono::time_point<Clock, Duration>& abs_time);
|
| 240 |
```
|
| 241 |
|
| 242 |
+
*Requires:* `lock.owns_lock()` is `true` and `lock.mutex()` is locked by
|
| 243 |
+
the calling thread, and either
|
| 244 |
|
| 245 |
- no other thread is waiting on this `condition_variable` object or
|
| 246 |
- `lock.mutex()` returns the same value for each of the `lock` arguments
|
| 247 |
supplied by all concurrently waiting (via `wait`, `wait_for`, or
|
| 248 |
`wait_until`) threads.
|
|
|
|
| 258 |
spuriously.
|
| 259 |
- If the function exits via an exception, `lock.lock()` shall be called
|
| 260 |
prior to exiting the function.
|
| 261 |
|
| 262 |
*Remarks:* If the function fails to meet the postcondition,
|
| 263 |
+
`terminate()` shall be called ([[except.terminate]]).
|
|
|
|
| 264 |
|
| 265 |
+
[*Note 4*: This can happen if the re-locking of the mutex throws an
|
| 266 |
+
exception. — *end note*]
|
| 267 |
+
|
| 268 |
+
*Postconditions:* `lock.owns_lock()` is `true` and `lock.mutex()` is
|
| 269 |
+
locked by the calling thread.
|
| 270 |
|
| 271 |
*Returns:* `cv_status::timeout` if the absolute
|
| 272 |
timeout ([[thread.req.timing]]) specified by `abs_time` expired,
|
| 273 |
otherwise `cv_status::no_timeout`.
|
| 274 |
|
|
|
|
| 278 |
template <class Rep, class Period>
|
| 279 |
cv_status wait_for(unique_lock<mutex>& lock,
|
| 280 |
const chrono::duration<Rep, Period>& rel_time);
|
| 281 |
```
|
| 282 |
|
| 283 |
+
*Requires:* `lock.owns_lock()` is `true` and `lock.mutex()` is locked by
|
| 284 |
+
the calling thread, and either
|
| 285 |
|
| 286 |
- no other thread is waiting on this `condition_variable` object or
|
| 287 |
- `lock.mutex()` returns the same value for each of the `lock` arguments
|
| 288 |
supplied by all concurrently waiting (via `wait`, `wait_for`, or
|
| 289 |
`wait_until`) threads.
|
|
|
|
| 297 |
*Returns:* `cv_status::timeout` if the relative
|
| 298 |
timeout ([[thread.req.timing]]) specified by `rel_time` expired,
|
| 299 |
otherwise `cv_status::no_timeout`.
|
| 300 |
|
| 301 |
*Remarks:* If the function fails to meet the postcondition,
|
| 302 |
+
`terminate()` shall be called ([[except.terminate]]).
|
|
|
|
| 303 |
|
| 304 |
+
[*Note 5*: This can happen if the re-locking of the mutex throws an
|
| 305 |
+
exception. — *end note*]
|
| 306 |
+
|
| 307 |
+
*Postconditions:* `lock.owns_lock()` is `true` and `lock.mutex()` is
|
| 308 |
+
locked by the calling thread.
|
| 309 |
|
| 310 |
*Throws:* Timeout-related exceptions ([[thread.req.timing]]).
|
| 311 |
|
| 312 |
``` cpp
|
| 313 |
template <class Clock, class Duration, class Predicate>
|
|
|
|
| 332 |
return pred();
|
| 333 |
return true;
|
| 334 |
```
|
| 335 |
|
| 336 |
*Remarks:* If the function fails to meet the postcondition,
|
| 337 |
+
`terminate()` shall be called ([[except.terminate]]).
|
|
|
|
| 338 |
|
| 339 |
+
[*Note 6*: This can happen if the re-locking of the mutex throws an
|
| 340 |
+
exception. — *end note*]
|
| 341 |
|
| 342 |
+
*Postconditions:* `lock.owns_lock()` is `true` and `lock.mutex()` is
|
| 343 |
+
locked by the calling thread.
|
| 344 |
+
|
| 345 |
+
[*Note 7*: The returned value indicates whether the predicate evaluated
|
| 346 |
+
to `true` regardless of whether the timeout was
|
| 347 |
+
triggered. — *end note*]
|
| 348 |
|
| 349 |
*Throws:* Timeout-related exceptions ([[thread.req.timing]]) or any
|
| 350 |
exception thrown by `pred`.
|
| 351 |
|
| 352 |
``` cpp
|
|
|
|
| 354 |
bool wait_for(unique_lock<mutex>& lock,
|
| 355 |
const chrono::duration<Rep, Period>& rel_time,
|
| 356 |
Predicate pred);
|
| 357 |
```
|
| 358 |
|
| 359 |
+
*Requires:* `lock.owns_lock()` is `true` and `lock.mutex()` is locked by
|
| 360 |
+
the calling thread, and either
|
| 361 |
|
| 362 |
- no other thread is waiting on this `condition_variable` object or
|
| 363 |
- `lock.mutex()` returns the same value for each of the `lock` arguments
|
| 364 |
supplied by all concurrently waiting (via `wait`, `wait_for`, or
|
| 365 |
`wait_until`) threads.
|
|
|
|
| 368 |
|
| 369 |
``` cpp
|
| 370 |
return wait_until(lock, chrono::steady_clock::now() + rel_time, std::move(pred));
|
| 371 |
```
|
| 372 |
|
| 373 |
+
[*Note 8*: There is no blocking if `pred()` is initially `true`, even
|
| 374 |
+
if the timeout has already expired. — *end note*]
|
| 375 |
|
| 376 |
*Remarks:* If the function fails to meet the postcondition,
|
| 377 |
+
`terminate()` shall be called ([[except.terminate]]).
|
|
|
|
| 378 |
|
| 379 |
+
[*Note 9*: This can happen if the re-locking of the mutex throws an
|
| 380 |
+
exception. — *end note*]
|
| 381 |
|
| 382 |
+
*Postconditions:* `lock.owns_lock()` is `true` and `lock.mutex()` is
|
| 383 |
+
locked by the calling thread.
|
| 384 |
+
|
| 385 |
+
[*Note 10*: The returned value indicates whether the predicate
|
| 386 |
+
evaluates to `true` regardless of whether the timeout was
|
| 387 |
+
triggered. — *end note*]
|
| 388 |
|
| 389 |
*Throws:* Timeout-related exceptions ([[thread.req.timing]]) or any
|
| 390 |
exception thrown by `pred`.
|
| 391 |
|
| 392 |
### Class `condition_variable_any` <a id="thread.condition.condvarany">[[thread.condition.condvarany]]</a>
|
| 393 |
|
| 394 |
A `Lock` type shall meet the `BasicLockable` requirements (
|
| 395 |
+
[[thread.req.lockable.basic]]).
|
| 396 |
+
|
| 397 |
+
[*Note 1*: All of the standard mutex types meet this requirement. If a
|
| 398 |
+
`Lock` type other than one of the standard mutex types or a
|
| 399 |
+
`unique_lock` wrapper for a standard mutex type is used with
|
| 400 |
`condition_variable_any`, the user must ensure that any necessary
|
| 401 |
synchronization is in place with respect to the predicate associated
|
| 402 |
+
with the `condition_variable_any` instance. — *end note*]
|
| 403 |
|
| 404 |
``` cpp
|
| 405 |
namespace std {
|
| 406 |
class condition_variable_any {
|
| 407 |
public:
|
|
|
|
| 450 |
|
| 451 |
``` cpp
|
| 452 |
~condition_variable_any();
|
| 453 |
```
|
| 454 |
|
| 455 |
+
*Requires:* There shall be no thread blocked on `*this`.
|
| 456 |
+
|
| 457 |
+
[*Note 1*: That is, all threads shall have been notified; they may
|
| 458 |
+
subsequently block on the lock specified in the wait. This relaxes the
|
| 459 |
+
usual rules, which would have required all wait calls to happen before
|
| 460 |
+
destruction. Only the notification to unblock the wait must happen
|
| 461 |
+
before destruction. The user must take care to ensure that no threads
|
| 462 |
+
wait on `*this` once the destructor has been started, especially when
|
| 463 |
+
the waiting threads are calling the wait functions in a loop or using
|
| 464 |
+
the overloads of `wait`, `wait_for`, or `wait_until` that take a
|
| 465 |
+
predicate. — *end note*]
|
| 466 |
|
| 467 |
*Effects:* Destroys the object.
|
| 468 |
|
| 469 |
``` cpp
|
| 470 |
void notify_one() noexcept;
|
|
|
|
| 482 |
``` cpp
|
| 483 |
template <class Lock>
|
| 484 |
void wait(Lock& lock);
|
| 485 |
```
|
| 486 |
|
| 487 |
+
[*Note 2*: If any of the `wait` functions exits via an exception, it is
|
| 488 |
unspecified whether the `Lock` is held. One can use a `Lock` type that
|
| 489 |
+
allows to query that, such as the `unique_lock` wrapper. — *end note*]
|
| 490 |
|
| 491 |
*Effects:*
|
| 492 |
|
| 493 |
- Atomically calls `lock.unlock()` and blocks on `*this`.
|
| 494 |
- When unblocked, calls `lock.lock()` (possibly blocking on the lock)
|
| 495 |
and returns.
|
| 496 |
- The function will unblock when signaled by a call to `notify_one()`, a
|
| 497 |
call to `notify_all()`, or spuriously.
|
| 498 |
|
| 499 |
*Remarks:* If the function fails to meet the postcondition,
|
| 500 |
+
`terminate()` shall be called ([[except.terminate]]).
|
|
|
|
| 501 |
|
| 502 |
+
[*Note 3*: This can happen if the re-locking of the mutex throws an
|
| 503 |
+
exception. — *end note*]
|
| 504 |
+
|
| 505 |
+
*Postconditions:* `lock` is locked by the calling thread.
|
| 506 |
|
| 507 |
*Throws:* Nothing.
|
| 508 |
|
| 509 |
``` cpp
|
| 510 |
template <class Lock, class Predicate>
|
|
|
|
| 534 |
spuriously.
|
| 535 |
- If the function exits via an exception, `lock.lock()` shall be called
|
| 536 |
prior to exiting the function.
|
| 537 |
|
| 538 |
*Remarks:* If the function fails to meet the postcondition,
|
| 539 |
+
`terminate()` shall be called ([[except.terminate]]).
|
|
|
|
| 540 |
|
| 541 |
+
[*Note 4*: This can happen if the re-locking of the mutex throws an
|
| 542 |
+
exception. — *end note*]
|
| 543 |
+
|
| 544 |
+
*Postconditions:* `lock` is locked by the calling thread.
|
| 545 |
|
| 546 |
*Returns:* `cv_status::timeout` if the absolute
|
| 547 |
timeout ([[thread.req.timing]]) specified by `abs_time` expired,
|
| 548 |
otherwise `cv_status::no_timeout`.
|
| 549 |
|
|
|
|
| 563 |
*Returns:* `cv_status::timeout` if the relative
|
| 564 |
timeout ([[thread.req.timing]]) specified by `rel_time` expired,
|
| 565 |
otherwise `cv_status::no_timeout`.
|
| 566 |
|
| 567 |
*Remarks:* If the function fails to meet the postcondition,
|
| 568 |
+
`terminate()` shall be called ([[except.terminate]]).
|
|
|
|
| 569 |
|
| 570 |
+
[*Note 5*: This can happen if the re-locking of the mutex throws an
|
| 571 |
+
exception. — *end note*]
|
| 572 |
+
|
| 573 |
+
*Postconditions:* `lock` is locked by the calling thread.
|
| 574 |
|
| 575 |
*Throws:* Timeout-related exceptions ([[thread.req.timing]]).
|
| 576 |
|
| 577 |
``` cpp
|
| 578 |
template <class Lock, class Clock, class Duration, class Predicate>
|
|
|
|
| 586 |
if (wait_until(lock, abs_time) == cv_status::timeout)
|
| 587 |
return pred();
|
| 588 |
return true;
|
| 589 |
```
|
| 590 |
|
| 591 |
+
[*Note 6*: There is no blocking if `pred()` is initially `true`, or if
|
| 592 |
+
the timeout has already expired. — *end note*]
|
| 593 |
|
| 594 |
+
[*Note 7*: The returned value indicates whether the predicate evaluates
|
| 595 |
+
to `true` regardless of whether the timeout was
|
| 596 |
+
triggered. — *end note*]
|
| 597 |
|
| 598 |
``` cpp
|
| 599 |
template <class Lock, class Rep, class Period, class Predicate>
|
| 600 |
bool wait_for(Lock& lock, const chrono::duration<Rep, Period>& rel_time, Predicate pred);
|
| 601 |
```
|