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
// Clippy false positive:
// https://github.com/rust-lang/rust-clippy/issues/3071
use crateMutex;
use VecDeque;
use ;
use Arc;
use Auto;
use ;
pub use Task;
/// Synchronization mechanism for performing non-interleaved output from
/// concurrent tasks.
///
/// # Bare-bones skeleton
///
/// This example performs 30 tasks in parallel across a pool of 10 threads. Each
/// task writes one line of output. All output is guaranteed to appear in order
/// by task index from 0 through 29.
///
/// ```
/// use oqueue::Sequencer;
///
/// fn main() {
/// let oqueue = Sequencer::stderr();
///
/// // Launch 10 worker threads.
/// rayon::scope(|scope| {
/// for _ in 0..10 {
/// scope.spawn(|_| worker(&oqueue));
/// }
/// });
/// }
///
/// fn worker(oqueue: &Sequencer) {
/// loop {
/// let task = oqueue.begin();
/// if task.index >= 30 {
/// return;
/// }
/// writeln!(task, "hello from task #{}", task.index);
/// }
/// }
/// ```
///
/// <details>
/// <summary style="padding-left:3em"><a><em>▷ Click to show output</em></a></summary>
///
/// ```text
/// hello from task #0
/// hello from task #1
/// hello from task #2
/// hello from task #3
/// hello from task #4
/// hello from task #5
/// hello from task #6
/// hello from task #7
/// hello from task #8
/// hello from task #9
/// hello from task #10
/// hello from task #11
/// hello from task #12
/// hello from task #13
/// hello from task #14
/// hello from task #15
/// hello from task #16
/// hello from task #17
/// hello from task #18
/// hello from task #19
/// hello from task #20
/// hello from task #21
/// hello from task #22
/// hello from task #23
/// hello from task #24
/// hello from task #25
/// hello from task #26
/// hello from task #27
/// hello from task #28
/// hello from task #29
/// ```
/// </details>
///
/// <br>
///
/// # Shared slice skeleton
///
/// This example uses a shared slice to coordinate work that needs to be
/// performed. Tasks perform work on one element of the slice according to their
/// task index.
///
/// ```
/// use oqueue::Sequencer;
///
/// struct WorkItem(u8);
///
/// fn main() {
/// let oqueue = Sequencer::stderr();
/// let work = (b'A'..=b'Z').map(WorkItem).collect::<Vec<_>>();
///
/// // Launch 10 worker threads.
/// rayon::scope(|scope| {
/// for i in 0..10 {
/// let oqueue = &oqueue;
/// let work = &work;
/// scope.spawn(move |_| worker(i, oqueue, work));
/// }
/// });
/// }
///
/// fn worker(thread: usize, oqueue: &Sequencer, work: &[WorkItem]) {
/// loop {
/// let task = oqueue.begin();
/// let input = match work.get(task.index) {
/// Some(input) => input,
/// None => return,
/// };
/// writeln!(
/// task,
/// "thread {} is performing work {}",
/// thread, input.0 as char,
/// );
/// }
/// }
/// ```
///
/// <details>
/// <summary style="padding-left:3em"><a><em>▷ Click to show output</em></a></summary>
///
/// ```text
/// thread 0 is performing work A
/// thread 9 is performing work B
/// thread 1 is performing work C
/// thread 2 is performing work D
/// thread 0 is performing work E
/// thread 0 is performing work F
/// thread 0 is performing work G
/// thread 0 is performing work H
/// thread 0 is performing work I
/// thread 0 is performing work J
/// thread 2 is performing work K
/// thread 9 is performing work L
/// thread 9 is performing work M
/// thread 9 is performing work N
/// thread 9 is performing work O
/// thread 9 is performing work P
/// thread 9 is performing work Q
/// thread 9 is performing work R
/// thread 0 is performing work S
/// thread 0 is performing work T
/// thread 0 is performing work U
/// thread 0 is performing work V
/// thread 2 is performing work W
/// thread 1 is performing work X
/// thread 1 is performing work Y
/// thread 1 is performing work Z
/// ```
/// </details>
///
/// <br>
///
/// # Synchronized queue skeleton
///
/// This example uses a synchronized queue of work items in the form of a mutex
/// holding an iterator, although any other channel-like implementation could
/// work too. The task index is not used in this approach.
///
/// ```
/// use oqueue::Sequencer;
/// use std::sync::Mutex;
///
/// struct WorkItem(u8);
///
/// fn main() {
/// let oqueue = Sequencer::stderr();
/// let work = Mutex::new((b'A'..=b'Z').map(WorkItem));
///
/// // Launch 10 worker threads.
/// rayon::scope(|scope| {
/// for i in 0..10 {
/// let oqueue = &oqueue;
/// let work = &work;
/// scope.spawn(move |_| worker(i, oqueue, work));
/// }
/// });
/// }
///
/// fn worker(thread: usize, oqueue: &Sequencer, work: &Mutex<dyn Iterator<Item = WorkItem>>) {
/// loop {
/// let task = oqueue.begin();
/// let input = match work.lock().unwrap().next() {
/// Some(input) => input,
/// None => return,
/// };
/// writeln!(
/// task,
/// "thread {} is performing work {}",
/// thread, input.0 as char,
/// );
/// }
/// }
/// ```
///
/// <details>
/// <summary style="padding-left:3em"><a><em>▷ Click to show output</em></a></summary>
///
/// ```text
/// thread 9 is performing work A
/// thread 0 is performing work B
/// thread 9 is performing work C
/// thread 9 is performing work D
/// thread 9 is performing work E
/// thread 9 is performing work F
/// thread 9 is performing work G
/// thread 9 is performing work H
/// thread 9 is performing work I
/// thread 1 is performing work J
/// thread 9 is performing work K
/// thread 2 is performing work L
/// thread 1 is performing work M
/// thread 2 is performing work N
/// thread 0 is performing work O
/// thread 9 is performing work P
/// thread 1 is performing work Q
/// thread 1 is performing work R
/// thread 1 is performing work S
/// thread 0 is performing work T
/// thread 1 is performing work U
/// thread 2 is performing work V
/// thread 9 is performing work W
/// thread 0 is performing work X
/// thread 1 is performing work Y
/// thread 1 is performing work Z
/// ```
/// </details>
///
/// <br>
;