From Jason Turner

[thread.barrier]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmp32iaoo73/{from.md → to.md} +202 -0
tmp/tmp32iaoo73/{from.md → to.md} RENAMED
@@ -0,0 +1,202 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ### Barriers <a id="thread.barrier">[[thread.barrier]]</a>
2
+
3
+ A barrier is a thread coordination mechanism whose lifetime consists of
4
+ a sequence of barrier phases, where each phase allows at most an
5
+ expected number of threads to block until the expected number of threads
6
+ arrive at the barrier.
7
+
8
+ [*Note 1*: A barrier is useful for managing repeated tasks that are
9
+ handled by multiple threads. — *end note*]
10
+
11
+ #### Header `<barrier>` synopsis <a id="barrier.syn">[[barrier.syn]]</a>
12
+
13
+ ``` cpp
14
+ namespace std {
15
+ template<class CompletionFunction = see below>
16
+ class barrier;
17
+ }
18
+ ```
19
+
20
+ #### Class template `barrier` <a id="thread.barrier.class">[[thread.barrier.class]]</a>
21
+
22
+ ``` cpp
23
+ namespace std {
24
+ template<class CompletionFunction = see below>
25
+ class barrier {
26
+ public:
27
+ using arrival_token = see below;
28
+
29
+ static constexpr ptrdiff_t max() noexcept;
30
+
31
+ constexpr explicit barrier(ptrdiff_t expected,
32
+ CompletionFunction f = CompletionFunction());
33
+ ~barrier();
34
+
35
+ barrier(const barrier&) = delete;
36
+ barrier& operator=(const barrier&) = delete;
37
+
38
+ [[nodiscard]] arrival_token arrive(ptrdiff_t update = 1);
39
+ void wait(arrival_token&& arrival) const;
40
+
41
+ void arrive_and_wait();
42
+ void arrive_and_drop();
43
+
44
+ private:
45
+ CompletionFunction completion; // exposition only
46
+ };
47
+ }
48
+ ```
49
+
50
+ Each *barrier phase* consists of the following steps:
51
+
52
+ - The expected count is decremented by each call to `arrive` or
53
+ `arrive_and_drop`.
54
+ - When the expected count reaches zero, the phase completion step is
55
+ run. For the specialization with the default value of the
56
+ `CompletionFunction` template parameter, the completion step is run as
57
+ part of the call to `arrive` or `arrive_and_drop` that caused the
58
+ expected count to reach zero. For other specializations, the
59
+ completion step is run on one of the threads that arrived at the
60
+ barrier during the phase.
61
+ - When the completion step finishes, the expected count is reset to what
62
+ was specified by the `expected` argument to the constructor, possibly
63
+ adjusted by calls to `arrive_and_drop`, and the next phase starts.
64
+
65
+ Each phase defines a *phase synchronization point*. Threads that arrive
66
+ at the barrier during the phase can block on the phase synchronization
67
+ point by calling `wait`, and will remain blocked until the phase
68
+ completion step is run.
69
+
70
+ The *phase completion step* that is executed at the end of each phase
71
+ has the following effects:
72
+
73
+ - Invokes the completion function, equivalent to `completion()`.
74
+ - Unblocks all threads that are blocked on the phase synchronization
75
+ point.
76
+
77
+ The end of the completion step strongly happens before the returns from
78
+ all calls that were unblocked by the completion step. For
79
+ specializations that do not have the default value of the
80
+ `CompletionFunction` template parameter, the behavior is undefined if
81
+ any of the barrier object’s member functions other than `wait` are
82
+ called while the completion step is in progress.
83
+
84
+ Concurrent invocations of the member functions of `barrier`, other than
85
+ its destructor, do not introduce data races. The member functions
86
+ `arrive` and `arrive_and_drop` execute atomically.
87
+
88
+ `CompletionFunction` shall meet the *Cpp17MoveConstructible* (
89
+ [[cpp17.moveconstructible]]) and *Cpp17Destructible* (
90
+ [[cpp17.destructible]]) requirements.
91
+ `is_nothrow_invocable_v<CompletionFunction&>` shall be `true`.
92
+
93
+ The default value of the `CompletionFunction` template parameter is an
94
+ unspecified type, such that, in addition to satisfying the requirements
95
+ of `CompletionFunction`, it meets the *Cpp17DefaultConstructible*
96
+ requirements ([[cpp17.defaultconstructible]]) and `completion()` has no
97
+ effects.
98
+
99
+ `barrier::arrival_token` is an unspecified type, such that it meets the
100
+ *Cpp17MoveConstructible* ([[cpp17.moveconstructible]]),
101
+ *Cpp17MoveAssignable* ([[cpp17.moveassignable]]), and
102
+ *Cpp17Destructible* ([[cpp17.destructible]]) requirements.
103
+
104
+ ``` cpp
105
+ static constexpr ptrdiff_t max() noexcept;
106
+ ```
107
+
108
+ *Returns:* The maximum expected count that the implementation supports.
109
+
110
+ ``` cpp
111
+ constexpr explicit barrier(ptrdiff_t expected,
112
+ CompletionFunction f = CompletionFunction());
113
+ ```
114
+
115
+ *Preconditions:* `expected >= 0` is `true` and `expected <= max()` is
116
+ `true`.
117
+
118
+ *Effects:* Sets both the initial expected count for each barrier phase
119
+ and the current expected count for the first phase to `expected`.
120
+ Initializes `completion` with `std::move(f)`. Starts the first phase.
121
+
122
+ [*Note 1*: If `expected` is 0 this object can only be
123
+ destroyed. — *end note*]
124
+
125
+ *Throws:* Any exception thrown by `CompletionFunction`’s move
126
+ constructor.
127
+
128
+ ``` cpp
129
+ [[nodiscard]] arrival_token arrive(ptrdiff_t update = 1);
130
+ ```
131
+
132
+ *Preconditions:* `update > 0` is `true`, and `update` is less than or
133
+ equal to the expected count for the current barrier phase.
134
+
135
+ *Effects:* Constructs an object of type `arrival_token` that is
136
+ associated with the phase synchronization point for the current phase.
137
+ Then, decrements the expected count by `update`.
138
+
139
+ *Synchronization:* The call to `arrive` strongly happens before the
140
+ start of the phase completion step for the current phase.
141
+
142
+ *Returns:* The constructed `arrival_token` object.
143
+
144
+ *Throws:* `system_error` when an exception is
145
+ required [[thread.req.exception]].
146
+
147
+ *Error conditions:* Any of the error conditions allowed for mutex
148
+ types [[thread.mutex.requirements.mutex]].
149
+
150
+ [*Note 2*: This call can cause the completion step for the current
151
+ phase to start. — *end note*]
152
+
153
+ ``` cpp
154
+ void wait(arrival_token&& arrival) const;
155
+ ```
156
+
157
+ *Preconditions:* `arrival` is associated with the phase synchronization
158
+ point for the current phase or the immediately preceding phase of the
159
+ same barrier object.
160
+
161
+ *Effects:* Blocks at the synchronization point associated with
162
+ `std::move(arrival)` until the phase completion step of the
163
+ synchronization point’s phase is run.
164
+
165
+ [*Note 3*: If `arrival` is associated with the synchronization point
166
+ for a previous phase, the call returns immediately. — *end note*]
167
+
168
+ *Throws:* `system_error` when an exception is
169
+ required [[thread.req.exception]].
170
+
171
+ *Error conditions:* Any of the error conditions allowed for mutex
172
+ types [[thread.mutex.requirements.mutex]].
173
+
174
+ ``` cpp
175
+ void arrive_and_wait();
176
+ ```
177
+
178
+ *Effects:* Equivalent to: `wait(arrive())`.
179
+
180
+ ``` cpp
181
+ void arrive_and_drop();
182
+ ```
183
+
184
+ *Preconditions:* The expected count for the current barrier phase is
185
+ greater than zero.
186
+
187
+ *Effects:* Decrements the initial expected count for all subsequent
188
+ phases by one. Then decrements the expected count for the current phase
189
+ by one.
190
+
191
+ *Synchronization:* The call to `arrive_and_drop` strongly happens before
192
+ the start of the phase completion step for the current phase.
193
+
194
+ *Throws:* `system_error` when an exception is
195
+ required [[thread.req.exception]].
196
+
197
+ *Error conditions:* Any of the error conditions allowed for mutex
198
+ types [[thread.mutex.requirements.mutex]].
199
+
200
+ [*Note 4*: This call can cause the completion step for the current
201
+ phase to start. — *end note*]
202
+