From Jason Turner

[thread.threads]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmp1s7aza06/{from.md → to.md} +75 -45
tmp/tmp1s7aza06/{from.md → to.md} RENAMED
@@ -1,27 +1,30 @@
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
  #include <compare> // see [compare.syn]
13
- #include <initializer_list> // see [initializer.list.syn]
14
 
15
  namespace std {
 
16
  class thread;
17
 
18
  void swap(thread& x, thread& y) noexcept;
19
 
20
- // [thread.jthread.class] class jthread
21
  class jthread;
22
 
 
23
  namespace this_thread {
24
  thread::id get_id() noexcept;
25
 
26
  void yield() noexcept;
27
  template<class Clock, class Duration>
@@ -32,10 +35,12 @@ namespace std {
32
  }
33
  ```
34
 
35
  ### Class `thread` <a id="thread.thread.class">[[thread.thread.class]]</a>
36
 
 
 
37
  The class `thread` provides a mechanism to create a new thread of
38
  execution, to join with a thread (i.e., wait for a thread to complete),
39
  and to perform other operations that manage and query the state of a
40
  thread. A `thread` object uniquely represents a particular thread of
41
  execution. That representation may be transferred to other `thread`
@@ -51,11 +56,11 @@ successful call to `detach` or `join`. — *end note*]
51
 
52
  ``` cpp
53
  namespace std {
54
  class thread {
55
  public:
56
- // types
57
  class id;
58
  using native_handle_type = implementation-defined; // see~[thread.req.native]
59
 
60
  // construct/copy/destroy
61
  thread() noexcept;
@@ -64,11 +69,11 @@ namespace std {
64
  thread(const thread&) = delete;
65
  thread(thread&&) noexcept;
66
  thread& operator=(const thread&) = delete;
67
  thread& operator=(thread&&) noexcept;
68
 
69
- // members
70
  void swap(thread&) noexcept;
71
  bool joinable() const noexcept;
72
  void join();
73
  void detach();
74
  id get_id() const noexcept;
@@ -94,10 +99,12 @@ namespace std {
94
 
95
  template<class charT, class traits>
96
  basic_ostream<charT, traits>&
97
  operator<<(basic_ostream<charT, traits>& out, thread::id id);
98
 
 
 
99
  // hash support
100
  template<class T> struct hash;
101
  template<> struct hash<thread::id>;
102
  }
103
  ```
@@ -108,10 +115,16 @@ that do not represent a thread of execution [[thread.thread.class]].
108
  Each thread of execution has an associated `thread::id` object that is
109
  not equal to the `thread::id` object of any other thread of execution
110
  and that is not equal to the `thread::id` object of any `thread` object
111
  that does not represent threads of execution.
112
 
 
 
 
 
 
 
113
  `thread::id` is a trivially copyable class [[class.prop]]. The library
114
  may reuse the value of a `thread::id` of a terminated thread that can no
115
  longer be joined.
116
 
117
  [*Note 1*: Relational operators allow `thread::id` objects to be used
@@ -146,17 +159,37 @@ described in [[alg.sorting]].
146
  template<class charT, class traits>
147
  basic_ostream<charT, traits>&
148
  operator<< (basic_ostream<charT, traits>& out, thread::id id);
149
  ```
150
 
151
- *Effects:* Inserts an unspecified text representation of `id` into
152
- `out`. For two objects of type `thread::id` `x` and `y`, if `x == y` the
153
- `thread::id` objects have the same text representation and if `x != y`
154
- the `thread::id` objects have distinct text representations.
155
 
156
  *Returns:* `out`.
157
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
158
  ``` cpp
159
  template<> struct hash<thread::id>;
160
  ```
161
 
162
  The specialization is enabled [[unord.hash]].
@@ -178,33 +211,30 @@ template<class F, class... Args> explicit thread(F&& f, Args&&... args);
178
  *Constraints:* `remove_cvref_t<F>` is not the same type as `thread`.
179
 
180
  *Mandates:* The following are all `true`:
181
 
182
  - `is_constructible_v<decay_t<F>, F>`,
183
- - `(is_constructible_v<decay_t<Args>, Args> && ...)`,
184
- - `is_move_constructible_v<decay_t<F>>`,
185
- - `(is_move_constructible_v<decay_t<Args>> && ...)`, and
186
  - `is_invocable_v<decay_t<F>, decay_t<Args>...>`.
187
 
188
- *Preconditions:* `decay_t<F>` and each type in `decay_t<Args>` meet the
189
- *Cpp17MoveConstructible* requirements.
190
-
191
  *Effects:* The new thread of execution executes
192
 
193
  ``` cpp
194
- invoke(decay-copy(std::forward<F>(f)), decay-copy(std::forward<Args>(args))...)
 
195
  ```
196
 
197
- with the calls to *`decay-copy`* being evaluated in the constructing
198
- thread. Any return value from this invocation is ignored.
 
199
 
200
  [*Note 1*: This implies that any exceptions not thrown from the
201
  invocation of the copy of `f` will be thrown in the constructing thread,
202
  not the new thread. — *end note*]
203
 
204
  If the invocation of `invoke` terminates with an uncaught exception,
205
- `terminate` is called.
206
 
207
  *Synchronization:* The completion of the invocation of the constructor
208
  synchronizes with the beginning of the invocation of the copy of `f`.
209
 
210
  *Ensures:* `get_id() != id()`. `*this` represents the newly started
@@ -229,27 +259,29 @@ thread(thread&& x) noexcept;
229
 
230
  ``` cpp
231
  ~thread();
232
  ```
233
 
234
- *Effects:* If `joinable()`, calls `terminate()`. Otherwise, has no
235
- effects.
236
 
237
  [*Note 1*: Either implicitly detaching or joining a `joinable()` thread
238
- in its destructor could result in difficult to debug correctness (for
239
  detach) or performance (for join) bugs encountered only when an
240
- exception is thrown. Thus the programmer must ensure that the destructor
241
- is never executed while the thread is still joinable. — *end note*]
 
242
 
243
  #### Assignment <a id="thread.thread.assign">[[thread.thread.assign]]</a>
244
 
245
  ``` cpp
246
  thread& operator=(thread&& x) noexcept;
247
  ```
248
 
249
- *Effects:* If `joinable()`, calls `terminate()`. Otherwise, assigns the
250
- state of `x` to `*this` and sets `x` to a default constructed state.
 
251
 
252
  *Ensures:* `x.get_id() == id()` and `get_id()` returns the value of
253
  `x.get_id()` prior to the assignment.
254
 
255
  *Returns:* `*this`.
@@ -343,10 +375,12 @@ void swap(thread& x, thread& y) noexcept;
343
 
344
  *Effects:* As if by `x.swap(y)`.
345
 
346
  ### Class `jthread` <a id="thread.jthread.class">[[thread.jthread.class]]</a>
347
 
 
 
348
  The class `jthread` provides a mechanism to create a new thread of
349
  execution. The functionality is the same as for class `thread`
350
  [[thread.thread.class]] with the additional abilities to provide a
351
  `stop_token` [[thread.stoptoken]] to the new thread of execution, make
352
  stop requests, and automatically join.
@@ -412,34 +446,30 @@ template<class F, class... Args> explicit jthread(F&& f, Args&&... args);
412
  *Constraints:* `remove_cvref_t<F>` is not the same type as `jthread`.
413
 
414
  *Mandates:* The following are all `true`:
415
 
416
  - `is_constructible_v<decay_t<F>, F>`,
417
- - `(is_constructible_v<decay_t<Args>, Args> && ...)`,
418
- - `is_move_constructible_v<decay_t<F>>`,
419
- - `(is_move_constructible_v<decay_t<Args>> && ...)`, and
420
  - `is_invocable_v<decay_t<F>, decay_t<Args>...> ||`
421
  `is_invocable_v<decay_t<F>, stop_token, decay_t<Args>...>`.
422
 
423
- *Preconditions:* `decay_t<F>` and each type in `decay_t<Args>` meet the
424
- *Cpp17MoveConstructible* requirements.
425
-
426
  *Effects:* Initializes `ssource`. The new thread of execution executes
427
 
428
  ``` cpp
429
- invoke(decay-copy(std::forward<F>(f)), get_stop_token(),
430
- decay-copy(std::forward<Args>(args))...)
431
  ```
432
 
433
  if that expression is well-formed, otherwise
434
 
435
  ``` cpp
436
- invoke(decay-copy(std::forward<F>(f)), decay-copy(std::forward<Args>(args))...)
437
  ```
438
 
439
- with the calls to *`decay-copy`* being evaluated in the constructing
440
- thread. Any return value from this invocation is ignored.
 
441
 
442
  [*Note 1*: This implies that any exceptions not thrown from the
443
  invocation of the copy of `f` will be thrown in the constructing thread,
444
  not the new thread. — *end note*]
445
 
@@ -483,18 +513,18 @@ of `x.ssource` prior to the start of construction and
483
 
484
  ``` cpp
485
  jthread& operator=(jthread&& x) noexcept;
486
  ```
487
 
488
- *Effects:* If `joinable()` is `true`, calls `request_stop()` and then
489
- `join()`. Assigns the state of `x` to `*this` and sets `x` to a default
 
490
  constructed state.
491
 
492
- *Ensures:* `x.get_id() == id()` and `get_id()` returns the value of
493
- `x.get_id()` prior to the assignment. `ssource` has the value of
494
- `x.ssource` prior to the assignment and `x.ssource.stop_possible()` is
495
- `false`.
496
 
497
  *Returns:* `*this`.
498
 
499
  #### Members <a id="thread.jthread.mem">[[thread.jthread.mem]]</a>
500
 
@@ -616,13 +646,13 @@ namespace std::this_thread {
616
  ``` cpp
617
  thread::id this_thread::get_id() noexcept;
618
  ```
619
 
620
  *Returns:* An object of type `thread::id` that uniquely identifies the
621
- current thread of execution. No other thread of execution has this id
622
- and this thread of execution always has this id. The object returned
623
- does not compare equal to a default constructed `thread::id`.
624
 
625
  ``` cpp
626
  void this_thread::yield() noexcept;
627
  ```
628
 
 
1
  ## Threads <a id="thread.threads">[[thread.threads]]</a>
2
 
3
+ ### General <a id="thread.threads.general">[[thread.threads.general]]</a>
4
+
5
  [[thread.threads]] describes components that can be used to create and
6
  manage threads.
7
 
8
  [*Note 1*: These threads are intended to map one-to-one with operating
9
  system threads. — *end note*]
10
 
11
  ### Header `<thread>` synopsis <a id="thread.syn">[[thread.syn]]</a>
12
 
13
  ``` cpp
14
  #include <compare> // see [compare.syn]
 
15
 
16
  namespace std {
17
+ // [thread.thread.class], class thread
18
  class thread;
19
 
20
  void swap(thread& x, thread& y) noexcept;
21
 
22
+ // [thread.jthread.class], class jthread
23
  class jthread;
24
 
25
+ // [thread.thread.this], namespace this_thread
26
  namespace this_thread {
27
  thread::id get_id() noexcept;
28
 
29
  void yield() noexcept;
30
  template<class Clock, class Duration>
 
35
  }
36
  ```
37
 
38
  ### Class `thread` <a id="thread.thread.class">[[thread.thread.class]]</a>
39
 
40
+ #### General <a id="thread.thread.class.general">[[thread.thread.class.general]]</a>
41
+
42
  The class `thread` provides a mechanism to create a new thread of
43
  execution, to join with a thread (i.e., wait for a thread to complete),
44
  and to perform other operations that manage and query the state of a
45
  thread. A `thread` object uniquely represents a particular thread of
46
  execution. That representation may be transferred to other `thread`
 
56
 
57
  ``` cpp
58
  namespace std {
59
  class thread {
60
  public:
61
+ // [thread.thread.id], class thread::id
62
  class id;
63
  using native_handle_type = implementation-defined; // see~[thread.req.native]
64
 
65
  // construct/copy/destroy
66
  thread() noexcept;
 
69
  thread(const thread&) = delete;
70
  thread(thread&&) noexcept;
71
  thread& operator=(const thread&) = delete;
72
  thread& operator=(thread&&) noexcept;
73
 
74
+ // [thread.thread.member], members
75
  void swap(thread&) noexcept;
76
  bool joinable() const noexcept;
77
  void join();
78
  void detach();
79
  id get_id() const noexcept;
 
99
 
100
  template<class charT, class traits>
101
  basic_ostream<charT, traits>&
102
  operator<<(basic_ostream<charT, traits>& out, thread::id id);
103
 
104
+ template<class charT> struct formatter<thread::id, charT>;
105
+
106
  // hash support
107
  template<class T> struct hash;
108
  template<> struct hash<thread::id>;
109
  }
110
  ```
 
115
  Each thread of execution has an associated `thread::id` object that is
116
  not equal to the `thread::id` object of any other thread of execution
117
  and that is not equal to the `thread::id` object of any `thread` object
118
  that does not represent threads of execution.
119
 
120
+ The *text representation* for the character type `charT` of an object of
121
+ type `thread::id` is an unspecified sequence of `charT` such that, for
122
+ two objects of type `thread::id` `x` and `y`, if `x == y` is `true`, the
123
+ `thread::id` objects have the same text representation, and if `x != y`
124
+ is `true`, the `thread::id` objects have distinct text representations.
125
+
126
  `thread::id` is a trivially copyable class [[class.prop]]. The library
127
  may reuse the value of a `thread::id` of a terminated thread that can no
128
  longer be joined.
129
 
130
  [*Note 1*: Relational operators allow `thread::id` objects to be used
 
159
  template<class charT, class traits>
160
  basic_ostream<charT, traits>&
161
  operator<< (basic_ostream<charT, traits>& out, thread::id id);
162
  ```
163
 
164
+ *Effects:* Inserts the text representation for `charT` of `id` into
165
+ `out`.
 
 
166
 
167
  *Returns:* `out`.
168
 
169
+ ``` cpp
170
+ template<class charT> struct formatter<thread::id, charT>;
171
+ ```
172
+
173
+ `formatter<thread::id, charT>` interprets *format-spec* as a
174
+ *thread-id-format-spec*. The syntax of format specifications is as
175
+ follows:
176
+
177
+ ``` bnf
178
+ thread-id-format-spec
179
+ fill-and-alignₒₚₜ widthₒₚₜ
180
+ ```
181
+
182
+ [*Note 1*: The productions *fill-and-align* and *width* are described
183
+ in [[format.string.std]]. — *end note*]
184
+
185
+ If the *align* option is omitted it defaults to `>`.
186
+
187
+ A `thread::id` object is formatted by writing its text representation
188
+ for `charT` to the output with additional padding and adjustments as
189
+ specified by the format specifiers.
190
+
191
  ``` cpp
192
  template<> struct hash<thread::id>;
193
  ```
194
 
195
  The specialization is enabled [[unord.hash]].
 
211
  *Constraints:* `remove_cvref_t<F>` is not the same type as `thread`.
212
 
213
  *Mandates:* The following are all `true`:
214
 
215
  - `is_constructible_v<decay_t<F>, F>`,
216
+ - `(is_constructible_v<decay_t<Args>, Args> && ...)`, and
 
 
217
  - `is_invocable_v<decay_t<F>, decay_t<Args>...>`.
218
 
 
 
 
219
  *Effects:* The new thread of execution executes
220
 
221
  ``` cpp
222
+ invoke(auto(std::forward<F>(f)), // for invoke, see [func.invoke]
223
+ auto(std::forward<Args>(args))...)
224
  ```
225
 
226
+ with the values produced by `auto` being materialized [[conv.rval]] in
227
+ the constructing thread. Any return value from this invocation is
228
+ ignored.
229
 
230
  [*Note 1*: This implies that any exceptions not thrown from the
231
  invocation of the copy of `f` will be thrown in the constructing thread,
232
  not the new thread. — *end note*]
233
 
234
  If the invocation of `invoke` terminates with an uncaught exception,
235
+ `terminate` is invoked [[except.terminate]].
236
 
237
  *Synchronization:* The completion of the invocation of the constructor
238
  synchronizes with the beginning of the invocation of the copy of `f`.
239
 
240
  *Ensures:* `get_id() != id()`. `*this` represents the newly started
 
259
 
260
  ``` cpp
261
  ~thread();
262
  ```
263
 
264
+ *Effects:* If `joinable()`, invokes `terminate` [[except.terminate]].
265
+ Otherwise, has no effects.
266
 
267
  [*Note 1*: Either implicitly detaching or joining a `joinable()` thread
268
+ in its destructor can result in difficult to debug correctness (for
269
  detach) or performance (for join) bugs encountered only when an
270
+ exception is thrown. These bugs can be avoided by ensuring that the
271
+ destructor is never executed while the thread is still
272
+ joinable. — *end note*]
273
 
274
  #### Assignment <a id="thread.thread.assign">[[thread.thread.assign]]</a>
275
 
276
  ``` cpp
277
  thread& operator=(thread&& x) noexcept;
278
  ```
279
 
280
+ *Effects:* If `joinable()`, invokes `terminate` [[except.terminate]].
281
+ Otherwise, assigns the state of `x` to `*this` and sets `x` to a default
282
+ constructed state.
283
 
284
  *Ensures:* `x.get_id() == id()` and `get_id()` returns the value of
285
  `x.get_id()` prior to the assignment.
286
 
287
  *Returns:* `*this`.
 
375
 
376
  *Effects:* As if by `x.swap(y)`.
377
 
378
  ### Class `jthread` <a id="thread.jthread.class">[[thread.jthread.class]]</a>
379
 
380
+ #### General <a id="thread.jthread.class.general">[[thread.jthread.class.general]]</a>
381
+
382
  The class `jthread` provides a mechanism to create a new thread of
383
  execution. The functionality is the same as for class `thread`
384
  [[thread.thread.class]] with the additional abilities to provide a
385
  `stop_token` [[thread.stoptoken]] to the new thread of execution, make
386
  stop requests, and automatically join.
 
446
  *Constraints:* `remove_cvref_t<F>` is not the same type as `jthread`.
447
 
448
  *Mandates:* The following are all `true`:
449
 
450
  - `is_constructible_v<decay_t<F>, F>`,
451
+ - `(is_constructible_v<decay_t<Args>, Args> && ...)`, and
 
 
452
  - `is_invocable_v<decay_t<F>, decay_t<Args>...> ||`
453
  `is_invocable_v<decay_t<F>, stop_token, decay_t<Args>...>`.
454
 
 
 
 
455
  *Effects:* Initializes `ssource`. The new thread of execution executes
456
 
457
  ``` cpp
458
+ invoke(auto(std::forward<F>(f)), get_stop_token(), // for invoke, see [func.invoke]
459
+ auto(std::forward<Args>(args))...)
460
  ```
461
 
462
  if that expression is well-formed, otherwise
463
 
464
  ``` cpp
465
+ invoke(auto(std::forward<F>(f)), auto(std::forward<Args>(args))...)
466
  ```
467
 
468
+ with the values produced by `auto` being materialized [[conv.rval]] in
469
+ the constructing thread. Any return value from this invocation is
470
+ 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
 
 
513
 
514
  ``` cpp
515
  jthread& operator=(jthread&& x) noexcept;
516
  ```
517
 
518
+ *Effects:* If `&x == this` is `true`, there are no effects. Otherwise,
519
+ if `joinable()` is `true`, calls `request_stop()` and then `join()`,
520
+ then assigns the state of `x` to `*this` and sets `x` to a default
521
  constructed state.
522
 
523
+ *Ensures:* `get_id()` returns the value of `x.get_id()` prior to the
524
+ assignment. `ssource` has the value of `x.ssource` prior to the
525
+ assignment.
 
526
 
527
  *Returns:* `*this`.
528
 
529
  #### Members <a id="thread.jthread.mem">[[thread.jthread.mem]]</a>
530
 
 
646
  ``` cpp
647
  thread::id this_thread::get_id() noexcept;
648
  ```
649
 
650
  *Returns:* An object of type `thread::id` that uniquely identifies the
651
+ current thread of execution. Every invocation from this thread of
652
+ execution returns the same value. The object returned does not compare
653
+ equal to a default-constructed `thread::id`.
654
 
655
  ``` cpp
656
  void this_thread::yield() noexcept;
657
  ```
658