tmp/tmpodre6w04/{from.md → to.md}
RENAMED
|
@@ -1,10 +1,14 @@
|
|
| 1 |
## Threads <a id="thread.threads">[[thread.threads]]</a>
|
| 2 |
|
| 3 |
[[thread.threads]] describes components that can be used to create and
|
| 4 |
-
manage threads.
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
``` cpp
|
| 8 |
namespace std {
|
| 9 |
class thread;
|
| 10 |
|
|
@@ -31,21 +35,23 @@ thread. A `thread` object uniquely represents a particular thread of
|
|
| 31 |
execution. That representation may be transferred to other `thread`
|
| 32 |
objects in such a way that no two `thread` objects simultaneously
|
| 33 |
represent the same thread of execution. A thread of execution is
|
| 34 |
*detached* when no `thread` object represents that thread. Objects of
|
| 35 |
class `thread` can be in a state that does not represent a thread of
|
| 36 |
-
execution.
|
|
|
|
|
|
|
| 37 |
after default construction, after being moved from, or after a
|
| 38 |
-
successful call to `detach` or `join`.
|
| 39 |
|
| 40 |
``` cpp
|
| 41 |
namespace std {
|
| 42 |
class thread {
|
| 43 |
public:
|
| 44 |
// types:
|
| 45 |
class id;
|
| 46 |
-
|
| 47 |
|
| 48 |
// construct/copy/destroy:
|
| 49 |
thread() noexcept;
|
| 50 |
template <class F, class... Args> explicit thread(F&& f, Args&&... args);
|
| 51 |
~thread();
|
|
@@ -97,19 +103,19 @@ namespace std {
|
|
| 97 |
An object of type `thread::id` provides a unique identifier for each
|
| 98 |
thread of execution and a single distinct value for all `thread` objects
|
| 99 |
that do not represent a thread of execution ([[thread.thread.class]]).
|
| 100 |
Each thread of execution has an associated `thread::id` object that is
|
| 101 |
not equal to the `thread::id` object of any other thread of execution
|
| 102 |
-
and that is not equal to the `thread::id` object of any `
|
| 103 |
-
|
| 104 |
|
| 105 |
`thread::id` shall be a trivially copyable class (Clause [[class]]).
|
| 106 |
The library may reuse the value of a `thread::id` of a terminated thread
|
| 107 |
that can no longer be joined.
|
| 108 |
|
| 109 |
-
Relational operators allow `thread::id` objects to be used
|
| 110 |
-
associative containers.
|
| 111 |
|
| 112 |
``` cpp
|
| 113 |
id() noexcept;
|
| 114 |
```
|
| 115 |
|
|
@@ -140,78 +146,80 @@ described in [[alg.sorting]].
|
|
| 140 |
|
| 141 |
``` cpp
|
| 142 |
bool operator<=(thread::id x, thread::id y) noexcept;
|
| 143 |
```
|
| 144 |
|
| 145 |
-
*Returns:* `!(y < x)`
|
| 146 |
|
| 147 |
``` cpp
|
| 148 |
bool operator>(thread::id x, thread::id y) noexcept;
|
| 149 |
```
|
| 150 |
|
| 151 |
-
*Returns:* `y < x`
|
| 152 |
|
| 153 |
``` cpp
|
| 154 |
bool operator>=(thread::id x, thread::id y) noexcept;
|
| 155 |
```
|
| 156 |
|
| 157 |
-
*Returns:* `!(x < y)`
|
| 158 |
|
| 159 |
``` cpp
|
| 160 |
template<class charT, class traits>
|
| 161 |
basic_ostream<charT, traits>&
|
| 162 |
-
operator<< (basic_ostream<charT, traits>&
|
| 163 |
```
|
| 164 |
|
| 165 |
*Effects:* Inserts an unspecified text representation of `id` into
|
| 166 |
`out`. For two objects of type `thread::id` `x` and `y`, if `x == y` the
|
| 167 |
`thread::id` objects shall have the same text representation and if
|
| 168 |
`x != y` the `thread::id` objects shall have distinct text
|
| 169 |
representations.
|
| 170 |
|
| 171 |
-
*Returns:* `out`
|
| 172 |
|
| 173 |
``` cpp
|
| 174 |
template <> struct hash<thread::id>;
|
| 175 |
```
|
| 176 |
|
| 177 |
-
The
|
| 178 |
-
template `hash` ([[unord.hash]]).
|
| 179 |
|
| 180 |
#### `thread` constructors <a id="thread.thread.constr">[[thread.thread.constr]]</a>
|
| 181 |
|
| 182 |
``` cpp
|
| 183 |
thread() noexcept;
|
| 184 |
```
|
| 185 |
|
| 186 |
*Effects:* Constructs a `thread` object that does not represent a thread
|
| 187 |
of execution.
|
| 188 |
|
| 189 |
-
`get_id() == id()`
|
| 190 |
|
| 191 |
``` cpp
|
| 192 |
template <class F, class... Args> explicit thread(F&& f, Args&&... args);
|
| 193 |
```
|
| 194 |
|
| 195 |
*Requires:* `F` and each `Ti` in `Args` shall satisfy the
|
| 196 |
-
`MoveConstructible` requirements.
|
| 197 |
-
`std::forward<F>(f)), `*`DECAY_COPY`*`(std::forward<Args>(args))...)` ([[func.require]])
|
| 198 |
shall be a valid expression.
|
| 199 |
|
| 200 |
*Remarks:* This constructor shall not participate in overload resolution
|
| 201 |
if `decay_t<F>` is the same type as `std::thread`.
|
| 202 |
|
| 203 |
*Effects:* Constructs an object of type `thread`. The new thread of
|
| 204 |
execution executes
|
| 205 |
-
*`INVOKE`*`(`*`DECAY_COPY`*`(
|
| 206 |
with the calls to *`DECAY_COPY`* being evaluated in the constructing
|
| 207 |
-
thread. Any return value from this invocation is ignored.
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
invocation of
|
| 211 |
-
|
| 212 |
-
|
|
|
|
|
|
|
|
|
|
| 213 |
|
| 214 |
*Synchronization:* The completion of the invocation of the constructor
|
| 215 |
synchronizes with the beginning of the invocation of the copy of `f`.
|
| 216 |
|
| 217 |
*Postconditions:* `get_id() != id()`. `*this` represents the newly
|
|
@@ -239,30 +247,31 @@ of `x.get_id()` prior to the start of construction.
|
|
| 239 |
|
| 240 |
``` cpp
|
| 241 |
~thread();
|
| 242 |
```
|
| 243 |
|
| 244 |
-
If `joinable()`, calls `
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
|
|
|
| 250 |
|
| 251 |
#### `thread` assignment <a id="thread.thread.assign">[[thread.thread.assign]]</a>
|
| 252 |
|
| 253 |
``` cpp
|
| 254 |
thread& operator=(thread&& x) noexcept;
|
| 255 |
```
|
| 256 |
|
| 257 |
-
*Effects:* If `joinable()`, calls `
|
| 258 |
-
|
| 259 |
|
| 260 |
*Postconditions:* `x.get_id() == id()` and `get_id()` returns the value
|
| 261 |
of `x.get_id()` prior to the assignment.
|
| 262 |
|
| 263 |
-
*Returns:* `*this`
|
| 264 |
|
| 265 |
#### `thread` members <a id="thread.thread.member">[[thread.thread.member]]</a>
|
| 266 |
|
| 267 |
``` cpp
|
| 268 |
void swap(thread& x) noexcept;
|
|
@@ -272,51 +281,49 @@ void swap(thread& x) noexcept;
|
|
| 272 |
|
| 273 |
``` cpp
|
| 274 |
bool joinable() const noexcept;
|
| 275 |
```
|
| 276 |
|
| 277 |
-
*Returns:* `get_id() != id()`
|
| 278 |
|
| 279 |
``` cpp
|
| 280 |
void join();
|
| 281 |
```
|
| 282 |
|
| 283 |
-
`joinable()` is `true`.
|
| 284 |
-
|
| 285 |
*Effects:* Blocks until the thread represented by `*this` has
|
| 286 |
completed.
|
| 287 |
|
| 288 |
*Synchronization:* The completion of the thread represented by `*this`
|
| 289 |
synchronizes with ([[intro.multithread]]) the corresponding successful
|
| 290 |
-
`join()` return.
|
|
|
|
|
|
|
| 291 |
|
| 292 |
*Postconditions:* The thread represented by `*this` has completed.
|
| 293 |
`get_id() == id()`.
|
| 294 |
|
| 295 |
*Throws:* `system_error` when an exception is
|
| 296 |
required ([[thread.req.exception]]).
|
| 297 |
|
| 298 |
*Error conditions:*
|
| 299 |
|
| 300 |
- `resource_deadlock_would_occur` — if deadlock is detected or
|
| 301 |
-
`
|
| 302 |
- `no_such_process` — if the thread is not valid.
|
| 303 |
- `invalid_argument` — if the thread is not joinable.
|
| 304 |
|
| 305 |
``` cpp
|
| 306 |
void detach();
|
| 307 |
```
|
| 308 |
|
| 309 |
-
`joinable()` is `true`.
|
| 310 |
-
|
| 311 |
*Effects:* The thread represented by `*this` continues execution without
|
| 312 |
the calling thread blocking. When `detach()` returns, `*this` no longer
|
| 313 |
represents the possibly continuing thread of execution. When the thread
|
| 314 |
previously represented by `*this` ends execution, the implementation
|
| 315 |
shall release any owned resources.
|
| 316 |
|
| 317 |
-
`get_id() == id()`.
|
| 318 |
|
| 319 |
*Throws:* `system_error` when an exception is
|
| 320 |
required ([[thread.req.exception]]).
|
| 321 |
|
| 322 |
*Error conditions:*
|
|
@@ -336,36 +343,38 @@ execution represented by `*this`.
|
|
| 336 |
|
| 337 |
``` cpp
|
| 338 |
unsigned hardware_concurrency() noexcept;
|
| 339 |
```
|
| 340 |
|
| 341 |
-
*Returns:* The number of hardware thread contexts.
|
| 342 |
-
|
| 343 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 344 |
|
| 345 |
#### `thread` specialized algorithms <a id="thread.thread.algorithm">[[thread.thread.algorithm]]</a>
|
| 346 |
|
| 347 |
``` cpp
|
| 348 |
void swap(thread& x, thread& y) noexcept;
|
| 349 |
```
|
| 350 |
|
| 351 |
-
*Effects:* `x.swap(y)`
|
| 352 |
|
| 353 |
### Namespace `this_thread` <a id="thread.thread.this">[[thread.thread.this]]</a>
|
| 354 |
|
| 355 |
``` cpp
|
| 356 |
-
namespace std {
|
| 357 |
-
namespace this_thread {
|
| 358 |
thread::id get_id() noexcept;
|
| 359 |
|
| 360 |
void yield() noexcept;
|
| 361 |
template <class Clock, class Duration>
|
| 362 |
void sleep_until(const chrono::time_point<Clock, Duration>& abs_time);
|
| 363 |
template <class Rep, class Period>
|
| 364 |
void sleep_for(const chrono::duration<Rep, Period>& rel_time);
|
| 365 |
}
|
| 366 |
-
}
|
| 367 |
```
|
| 368 |
|
| 369 |
``` cpp
|
| 370 |
thread::id this_thread::get_id() noexcept;
|
| 371 |
```
|
|
|
|
| 1 |
## Threads <a id="thread.threads">[[thread.threads]]</a>
|
| 2 |
|
| 3 |
[[thread.threads]] describes components that can be used to create and
|
| 4 |
+
manage threads.
|
| 5 |
+
|
| 6 |
+
[*Note 1*: These threads are intended to map one-to-one with operating
|
| 7 |
+
system threads. — *end note*]
|
| 8 |
+
|
| 9 |
+
### Header `<thread>` synopsis <a id="thread.syn">[[thread.syn]]</a>
|
| 10 |
|
| 11 |
``` cpp
|
| 12 |
namespace std {
|
| 13 |
class thread;
|
| 14 |
|
|
|
|
| 35 |
execution. That representation may be transferred to other `thread`
|
| 36 |
objects in such a way that no two `thread` objects simultaneously
|
| 37 |
represent the same thread of execution. A thread of execution is
|
| 38 |
*detached* when no `thread` object represents that thread. Objects of
|
| 39 |
class `thread` can be in a state that does not represent a thread of
|
| 40 |
+
execution.
|
| 41 |
+
|
| 42 |
+
[*Note 1*: A `thread` object does not represent a thread of execution
|
| 43 |
after default construction, after being moved from, or after a
|
| 44 |
+
successful call to `detach` or `join`. — *end note*]
|
| 45 |
|
| 46 |
``` cpp
|
| 47 |
namespace std {
|
| 48 |
class thread {
|
| 49 |
public:
|
| 50 |
// types:
|
| 51 |
class id;
|
| 52 |
+
using native_handle_type = implementation-defined; // See~[thread.req.native]
|
| 53 |
|
| 54 |
// construct/copy/destroy:
|
| 55 |
thread() noexcept;
|
| 56 |
template <class F, class... Args> explicit thread(F&& f, Args&&... args);
|
| 57 |
~thread();
|
|
|
|
| 103 |
An object of type `thread::id` provides a unique identifier for each
|
| 104 |
thread of execution and a single distinct value for all `thread` objects
|
| 105 |
that do not represent a thread of execution ([[thread.thread.class]]).
|
| 106 |
Each thread of execution has an associated `thread::id` object that is
|
| 107 |
not equal to the `thread::id` object of any other thread of execution
|
| 108 |
+
and that is not equal to the `thread::id` object of any `thread` object
|
| 109 |
+
that does not represent threads of execution.
|
| 110 |
|
| 111 |
`thread::id` shall be a trivially copyable class (Clause [[class]]).
|
| 112 |
The library may reuse the value of a `thread::id` of a terminated thread
|
| 113 |
that can no longer be joined.
|
| 114 |
|
| 115 |
+
[*Note 1*: Relational operators allow `thread::id` objects to be used
|
| 116 |
+
as keys in associative containers. — *end note*]
|
| 117 |
|
| 118 |
``` cpp
|
| 119 |
id() noexcept;
|
| 120 |
```
|
| 121 |
|
|
|
|
| 146 |
|
| 147 |
``` cpp
|
| 148 |
bool operator<=(thread::id x, thread::id y) noexcept;
|
| 149 |
```
|
| 150 |
|
| 151 |
+
*Returns:* `!(y < x)`.
|
| 152 |
|
| 153 |
``` cpp
|
| 154 |
bool operator>(thread::id x, thread::id y) noexcept;
|
| 155 |
```
|
| 156 |
|
| 157 |
+
*Returns:* `y < x`.
|
| 158 |
|
| 159 |
``` cpp
|
| 160 |
bool operator>=(thread::id x, thread::id y) noexcept;
|
| 161 |
```
|
| 162 |
|
| 163 |
+
*Returns:* `!(x < y)`.
|
| 164 |
|
| 165 |
``` cpp
|
| 166 |
template<class charT, class traits>
|
| 167 |
basic_ostream<charT, traits>&
|
| 168 |
+
operator<< (basic_ostream<charT, traits>& out, thread::id id);
|
| 169 |
```
|
| 170 |
|
| 171 |
*Effects:* Inserts an unspecified text representation of `id` into
|
| 172 |
`out`. For two objects of type `thread::id` `x` and `y`, if `x == y` the
|
| 173 |
`thread::id` objects shall have the same text representation and if
|
| 174 |
`x != y` the `thread::id` objects shall have distinct text
|
| 175 |
representations.
|
| 176 |
|
| 177 |
+
*Returns:* `out`.
|
| 178 |
|
| 179 |
``` cpp
|
| 180 |
template <> struct hash<thread::id>;
|
| 181 |
```
|
| 182 |
|
| 183 |
+
The specialization is enabled ([[unord.hash]]).
|
|
|
|
| 184 |
|
| 185 |
#### `thread` constructors <a id="thread.thread.constr">[[thread.thread.constr]]</a>
|
| 186 |
|
| 187 |
``` cpp
|
| 188 |
thread() noexcept;
|
| 189 |
```
|
| 190 |
|
| 191 |
*Effects:* Constructs a `thread` object that does not represent a thread
|
| 192 |
of execution.
|
| 193 |
|
| 194 |
+
*Postconditions:* `get_id() == id()`.
|
| 195 |
|
| 196 |
``` cpp
|
| 197 |
template <class F, class... Args> explicit thread(F&& f, Args&&... args);
|
| 198 |
```
|
| 199 |
|
| 200 |
*Requires:* `F` and each `Ti` in `Args` shall satisfy the
|
| 201 |
+
`MoveConstructible` requirements.
|
| 202 |
+
` `*`INVOKE`*`( `*`DECAY_COPY`*`( std::forward<F>(f)), `*`DECAY_COPY`*`( std::forward<Args>(args))...)` ([[func.require]])
|
| 203 |
shall be a valid expression.
|
| 204 |
|
| 205 |
*Remarks:* This constructor shall not participate in overload resolution
|
| 206 |
if `decay_t<F>` is the same type as `std::thread`.
|
| 207 |
|
| 208 |
*Effects:* Constructs an object of type `thread`. The new thread of
|
| 209 |
execution executes
|
| 210 |
+
` `*`INVOKE`*`( `*`DECAY_COPY`*`( std::forward<F>(f)), `*`DECAY_COPY`*`( std::forward<Args>(args))...)`
|
| 211 |
with the calls to *`DECAY_COPY`* being evaluated in the constructing
|
| 212 |
+
thread. Any return value from this invocation is ignored.
|
| 213 |
+
|
| 214 |
+
[*Note 1*: This implies that any exceptions not thrown from the
|
| 215 |
+
invocation of the copy of `f` will be thrown in the constructing thread,
|
| 216 |
+
not the new thread. — *end note*]
|
| 217 |
+
|
| 218 |
+
If the invocation of
|
| 219 |
+
` `*`INVOKE`*`( `*`DECAY_COPY`*`( std::forward<F>(f)), `*`DECAY_COPY`*`( std::forward<Args>(args))...)`
|
| 220 |
+
terminates with an uncaught exception, `terminate` shall be called.
|
| 221 |
|
| 222 |
*Synchronization:* The completion of the invocation of the constructor
|
| 223 |
synchronizes with the beginning of the invocation of the copy of `f`.
|
| 224 |
|
| 225 |
*Postconditions:* `get_id() != id()`. `*this` represents the newly
|
|
|
|
| 247 |
|
| 248 |
``` cpp
|
| 249 |
~thread();
|
| 250 |
```
|
| 251 |
|
| 252 |
+
If `joinable()`, calls `terminate()`. Otherwise, has no effects.
|
| 253 |
+
|
| 254 |
+
[*Note 1*: Either implicitly detaching or joining a `joinable()` thread
|
| 255 |
+
in its destructor could result in difficult to debug correctness (for
|
| 256 |
+
detach) or performance (for join) bugs encountered only when an
|
| 257 |
+
exception is thrown. Thus the programmer must ensure that the destructor
|
| 258 |
+
is never executed while the thread is still joinable. — *end note*]
|
| 259 |
|
| 260 |
#### `thread` assignment <a id="thread.thread.assign">[[thread.thread.assign]]</a>
|
| 261 |
|
| 262 |
``` cpp
|
| 263 |
thread& operator=(thread&& x) noexcept;
|
| 264 |
```
|
| 265 |
|
| 266 |
+
*Effects:* If `joinable()`, calls `terminate()`. Otherwise, assigns the
|
| 267 |
+
state of `x` to `*this` and sets `x` to a default constructed state.
|
| 268 |
|
| 269 |
*Postconditions:* `x.get_id() == id()` and `get_id()` returns the value
|
| 270 |
of `x.get_id()` prior to the assignment.
|
| 271 |
|
| 272 |
+
*Returns:* `*this`.
|
| 273 |
|
| 274 |
#### `thread` members <a id="thread.thread.member">[[thread.thread.member]]</a>
|
| 275 |
|
| 276 |
``` cpp
|
| 277 |
void swap(thread& x) noexcept;
|
|
|
|
| 281 |
|
| 282 |
``` cpp
|
| 283 |
bool joinable() const noexcept;
|
| 284 |
```
|
| 285 |
|
| 286 |
+
*Returns:* `get_id() != id()`.
|
| 287 |
|
| 288 |
``` cpp
|
| 289 |
void join();
|
| 290 |
```
|
| 291 |
|
|
|
|
|
|
|
| 292 |
*Effects:* Blocks until the thread represented by `*this` has
|
| 293 |
completed.
|
| 294 |
|
| 295 |
*Synchronization:* The completion of the thread represented by `*this`
|
| 296 |
synchronizes with ([[intro.multithread]]) the corresponding successful
|
| 297 |
+
`join()` return.
|
| 298 |
+
|
| 299 |
+
[*Note 1*: Operations on `*this` are not synchronized. — *end note*]
|
| 300 |
|
| 301 |
*Postconditions:* The thread represented by `*this` has completed.
|
| 302 |
`get_id() == id()`.
|
| 303 |
|
| 304 |
*Throws:* `system_error` when an exception is
|
| 305 |
required ([[thread.req.exception]]).
|
| 306 |
|
| 307 |
*Error conditions:*
|
| 308 |
|
| 309 |
- `resource_deadlock_would_occur` — if deadlock is detected or
|
| 310 |
+
`get_id() == this_thread::get_id()`.
|
| 311 |
- `no_such_process` — if the thread is not valid.
|
| 312 |
- `invalid_argument` — if the thread is not joinable.
|
| 313 |
|
| 314 |
``` cpp
|
| 315 |
void detach();
|
| 316 |
```
|
| 317 |
|
|
|
|
|
|
|
| 318 |
*Effects:* The thread represented by `*this` continues execution without
|
| 319 |
the calling thread blocking. When `detach()` returns, `*this` no longer
|
| 320 |
represents the possibly continuing thread of execution. When the thread
|
| 321 |
previously represented by `*this` ends execution, the implementation
|
| 322 |
shall release any owned resources.
|
| 323 |
|
| 324 |
+
*Postconditions:* `get_id() == id()`.
|
| 325 |
|
| 326 |
*Throws:* `system_error` when an exception is
|
| 327 |
required ([[thread.req.exception]]).
|
| 328 |
|
| 329 |
*Error conditions:*
|
|
|
|
| 343 |
|
| 344 |
``` cpp
|
| 345 |
unsigned hardware_concurrency() noexcept;
|
| 346 |
```
|
| 347 |
|
| 348 |
+
*Returns:* The number of hardware thread contexts.
|
| 349 |
+
|
| 350 |
+
[*Note 1*: This value should only be considered to be a
|
| 351 |
+
hint. — *end note*]
|
| 352 |
+
|
| 353 |
+
If this value is not computable or well defined an implementation should
|
| 354 |
+
return 0.
|
| 355 |
|
| 356 |
#### `thread` specialized algorithms <a id="thread.thread.algorithm">[[thread.thread.algorithm]]</a>
|
| 357 |
|
| 358 |
``` cpp
|
| 359 |
void swap(thread& x, thread& y) noexcept;
|
| 360 |
```
|
| 361 |
|
| 362 |
+
*Effects:* As if by `x.swap(y)`.
|
| 363 |
|
| 364 |
### Namespace `this_thread` <a id="thread.thread.this">[[thread.thread.this]]</a>
|
| 365 |
|
| 366 |
``` cpp
|
| 367 |
+
namespace std::this_thread {
|
|
|
|
| 368 |
thread::id get_id() noexcept;
|
| 369 |
|
| 370 |
void yield() noexcept;
|
| 371 |
template <class Clock, class Duration>
|
| 372 |
void sleep_until(const chrono::time_point<Clock, Duration>& abs_time);
|
| 373 |
template <class Rep, class Period>
|
| 374 |
void sleep_for(const chrono::duration<Rep, Period>& rel_time);
|
| 375 |
}
|
|
|
|
| 376 |
```
|
| 377 |
|
| 378 |
``` cpp
|
| 379 |
thread::id this_thread::get_id() noexcept;
|
| 380 |
```
|