forked from cplusplus/draft
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreads.tex
More file actions
5080 lines (4099 loc) · 163 KB
/
threads.tex
File metadata and controls
5080 lines (4099 loc) · 163 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
%!TEX root = std.tex
\rSec0[thread]{Thread support library}
\rSec1[thread.general]{General}
\pnum
The following subclauses describe components to create and manage
threads~(\ref{intro.multithread}), perform mutual exclusion, and communicate conditions
and values
between threads, as summarized in Table~\ref{tab:thread.lib.summary}.
\begin{libsumtab}{Thread support library summary}{tab:thread.lib.summary}
\ref{thread.req} & Requirements & \\ \rowsep
\ref{thread.threads} & Threads & \tcode{<thread>} \\ \rowsep
\ref{thread.mutex} & Mutual exclusion & \tcode{<mutex>} \\
& & \tcode{<shared_mutex>} \\ \rowsep
\ref{thread.condition}& Condition variables & \tcode{<condition_variable>} \\ \rowsep
\ref{futures} & Futures & \tcode{<future>} \\
\end{libsumtab}
\rSec1[thread.req]{Requirements}
\rSec2[thread.req.paramname]{Template parameter names}
\pnum
Throughout this Clause, the names of template parameters are used to express type
requirements.
If a template parameter is named \tcode{Predicate}, \tcode{operator()} applied to
the template argument shall return a value that is convertible to \tcode{bool}.
\rSec2[thread.req.exception]{Exceptions}
\pnum
Some functions described in this Clause are specified to throw exceptions of type
\tcode{system_error}~(\ref{syserr.syserr}). Such exceptions shall be thrown if
any of the function's error conditions is detected or
a call to
an operating system or other underlying API results in an error that prevents the
library function from
meeting its specifications. Failure to allocate storage shall be reported as described
in~\ref{res.on.exception.handling}.
\enterexample
Consider a function in this clause that is specified to throw exceptions of type
\tcode{system_error} and specifies error conditions that include
\tcode{operation_not_permitted} for a thread that does not have the privilege to
perform the operation. Assume that, during the execution of this function, an \tcode{errno}
of \tcode{EPERM} is reported by a POSIX API call used by the implementation. Since POSIX
specifies an \tcode{errno} of \tcode{EPERM} when ``the caller does not have the privilege
to perform the operation'', the implementation maps \tcode{EPERM} to an
\tcode{error_condition} of \tcode{operation_not_permitted}~(\ref{syserr}) and an exception
of type \tcode{system_error} is thrown.
\exitexample
\pnum
The \tcode{error_code} reported by such an exception's \tcode{code()} member function
shall compare equal to one of the conditions specified in the function's error condition
element.
\rSec2[thread.req.native]{Native handles}
\pnum
Several classes described in this Clause have members \tcode{native_handle_type} and
\tcode{native_handle}. The presence of these members and their semantics is
\impldef{presence and meaning of \tcode{native_handle_type} and \tcode{native_handle}}.
\enternote These members allow implementations to provide access
to implementation details. Their names are specified to facilitate portable compile-time
detection. Actual use of these members is inherently non-portable. \exitnote
\rSec2[thread.req.timing]{Timing specifications}
\pnum
Several functions described in this Clause take an argument to specify a timeout. These
timeouts are specified as either a \tcode{duration} or a \tcode{time_point} type as
specified in~\ref{time}.
\pnum
Implementations necessarily have some delay in returning from a timeout. Any overhead in
interrupt response, function return, and scheduling induces a ``quality of implementation''
delay, expressed as duration $D_i$. Ideally, this delay would be zero. Further, any contention for
processor and memory resources induces a ``quality of management'' delay, expressed as duration
$D_m$. The delay durations may vary from timeout to timeout, but in all cases shorter is better.
\pnum
The member functions whose names end in \tcode{_for} take an argument that
specifies a duration. These functions produce relative timeouts. Implementations
should use a steady clock to measure time for these functions.\footnote{All
implementations for which standard time units are meaningful must necessarily
have a steady clock within their hardware implementation.} Given a duration
argument $D_t$, the real-time duration of the timeout is $D_t + D_i + D_m$.
\pnum
The member functions whose names end in \tcode{_until} take an argument that specifies a time
point. These functions produce absolute timeouts. Implementations should use the clock
specified in the time point to measure time for these functions. Given a clock time point
argument $C_t$, the clock time point of the return from timeout should be $C_t + D_i + D_m$
when the clock is not adjusted during the timeout. If the clock is adjusted to the time $C_a$
during the timeout, the behavior should be as follows:
\begin{itemize}
\item
if $C_a > C_t$, the waiting function should wake as soon as possible, i.e. $C_a + D_i + D_m$,
since the timeout is already satisfied. \enternote This specification may result in the total
duration of the wait decreasing when measured against a steady clock. \exitnote
\item
if $C_a <= C_t$, the waiting function should not time out until \tcode{Clock::now()} returns a
time $C_n >= C_t$, i.e. waking at $C_t + D_i + D_m$. \enternote When the clock is adjusted
backwards, this specification may result in the total duration of the wait increasing when
measured against a steady clock. When the clock is adjusted forwards, this specification may
result in the total duration of the wait decreasing when measured against a steady clock.
\exitnote
\end{itemize}
An implementation shall return from such a timeout at any point from the time specified above to
the time it would return from a steady-clock relative timeout on the difference between $C_t$
and the time point of the call to the \tcode{_until} function. \enternote Implementations
should decrease the duration of the wait when the clock is adjusted forwards.
\exitnote
\pnum
\enternote If the clock is not synchronized with a steady clock, e.g., a CPU time clock, these
timeouts might not provide useful functionality. \exitnote
\pnum
The resolution of timing provided by an implementation depends on both operating system
and hardware. The finest resolution provided by an implementation is called the
\term{native resolution}.
\pnum
Implementation-provided clocks that are used for these functions shall meet the
\tcode{TrivialClock} requirements (\ref{time.clock.req}).
\pnum
A function that takes an argument which specifies a timeout will throw if,
during its execution, a clock, time point, or time duration throws an exception.
Such exceptions are referred to as \term{timeout-related exceptions}.
\enternote instantiations of clock, time point and duration types supplied by
the implementation as specified in~\ref{time.clock} do not throw exceptions.
\exitnote
\rSec2[thread.req.lockable]{Requirements for Lockable types}
\rSec3[thread.req.lockable.general]{In general}
\pnum
An \defn{execution agent} is an entity such as a thread that may perform work in parallel with
other execution agents. \enternote Implementations or users may introduce other kinds of
agents such as processes or thread-pool tasks. \exitnote The calling agent is determined by
context, e.g. the calling thread that contains the call, and so on.
\pnum
\enternote Some lockable objects are ``agent oblivious'' in that they work for any
execution agent model because they do not determine or store the agent's ID (e.g., an
ordinary spin lock). \exitnote
\pnum
The standard library templates \tcode{unique_lock}~(\ref{thread.lock.unique}),
\tcode{lock_guard}~(\ref{thread.lock.guard}), \tcode{lock},
\tcode{try_lock}~(\ref{thread.lock.algorithm}), and
\tcode{condition_variable_any}~(\ref{thread.condition.condvarany}) all operate on user-supplied
lockable objects. The \tcode{BasicLockable} requirements, the \tcode{Lockable} requirements,
and the \tcode{TimedLockable} requirements list the requirements imposed by these library types
in order to acquire or release ownership of a \tcode{lock} by a given execution agent.
\enternote The nature of any lock ownership and any synchronization it may entail are not part
of these requirements. \exitnote
\rSec3[thread.req.lockable.basic]{\tcode{BasicLockable} requirements}
\pnum
A type \tcode{L} meets the \tcode{BasicLockable} requirements if the following expressions are
well-formed and have the specified semantics (\tcode{m} denotes a value of type \tcode{L}).
\begin{itemdecl}
m.lock()
\end{itemdecl}
\begin{itemdescr}
\pnum
\effects Blocks until a lock can be acquired for the current execution agent. If an exception
is thrown then a lock shall not have been acquired for the current execution agent.
\end{itemdescr}
\begin{itemdecl}
m.unlock()
\end{itemdecl}
\begin{itemdescr}
\pnum
\requires The current execution agent shall hold a lock on \tcode{m}.
\pnum
\effects Releases a lock on \tcode{m} held by the current execution agent.
\pnum
\throws Nothing.
\end{itemdescr}
\rSec3[thread.req.lockable.req]{\tcode{Lockable} requirements}
\pnum
A type \tcode{L} meets the \tcode{Lockable} requirements if it meets the \tcode{BasicLockable}
requirements and the following expressions are well-formed and have the specified semantics
(\tcode{m} denotes a value of type \tcode{L}).
\begin{itemdecl}
m.try_lock()
\end{itemdecl}
\begin{itemdescr}
\pnum
\effects attempts to acquire a lock for the current execution agent without blocking. If an
exception is thrown then a lock shall not have been acquired for the current execution agent.
\pnum
\returntype \tcode{bool}.
\pnum
\returns \tcode{true} if the lock was acquired, \tcode{false} otherwise.
\end{itemdescr}
\rSec3[thread.req.lockable.timed]{\tcode{TimedLockable} requirements}
\pnum
A type \tcode{L} meets the \tcode{TimedLockable} requirements if it meets the \tcode{Lockable}
requirements and the following expressions are well-formed and have the specified semantics
(\tcode{m} denotes a value of type \tcode{L}, \tcode{rel_time} denotes a value of an
instantiation of \tcode{duration}~(\ref{time.duration}), and \tcode{abs_time} denotes a value
of an instantiation of \tcode{time_point}~(\ref{time.point})).
\begin{itemdecl}
m.try_lock_for(rel_time)
\end{itemdecl}
\begin{itemdescr}
\pnum
\effects attempts to acquire a lock for the current execution agent within the relative
timeout~(\ref{thread.req.timing}) specified by \tcode{rel_time}. The function shall not return
within the timeout specified by \tcode{rel_time} unless it has obtained a lock on \tcode{m}
for the current execution agent. If an exception is thrown then a lock shall not have been
acquired for the current execution agent.
\pnum
\returntype \tcode{bool}.
\pnum
\returns \tcode{true} if the lock was acquired, \tcode{false} otherwise.
\end{itemdescr}
\begin{itemdecl}
m.try_lock_until(abs_time)
\end{itemdecl}
\begin{itemdescr}
\pnum
\effects attempts to acquire a lock for the current execution agent before the absolute
timeout~(\ref{thread.req.timing}) specified by \tcode{abs_time}. The function shall not return
before the timeout specified by \tcode{abs_time} unless it has obtained a lock on \tcode{m} for
the current execution agent. If an exception is thrown then a lock shall not have been acquired
for the current execution agent.
\pnum
\returntype \tcode{bool}.
\pnum
\returns \tcode{true} if the lock was acquired, \tcode{false} otherwise.
\end{itemdescr}
\rSec2[thread.decaycopy]{\tcode{decay_copy}}
\pnum
In several places in this Clause the operation
\defnx{DECAY_COPY(x)}{DECAY_COPY} is used. All
such uses mean call the function \tcode{decay_copy(x)} and use the
result, where \tcode{decay_copy} is defined as follows:
\begin{codeblock}
template <class T> decay_t<T> decay_copy(T&& v)
{ return std::forward<T>(v); }
\end{codeblock}
\rSec1[thread.threads]{Threads}
\pnum
\ref{thread.threads} describes components that can be used to create and manage threads.
\enternote These threads are intended to map one-to-one with operating system threads.
\exitnote
\synopsis{Header \tcode{<thread>} synopsis}
\indexlibrary{\idxhdr{thread}}%
\begin{codeblock}
namespace std {
class thread;
void swap(thread& x, thread& y) noexcept;
namespace this_thread {
thread::id get_id() noexcept;
void yield() noexcept;
template <class Clock, class Duration>
void sleep_until(const chrono::time_point<Clock, Duration>& abs_time);
template <class Rep, class Period>
void sleep_for(const chrono::duration<Rep, Period>& rel_time);
}
}
\end{codeblock}
\rSec2[thread.thread.class]{Class \tcode{thread}}
\pnum
The class \tcode{thread} provides a mechanism to create a new thread of execution, to join with
a thread (i.e., wait for a thread to complete), and to perform other operations that manage and
query the state of a thread. A \tcode{thread} object uniquely represents a particular thread of
execution. That representation may be transferred to other \tcode{thread} objects in such a way
that no two \tcode{thread} objects simultaneously represent the same thread of execution. A
thread of execution is \term{detached} when no \tcode{thread} object represents that thread.
Objects of class \tcode{thread} can be in a state that does not represent a thread of
execution. \enternote A \tcode{thread} object does not represent a thread of execution after
default construction, after being moved from, or after a successful call to \tcode{detach} or
\tcode{join}. \exitnote
\begin{codeblock}
namespace std {
class thread {
public:
// types:
class id;
typedef @\impdef@ native_handle_type; // See~\ref{thread.req.native}
// construct/copy/destroy:
thread() noexcept;
template <class F, class ...Args> explicit thread(F&& f, Args&&... args);
~thread();
thread(const thread&) = delete;
thread(thread&&) noexcept;
thread& operator=(const thread&) = delete;
thread& operator=(thread&&) noexcept;
// members:
void swap(thread&) noexcept;
bool joinable() const noexcept;
void join();
void detach();
id get_id() const noexcept;
native_handle_type native_handle(); // See~\ref{thread.req.native}
// static members:
static unsigned hardware_concurrency() noexcept;
};
}
\end{codeblock}
\rSec3[thread.thread.id]{Class \tcode{thread::id}}
\begin{codeblock}
namespace std {
class thread::id {
public:
id() noexcept;
};
bool operator==(thread::id x, thread::id y) noexcept;
bool operator!=(thread::id x, thread::id y) noexcept;
bool operator<(thread::id x, thread::id y) noexcept;
bool operator<=(thread::id x, thread::id y) noexcept;
bool operator>(thread::id x, thread::id y) noexcept;
bool operator>=(thread::id x, thread::id y) noexcept;
template<class charT, class traits>
basic_ostream<charT, traits>&
operator<< (basic_ostream<charT, traits>& out, thread::id id);
// Hash support
template <class T> struct hash;
template <> struct hash<thread::id>;
}
\end{codeblock}
\pnum An object of type \tcode{thread::id} provides a unique identifier for
each thread of execution and a single distinct value for all \tcode{thread}
objects that do not represent a thread of
execution~(\ref{thread.thread.class}). Each thread of execution has an
associated \tcode{thread::id} object that is not equal to the
\tcode{thread::id} object of any other thread of execution and that is not
equal to the \tcode{thread::id} object of any \tcode{std::thread} object that
does not represent threads of execution.
\pnum
\tcode{thread::id} shall be a trivially copyable class (Clause~\ref{class}).
The library may reuse the value of a \tcode{thread::id} of a terminated thread that can no longer be joined.
\pnum
\enternote Relational operators allow \tcode{thread::id} objects to be used as
keys in associative containers. \exitnote
\indexlibrary{\idxcode{thread::id}!constructor}%
\begin{itemdecl}
id() noexcept;
\end{itemdecl}
\begin{itemdescr}
\pnum\effects Constructs an object of type \tcode{id}.
\pnum\postconditions The constructed object does not represent a thread of execution.
\end{itemdescr}
\indexlibrary{\idxcode{thread::id}!\idxcode{operator==}}%
\indexlibrary{\idxcode{operator==}!\idxcode{thread::id}}%
\begin{itemdecl}
bool operator==(thread::id x, thread::id y) noexcept;
\end{itemdecl}
\begin{itemdescr}
\pnum\returns \tcode{true} only if \tcode{x} and \tcode{y} represent the same
thread of execution or neither \tcode{x} nor \tcode{y} represents a thread of
execution.
\end{itemdescr}
\indexlibrary{\idxcode{thread::id}!\idxcode{operator"!=}}%
\indexlibrary{\idxcode{operator"!=}!\idxcode{thread::id}}%
\begin{itemdecl}
bool operator!=(thread::id x, thread::id y) noexcept;
\end{itemdecl}
\begin{itemdescr}
\pnum\returns \tcode{!(x == y)}
\end{itemdescr}
\indexlibrary{\idxcode{thread::id}!\idxcode{operator<}}%
\indexlibrary{\idxcode{operator<}!\idxcode{thread::id}}%
\begin{itemdecl}
bool operator<(thread::id x, thread::id y) noexcept;
\end{itemdecl}
\begin{itemdescr}
\pnum
\returns A value such that \tcode{operator<} is a total ordering as described in~\ref{alg.sorting}.
\end{itemdescr}
\indexlibrary{\idxcode{thread::id}!\idxcode{operator<=}}%
\indexlibrary{\idxcode{operator<=}!\idxcode{thread::id}}%
\begin{itemdecl}
bool operator<=(thread::id x, thread::id y) noexcept;
\end{itemdecl}
\begin{itemdescr}
\pnum
\returns \tcode{!(y < x)}
\end{itemdescr}
\indexlibrary{\idxcode{thread::id}!\idxcode{operator>}}%
\indexlibrary{\idxcode{operator>}!\idxcode{thread::id}}%
\begin{itemdecl}
bool operator>(thread::id x, thread::id y) noexcept;
\end{itemdecl}
\begin{itemdescr}
\pnum\returns \tcode{y < x}
\end{itemdescr}
\indexlibrary{\idxcode{thread::id}!\idxcode{operator>=}}%
\indexlibrary{\idxcode{operator>=}!\idxcode{thread::id}}%
\begin{itemdecl}
bool operator>=(thread::id x, thread::id y) noexcept;
\end{itemdecl}
\begin{itemdescr}
\pnum\returns \tcode{!(x < y)}
\end{itemdescr}
\indexlibrary{\idxcode{thread::id}!\idxcode{operator\shl}}%
\indexlibrary{\idxcode{operator\shl}!\idxcode{thread::id}}%
\begin{itemdecl}
template<class charT, class traits>
basic_ostream<charT, traits>&
operator<< (basic_ostream<charT, traits>&& out, thread::id id);
\end{itemdecl}
\begin{itemdescr}
\pnum\effects Inserts an unspecified text representation of \tcode{id} into
\tcode{out}. For two objects of type \tcode{thread::id} \tcode{x} and \tcode{y},
if \tcode{x == y} the \tcode{thread::id} objects shall have the same text
representation and if \tcode{x != y} the \tcode{thread::id} objects shall have
distinct text representations.
\pnum\returns \tcode{out}
\end{itemdescr}
\begin{itemdecl}
template <> struct hash<thread::id>;
\end{itemdecl}
\begin{itemdescr}
\pnum The template specialization shall meet the requirements of class template
\tcode{hash}~(\ref{unord.hash}).
\end{itemdescr}
\rSec3[thread.thread.constr]{\tcode{thread} constructors}
\indexlibrary{\idxcode{thread}!constructor}%
\begin{itemdecl}
thread() noexcept;
\end{itemdecl}
\begin{itemdescr}
\pnum\effects Constructs a \tcode{thread} object that does not represent a thread of execution.
\pnum\postcondition \tcode{get_id() == id()}
\end{itemdescr}
\indexlibrary{\idxcode{thread}!constructor}%
\begin{itemdecl}
template <class F, class ...Args> explicit thread(F&& f, Args&&... args);
\end{itemdecl}
\begin{itemdescr}
\pnum
\requires\ \tcode{F} and each \tcode{Ti} in \tcode{Args} shall satisfy the
\tcode{MoveConstructible} requirements.
\tcode{\textit{INVOKE}(\textit{DECAY_COPY}(}
\tcode{std::forward<F>(f)), \textit{DECAY_COPY}(std::forward<Args>(args))...)}~(\ref{func.require}) shall be
a valid expression.
\pnum
\remarks
This constructor shall not participate in overload resolution if \tcode{decay_t<F>}
is the same type as \tcode{std::thread}.
\pnum
\effects\ Constructs an object of type \tcode{thread}. The new thread of execution executes
\tcode{\textit{INVOKE}(\textit{DECAY_COPY}(}\
\tcode{std::forward<F>(f)), \textit{DECAY_COPY}(std::forward<Args>(args))...)} with the calls to
\brk{}\tcode{\textit{DECAY_COPY}} being evaluated in the constructing thread. Any return value from this invocation
is ignored. \enternote This implies that any exceptions not thrown from the invocation of the copy
of \tcode{f} will be thrown in the constructing thread, not the new thread. \exitnote If the
invocation of
\tcode{\textit{INVOKE}(\textit{DECAY_COPY}(}
\tcode{std::forward<F>(f)), \textit{DECAY_COPY}(std::forward<Args>(args))...)}
terminates with an uncaught exception, \tcode{std::terminate} shall be called.
\pnum\sync The completion of the invocation of the constructor
synchronizes with the beginning of the invocation of the copy of \tcode{f}.
\pnum\postconditions \tcode{get_id() != id()}. \tcode{*this} represents the newly started thread.
\pnum\throws \tcode{system_error} if unable to start the new thread.
\pnum\errors
\begin{itemize}
\item \tcode{resource_unavailable_try_again} --- the system lacked the necessary
resources to create another thread, or the system-imposed limit on the number of
threads in a process would be exceeded.
\end{itemize}
\end{itemdescr}
\indexlibrary{\idxcode{thread}!constructor}%
\begin{itemdecl}
thread(thread&& x) noexcept;
\end{itemdecl}
\begin{itemdescr}
\pnum
\effects Constructs an object of type \tcode{thread} from \tcode{x}, and sets
\tcode{x} to a default constructed state.
\pnum
\postconditions \tcode{x.get_id() == id()} and \tcode{get_id()} returns the
value of \tcode{x.get_id()} prior to the start of construction.
\end{itemdescr}
\rSec3[thread.thread.destr]{\tcode{thread} destructor}
\indexlibrary{\idxcode{thread}!destructor}%
\begin{itemdecl}
~thread();
\end{itemdecl}
\begin{itemdescr}
\pnum
If \tcode{joinable()}, calls \tcode{std::terminate()}. Otherwise, has no effects.
\enternote Either implicitly detaching or joining a \tcode{joinable()} thread in its
destructor could result in difficult to debug correctness (for detach) or performance
(for join) bugs encountered only when an exception is raised. Thus the programmer must
ensure that the destructor is never executed while the thread is still joinable.
\exitnote
\end{itemdescr}
\rSec3[thread.thread.assign]{\tcode{thread} assignment}
\indexlibrary{\idxcode{thread}!\idxcode{operator=}}%
\indexlibrary{\idxcode{operator=}!\idxcode{thread}}%
\begin{itemdecl}
thread& operator=(thread&& x) noexcept;
\end{itemdecl}
\begin{itemdescr}
\pnum
\effects If \tcode{joinable()}, calls \tcode{std::terminate()}. Otherwise, assigns the
state of \tcode{x} to \tcode{*this} and sets \tcode{x} to a default constructed state.
\pnum
\postconditions \tcode{x.get_id() == id()} and \tcode{get_id()} returns the value of
\tcode{x.get_id()} prior to the assignment.
\pnum
\returns \tcode{*this}
\end{itemdescr}
\rSec3[thread.thread.member]{\tcode{thread} members}
\indexlibrary{\idxcode{thread}!\idxcode{swap}}%
\indexlibrary{\idxcode{swap}!\idxcode{thread}}%
\begin{itemdecl}
void swap(thread& x) noexcept;
\end{itemdecl}
\begin{itemdescr}
\pnum
\effects Swaps the state of \tcode{*this} and \tcode{x}.
\end{itemdescr}
\indexlibrary{\idxcode{thread}!\idxcode{joinable}}%
\indexlibrary{\idxcode{joinable}!\idxcode{thread}}%
\begin{itemdecl}
bool joinable() const noexcept;
\end{itemdecl}
\begin{itemdescr}
\pnum
\returns \tcode{get_id() != id()}
\end{itemdescr}
\indexlibrary{\idxcode{thread}!\idxcode{join}}%
\indexlibrary{\idxcode{join}!\idxcode{thread}}%
\begin{itemdecl}
void join();
\end{itemdecl}
\begin{itemdescr}
\pnum
\precondition \tcode{joinable()} is \tcode{true}.
\pnum
\effects\ Blocks until the thread represented by \tcode{*this} has completed.
\pnum
\sync The completion of the thread represented by \tcode{*this} synchronizes with~(\ref{intro.multithread})
the corresponding successful
\tcode{join()} return. \enternote Operations on
\tcode{*this} are not synchronized. \exitnote
\pnum
\postconditions The thread represented by \tcode{*this} has completed. \tcode{get_id() == id()}.
\pnum
\throws \tcode{system_error} when
an exception is required~(\ref{thread.req.exception}).
\pnum
\errors
\begin{itemize}
\item \tcode{resource_deadlock_would_occur} --- if deadlock is detected or
\tcode{this->get_id() == std::this_thread::get_id()}.
\item \tcode{no_such_process} --- if the thread is not valid.
\item \tcode{invalid_argument} --- if the thread is not joinable.
\end{itemize}
\end{itemdescr}
\indexlibrary{\idxcode{thread}!\idxcode{detach}}%
\indexlibrary{\idxcode{detach}!\idxcode{thread}}%
\begin{itemdecl}
void detach();
\end{itemdecl}
\begin{itemdescr}
\pnum\precondition \tcode{joinable()} is \tcode{true}.
\pnum
\effects The thread represented by \tcode{*this} continues execution without the calling thread
blocking. When \tcode{detach()} returns, \tcode{*this} no longer represents the possibly continuing
thread of execution. When the thread previously represented by \tcode{*this} ends execution, the
implementation shall release any owned resources.
\pnum\postcondition \tcode{get_id() == id()}.
\pnum\throws \tcode{system_error} when
an exception is required~(\ref{thread.req.exception}).
\pnum \errors
\begin{itemize}
\item \tcode{no_such_process} --- if the thread is not valid.
\item \tcode{invalid_argument} --- if the thread is not joinable.
\end{itemize}
\end{itemdescr}
\indexlibrary{\idxcode{thread}!\idxcode{get_id}}%
\indexlibrary{\idxcode{get_id}!\idxcode{thread}}%
\begin{itemdecl}
id get_id() const noexcept;
\end{itemdecl}
\begin{itemdescr}
\pnum
\returns A default constructed \tcode{id} object if \tcode{*this} does not represent a thread,
otherwise \tcode{this_thread::get_id()} for the thread of execution represented by
\tcode{*this}.
\end{itemdescr}
\rSec3[thread.thread.static]{\tcode{thread} static members}
\indexlibrary{\idxcode{thread}!\idxcode{hardware_concurrency}}%
\indexlibrary{\idxcode{hardware_concurrency}!\idxcode{thread}}%
\begin{itemdecl}
unsigned hardware_concurrency() noexcept;
\end{itemdecl}
\begin{itemdescr}
\pnum
\returns The number of hardware thread contexts. \enternote This value should
only be considered to be a hint. \exitnote If this value is not computable or
well defined an implementation should return 0.
\end{itemdescr}
\rSec3[thread.thread.algorithm]{\tcode{thread} specialized algorithms}
\indexlibrary{\idxcode{thread}!\idxcode{swap}}%
\indexlibrary{\idxcode{swap}!\idxcode{thread}}%
\begin{itemdecl}
void swap(thread& x, thread& y) noexcept;
\end{itemdecl}
\begin{itemdescr}
\pnum\effects \tcode{x.swap(y)}
\end{itemdescr}
\rSec2[thread.thread.this]{Namespace \tcode{this_thread}}
\begin{codeblock}
namespace std::this_thread {
thread::id get_id() noexcept;
void yield() noexcept;
template <class Clock, class Duration>
void sleep_until(const chrono::time_point<Clock, Duration>& abs_time);
template <class Rep, class Period>
void sleep_for(const chrono::duration<Rep, Period>& rel_time);
}
\end{codeblock}
\indexlibrary{\idxcode{this_thread}!\idxcode{get_id}}%
\indexlibrary{\idxcode{get_id}!\idxcode{this_thread}}%
\begin{itemdecl}
thread::id this_thread::get_id() noexcept;
\end{itemdecl}
\begin{itemdescr}
\pnum
\returns An object of type \tcode{thread::id} that uniquely identifies the current thread of
execution. No other thread of execution shall have this id and this thread of execution shall
always have this id. The object returned shall not compare equal to a default constructed
\tcode{thread::id}.
\end{itemdescr}
\indexlibrary{\idxcode{this_thread}!\idxcode{yield}}%
\indexlibrary{\idxcode{yield}!\idxcode{this_thread}}%
\begin{itemdecl}
void this_thread::yield() noexcept;
\end{itemdecl}
\begin{itemdescr}
\pnum
\effects Offers the implementation the opportunity to reschedule.
\pnum
\sync None.
\end{itemdescr}
\indexlibrary{\idxcode{this_thread}!\idxcode{sleep_until}}%
\indexlibrary{\idxcode{sleep_until}!\idxcode{this_thread}}%
\begin{itemdecl}
template <class Clock, class Duration>
void sleep_until(const chrono::time_point<Clock, Duration>& abs_time);
\end{itemdecl}
\begin{itemdescr}
\pnum
\effects Blocks the calling thread for the absolute timeout~(\ref{thread.req.timing}) specified
by \tcode{abs_time}.
\pnum
\sync None.
\pnum
\throws Timeout-related exceptions~(\ref{thread.req.timing}).
\end{itemdescr}
\indexlibrary{\idxcode{this_thread}!\idxcode{sleep_for}}%
\indexlibrary{\idxcode{sleep_for}!\idxcode{this_thread}}%
\begin{itemdecl}
template <class Rep, class Period>
void sleep_for(const chrono::duration<Rep, Period>& rel_time);
\end{itemdecl}
\begin{itemdescr}
\pnum
\effects Blocks the calling thread for the relative timeout~(\ref{thread.req.timing}) specified
by \tcode{rel_time}.
\pnum
\sync None.
\pnum
\throws Timeout-related exceptions~(\ref{thread.req.timing}).
\end{itemdescr}
\rSec1[thread.mutex]{Mutual exclusion}
\pnum
This section provides mechanisms for mutual exclusion: mutexes, locks, and call
once. These mechanisms ease the production of race-free
programs~(\ref{intro.multithread}).
\synopsis{Header \tcode{<mutex>} synopsis}
\indexlibrary{\idxhdr{mutex}}%
\begin{codeblock}
namespace std {
class mutex;
class recursive_mutex;
class timed_mutex;
class recursive_timed_mutex;
struct defer_lock_t { };
struct try_to_lock_t { };
struct adopt_lock_t { };
constexpr defer_lock_t defer_lock { };
constexpr try_to_lock_t try_to_lock { };
constexpr adopt_lock_t adopt_lock { };
template <class Mutex> class lock_guard;
template <class Mutex> class unique_lock;
template <class Mutex>
void swap(unique_lock<Mutex>& x, unique_lock<Mutex>& y) noexcept;
template <class L1, class L2, class... L3> int try_lock(L1&, L2&, L3&...);
template <class L1, class L2, class... L3> void lock(L1&, L2&, L3&...);
struct once_flag {
constexpr once_flag() noexcept;
once_flag(const once_flag&) = delete;
once_flag& operator=(const once_flag&) = delete;
};
template<class Callable, class ...Args>
void call_once(once_flag& flag, Callable&& func, Args&&... args);
}
\end{codeblock}
\synopsis{Header \tcode{<shared_mutex>} synopsis}
\indexlibrary{\idxhdr{shared_mutex}}%
\begin{codeblock}
namespace std {
class shared_timed_mutex;
template <class Mutex> class shared_lock;
template <class Mutex>
void swap(shared_lock<Mutex>& x, shared_lock<Mutex>& y) noexcept;
}
\end{codeblock}
\rSec2[thread.mutex.requirements]{Mutex requirements}
\rSec3[thread.mutex.requirements.general]{In general}
\pnum
A mutex object facilitates protection against data races and allows safe synchronization of
data between execution agents~(\ref{thread.req.lockable}).
An execution agent \term{owns} a mutex from the time it successfully calls one of the
lock functions until it calls unlock. Mutexes can be either recursive or non-recursive, and can
grant simultaneous ownership to one or many execution agents. Both
recursive and non-recursive mutexes are supplied.
\rSec3[thread.mutex.requirements.mutex]{Mutex types}
\pnum
The \defn{mutex types} are the standard library types \tcode{std::mutex},
\tcode{std::recursive_mutex}, \tcode{std::timed_mutex}, \tcode{std::recursive_timed_mutex},
and \tcode{std::shared_timed_mutex}.
They shall meet the requirements set out in this section. In this description, \tcode{m}
denotes an object of a mutex type.
\pnum
The mutex types shall meet the \tcode{Lockable} requirements~(\ref{thread.req.lockable.req}).
\pnum
The mutex types shall be \tcode{DefaultConstructible} and \tcode{Destructible}. If
initialization of an object of a mutex type fails, an exception of type
\tcode{system_error} shall be thrown. The mutex types shall not be copyable or movable.
\pnum
The error conditions for error codes, if any, reported by member functions of the mutex types
shall be:
\begin{itemize}
\item \tcode{resource_unavailable_try_again} --- if any native handle type manipulated is not available.
\item \tcode{operation_not_permitted} --- if the thread does not have the
privilege to perform the operation.
\item \tcode{device_or_resource_busy} --- if any native handle type manipulated is already locked.
\item \tcode{invalid_argument} --- if any native handle type manipulated as part of mutex
construction is incorrect.
\end{itemize}
\pnum
The implementation shall provide lock and unlock operations, as described below.
For purposes of determining the existence of a data race, these behave as
atomic operations~(\ref{intro.multithread}). The lock and unlock operations on
a single mutex shall appear to occur in a single total order. \enternote this
can be viewed as the modification order~(\ref{intro.multithread}) of the
mutex. \exitnote
\enternote Construction and
destruction of an object of a mutex type need not be thread-safe; other
synchronization should be used to ensure that mutex objects are initialized
and visible to other threads. \exitnote
\pnum
The expression \tcode{m.lock()} shall be well-formed and have the following semantics:
\begin{itemdescr}
\pnum
\requires If \tcode{m} is of type \tcode{std::mutex}, \tcode{std::timed_mutex}, or
\tcode{std::shared_timed_mutex}, the calling
thread does not own the mutex.
\pnum
\effects Blocks the calling thread until ownership of the mutex can be obtained for the calling thread.
\pnum
\postcondition The calling thread owns the mutex.
\pnum
\returntype \tcode{void}
\pnum
\sync Prior \tcode{unlock()} operations on the same object shall
\term{synchronize with}~(\ref{intro.multithread}) this operation.
\pnum
\throws \tcode{system_error} when
an exception is required~(\ref{thread.req.exception}).
\pnum \errors
\begin{itemize}
\item \tcode{operation_not_permitted} --- if the thread does not have the
privilege to perform the operation.
\item \tcode{resource_deadlock_would_occur} --- if the implementation detects
that a deadlock would occur.
\item \tcode{device_or_resource_busy} --- if the mutex is already locked and blocking is not possible.
\end{itemize}
\end{itemdescr}
\pnum
The expression \tcode{m.try_lock()} shall be well-formed and have the following semantics:
\begin{itemdescr}
\pnum
\requires If \tcode{m} is of type \tcode{std::mutex}, \tcode{std::timed_mutex},
or \tcode{std::shared_timed_mutex}, the calling
thread does not own the mutex.
\pnum
\effects Attempts to obtain ownership of the mutex for the calling thread without
blocking. If ownership is not obtained, there is no effect and \tcode{try_lock()}
immediately returns. An implementation may fail to obtain the lock even if it is not
held by any other thread. \enternote This spurious failure is normally uncommon, but
allows interesting implementations based on a simple
compare and exchange
(Clause~\ref{atomics}). \exitnote
An implementation should ensure that \tcode{try_lock()} does not consistently return \tcode{false}
in the absence of contending mutex acquisitions.
\pnum
\returntype \tcode{bool}
\pnum
\returns \tcode{true} if ownership of the mutex was obtained for the calling
thread, otherwise \tcode{false}.
\pnum
\sync If \tcode{try_lock()} returns \tcode{true}, prior \tcode{unlock()} operations
on the same object \term{synchronize with}~(\ref{intro.multithread}) this operation.