Debian Codegraph @

upstream/opensuse-tumbleweed/man3/Thread::Queue.3pm

raw
1 .\" -*- mode: troff; coding: utf-8 -*-
2 .\" Automatically generated by Pod::Man v6.0.2 (Pod::Simple 3.45)
3 .\"
4 .\" Standard preamble:
5 .\" ========================================================================
6 .de Sp \" Vertical space (when we can't use .PP)
7 .if t .sp .5v
8 .if n .sp
9 ..
10 .de Vb \" Begin verbatim text
11 .ft CW
12 .nf
13 .ne \\$1
14 ..
15 .de Ve \" End verbatim text
16 .ft R
17 .fi
18 ..
19 .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>.
20 .ie n \{\
21 . ds C` ""
22 . ds C' ""
23 'br\}
24 .el\{\
25 . ds C`
26 . ds C'
27 'br\}
28 .\"
29 .\" Escape single quotes in literal strings from groff's Unicode transform.
30 .ie \n(.g .ds Aq \(aq
31 .el .ds Aq '
32 .\"
33 .\" If the F register is >0, we'll generate index entries on stderr for
34 .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
35 .\" entries marked with X<> in POD. Of course, you'll have to process the
36 .\" output yourself in some meaningful fashion.
37 .\"
38 .\" Avoid warning from groff about undefined register 'F'.
39 .de IX
40 ..
41 .nr rF 0
42 .if \n(.g .if rF .nr rF 1
43 .if (\n(rF:(\n(.g==0)) \{\
44 . if \nF \{\
45 . de IX
46 . tm Index:\\$1\t\\n%\t"\\$2"
47 ..
48 . if !\nF==2 \{\
49 . nr % 0
50 . nr F 2
51 . \}
52 . \}
53 .\}
54 .rr rF
55 .\"
56 .\" Required to disable full justification in groff 1.23.0.
57 .if n .ds AD l
58 .\" ========================================================================
59 .\"
60 .IX Title "Thread::Queue 3pm"
61 .TH Thread::Queue 3pm 2026-03-09 "perl v5.42.1" "Perl Programmers Reference Guide"
62 .\" For nroff, turn off justification. Always turn off hyphenation; it makes
63 .\" way too many mistakes in technical documents.
64 .if n .ad l
65 .nh
66 .SH NAME
67 Thread::Queue \- Thread\-safe queues
68 .SH VERSION
69 .IX Header "VERSION"
70 This document describes Thread::Queue version 3.14
71 .SH SYNOPSIS
72 .IX Header "SYNOPSIS"
73 .Vb 2
74 \& use strict;
75 \& use warnings;
76 \&
77 \& use threads;
78 \& use Thread::Queue;
79 \&
80 \& my $q = Thread::Queue\->new(); # A new empty queue
81 \&
82 \& # Worker thread
83 \& my $thr = threads\->create(
84 \& sub {
85 \& # Thread will loop until no more work
86 \& while (defined(my $item = $q\->dequeue())) {
87 \& # Do work on $item
88 \& ...
89 \& }
90 \& }
91 \& );
92 \&
93 \& # Send work to the thread
94 \& $q\->enqueue($item1, ...);
95 \& # Signal that there is no more work to be sent
96 \& $q\->end();
97 \& # Join up with the thread when it finishes
98 \& $thr\->join();
99 \&
100 \& ...
101 \&
102 \& # Count of items in the queue
103 \& my $left = $q\->pending();
104 \&
105 \& # Non\-blocking dequeue
106 \& if (defined(my $item = $q\->dequeue_nb())) {
107 \& # Work on $item
108 \& }
109 \&
110 \& # Blocking dequeue with 5\-second timeout
111 \& if (defined(my $item = $q\->dequeue_timed(5))) {
112 \& # Work on $item
113 \& }
114 \&
115 \& # Set a size for a queue
116 \& $q\->limit = 5;
117 \&
118 \& # Get the second item in the queue without dequeuing anything
119 \& my $item = $q\->peek(1);
120 \&
121 \& # Insert two items into the queue just behind the head
122 \& $q\->insert(1, $item1, $item2);
123 \&
124 \& # Extract the last two items on the queue
125 \& my ($item1, $item2) = $q\->extract(\-2, 2);
126 .Ve
127 .SH DESCRIPTION
128 .IX Header "DESCRIPTION"
129 This module provides thread\-safe FIFO queues that can be accessed safely by
130 any number of threads.
131 .PP
132 Any data types supported by threads::shared can be passed via queues:
133 .IP "Ordinary scalars" 4
134 .IX Item "Ordinary scalars"
135 .PD 0
136 .IP "Array refs" 4
137 .IX Item "Array refs"
138 .IP "Hash refs" 4
139 .IX Item "Hash refs"
140 .IP "Scalar refs" 4
141 .IX Item "Scalar refs"
142 .IP "Objects based on the above" 4
143 .IX Item "Objects based on the above"
144 .PD
145 .PP
146 Ordinary scalars are added to queues as they are.
147 .PP
148 If not already thread\-shared, the other complex data types will be cloned
149 (recursively, if needed, and including any \f(CW\*(C`bless\*(C'\fRings and read\-only
150 settings) into thread\-shared structures before being placed onto a queue.
151 .PP
152 For example, the following would cause Thread::Queue to create a empty,
153 shared array reference via \f(CW&shared([])\fR, copy the elements \*(Aqfoo\*(Aq, \*(Aqbar\*(Aq
154 and \*(Aqbaz\*(Aq from \f(CW@ary\fR into it, and then place that shared reference onto
155 the queue:
156 .PP
157 .Vb 2
158 \& my @ary = qw/foo bar baz/;
159 \& $q\->enqueue(\e@ary);
160 .Ve
161 .PP
162 However, for the following, the items are already shared, so their references
163 are added directly to the queue, and no cloning takes place:
164 .PP
165 .Vb 2
166 \& my @ary :shared = qw/foo bar baz/;
167 \& $q\->enqueue(\e@ary);
168 \&
169 \& my $obj = &shared({});
170 \& $$obj{\*(Aqfoo\*(Aq} = \*(Aqbar\*(Aq;
171 \& $$obj{\*(Aqqux\*(Aq} = 99;
172 \& bless($obj, \*(AqMy::Class\*(Aq);
173 \& $q\->enqueue($obj);
174 .Ve
175 .PP
176 See "LIMITATIONS" for caveats related to passing objects via queues.
177 .SH "QUEUE CREATION"
178 .IX Header "QUEUE CREATION"
179 .IP \->\fBnew()\fR 4
180 .IX Item "->new()"
181 Creates a new empty queue.
182 .IP \->new(LIST) 4
183 .IX Item "->new(LIST)"
184 Creates a new queue pre\-populated with the provided list of items.
185 .SH "BASIC METHODS"
186 .IX Header "BASIC METHODS"
187 The following methods deal with queues on a FIFO basis.
188 .IP \->enqueue(LIST) 4
189 .IX Item "->enqueue(LIST)"
190 Adds a list of items onto the end of the queue.
191 .IP \->\fBdequeue()\fR 4
192 .IX Item "->dequeue()"
193 .PD 0
194 .IP \->dequeue(COUNT) 4
195 .IX Item "->dequeue(COUNT)"
196 .PD
197 Removes the requested number of items (default is 1) from the head of the
198 queue, and returns them. If the queue contains fewer than the requested
199 number of items, then the thread will be blocked until the requisite number
200 of items are available (i.e., until other threads \f(CW\*(C`enqueue\*(C'\fR more items).
201 .IP \->\fBdequeue_nb()\fR 4
202 .IX Item "->dequeue_nb()"
203 .PD 0
204 .IP \->dequeue_nb(COUNT) 4
205 .IX Item "->dequeue_nb(COUNT)"
206 .PD
207 Removes the requested number of items (default is 1) from the head of the
208 queue, and returns them. If the queue contains fewer than the requested
209 number of items, then it immediately (i.e., non\-blocking) returns whatever
210 items there are on the queue. If the queue is empty, then \f(CW\*(C`undef\*(C'\fR is
211 returned.
212 .IP \->dequeue_timed(TIMEOUT) 4
213 .IX Item "->dequeue_timed(TIMEOUT)"
214 .PD 0
215 .IP "\->dequeue_timed(TIMEOUT, COUNT)" 4
216 .IX Item "->dequeue_timed(TIMEOUT, COUNT)"
217 .PD
218 Removes the requested number of items (default is 1) from the head of the
219 queue, and returns them. If the queue contains fewer than the requested
220 number of items, then the thread will be blocked until the requisite number of
221 items are available, or until the timeout is reached. If the timeout is
222 reached, it returns whatever items there are on the queue, or \f(CW\*(C`undef\*(C'\fR if the
223 queue is empty.
224 .Sp
225 The timeout may be a number of seconds relative to the current time (e.g., 5
226 seconds from when the call is made), or may be an absolute timeout in \fIepoch\fR
227 seconds the same as would be used with
228 \&\fBcond_timedwait()\fR.
229 Fractional seconds (e.g., 2.5 seconds) are also supported (to the extent of
230 the underlying implementation).
231 .Sp
232 If \f(CW\*(C`TIMEOUT\*(C'\fR is missing, \f(CW\*(C`undef\*(C'\fR, or less than or equal to 0, then this call
233 behaves the same as \f(CW\*(C`dequeue_nb\*(C'\fR.
234 .IP \->\fBpending()\fR 4
235 .IX Item "->pending()"
236 Returns the number of items still in the queue. Returns \f(CW\*(C`undef\*(C'\fR if the queue
237 has been ended (see below), and there are no more items in the queue.
238 .IP \->limit 4
239 .IX Item "->limit"
240 Sets the size of the queue. If set, calls to \f(CWenqueue()\fR will block until
241 the number of pending items in the queue drops below the \f(CW\*(C`limit\*(C'\fR. The
242 \&\f(CW\*(C`limit\*(C'\fR does not prevent enqueuing items beyond that count:
243 .Sp
244 .Vb 8
245 \& my $q = Thread::Queue\->new(1, 2);
246 \& $q\->limit = 4;
247 \& $q\->enqueue(3, 4, 5); # Does not block
248 \& $q\->enqueue(6); # Blocks until at least 2 items are
249 \& # dequeued
250 \& my $size = $q\->limit; # Returns the current limit (may return
251 \& # \*(Aqundef\*(Aq)
252 \& $q\->limit = 0; # Queue size is now unlimited
253 .Ve
254 .Sp
255 Calling any of the dequeue methods with \f(CW\*(C`COUNT\*(C'\fR greater than a queue\*(Aqs
256 \&\f(CW\*(C`limit\*(C'\fR will generate an error.
257 .IP \->\fBend()\fR 4
258 .IX Item "->end()"
259 Declares that no more items will be added to the queue.
260 .Sp
261 All threads blocking on \f(CWdequeue()\fR calls will be unblocked with any
262 remaining items in the queue and/or \f(CW\*(C`undef\*(C'\fR being returned. Any subsequent
263 calls to \f(CWdequeue()\fR will behave like \f(CWdequeue_nb()\fR.
264 .Sp
265 Once ended, no more items may be placed in the queue.
266 .SH "ADVANCED METHODS"
267 .IX Header "ADVANCED METHODS"
268 The following methods can be used to manipulate items anywhere in a queue.
269 .PP
270 To prevent the contents of a queue from being modified by another thread
271 while it is being examined and/or changed, lock the queue inside a local block:
272 .PP
273 .Vb 8
274 \& {
275 \& lock($q); # Keep other threads from changing the queue\*(Aqs contents
276 \& my $item = $q\->peek();
277 \& if ($item ...) {
278 \& ...
279 \& }
280 \& }
281 \& # Queue is now unlocked
282 .Ve
283 .IP \->\fBpeek()\fR 4
284 .IX Item "->peek()"
285 .PD 0
286 .IP \->peek(INDEX) 4
287 .IX Item "->peek(INDEX)"
288 .PD
289 Returns an item from the queue without dequeuing anything. Defaults to the
290 head of queue (at index position 0) if no index is specified. Negative
291 index values are supported as with arrays (i.e., \-1
292 is the end of the queue, \-2 is next to last, and so on).
293 .Sp
294 If no items exists at the specified index (i.e., the queue is empty, or the
295 index is beyond the number of items on the queue), then \f(CW\*(C`undef\*(C'\fR is returned.
296 .Sp
297 Remember, the returned item is not removed from the queue, so manipulating a
298 \&\f(CW\*(C`peek\*(C'\fRed at reference affects the item on the queue.
299 .IP "\->insert(INDEX, LIST)" 4
300 .IX Item "->insert(INDEX, LIST)"
301 Adds the list of items to the queue at the specified index position (0
302 is the head of the list). Any existing items at and beyond that position are
303 pushed back past the newly added items:
304 .Sp
305 .Vb 3
306 \& $q\->enqueue(1, 2, 3, 4);
307 \& $q\->insert(1, qw/foo bar/);
308 \& # Queue now contains: 1, foo, bar, 2, 3, 4
309 .Ve
310 .Sp
311 Specifying an index position greater than the number of items in the queue
312 just adds the list to the end.
313 .Sp
314 Negative index positions are supported:
315 .Sp
316 .Vb 3
317 \& $q\->enqueue(1, 2, 3, 4);
318 \& $q\->insert(\-2, qw/foo bar/);
319 \& # Queue now contains: 1, 2, foo, bar, 3, 4
320 .Ve
321 .Sp
322 Specifying a negative index position greater than the number of items in the
323 queue adds the list to the head of the queue.
324 .IP \->\fBextract()\fR 4
325 .IX Item "->extract()"
326 .PD 0
327 .IP \->extract(INDEX) 4
328 .IX Item "->extract(INDEX)"
329 .IP "\->extract(INDEX, COUNT)" 4
330 .IX Item "->extract(INDEX, COUNT)"
331 .PD
332 Removes and returns the specified number of items (defaults to 1) from the
333 specified index position in the queue (0 is the head of the queue). When
334 called with no arguments, \f(CW\*(C`extract\*(C'\fR operates the same as \f(CW\*(C`dequeue_nb\*(C'\fR.
335 .Sp
336 This method is non\-blocking, and will return only as many items as are
337 available to fulfill the request:
338 .Sp
339 .Vb 5
340 \& $q\->enqueue(1, 2, 3, 4);
341 \& my $item = $q\->extract(2) # Returns 3
342 \& # Queue now contains: 1, 2, 4
343 \& my @items = $q\->extract(1, 3) # Returns (2, 4)
344 \& # Queue now contains: 1
345 .Ve
346 .Sp
347 Specifying an index position greater than the number of items in the
348 queue results in \f(CW\*(C`undef\*(C'\fR or an empty list being returned.
349 .Sp
350 .Vb 3
351 \& $q\->enqueue(\*(Aqfoo\*(Aq);
352 \& my $nada = $q\->extract(3) # Returns undef
353 \& my @nada = $q\->extract(1, 3) # Returns ()
354 .Ve
355 .Sp
356 Negative index positions are supported. Specifying a negative index position
357 greater than the number of items in the queue may return items from the head
358 of the queue (similar to \f(CW\*(C`dequeue_nb\*(C'\fR) if the count overlaps the head of the
359 queue from the specified position (i.e. if queue size + index + count is
360 greater than zero):
361 .Sp
362 .Vb 6
363 \& $q\->enqueue(qw/foo bar baz/);
364 \& my @nada = $q\->extract(\-6, 2); # Returns () \- (3+(\-6)+2) <= 0
365 \& my @some = $q\->extract(\-6, 4); # Returns (foo) \- (3+(\-6)+4) > 0
366 \& # Queue now contains: bar, baz
367 \& my @rest = $q\->extract(\-3, 4); # Returns (bar, baz) \-
368 \& # (2+(\-3)+4) > 0
369 .Ve
370 .SH NOTES
371 .IX Header "NOTES"
372 Queues created by Thread::Queue can be used in both threaded and
373 non\-threaded applications.
374 .SH LIMITATIONS
375 .IX Header "LIMITATIONS"
376 Passing objects on queues may not work if the objects\*(Aq classes do not support
377 sharing. See "BUGS AND LIMITATIONS" in threads::shared for more.
378 .PP
379 Passing array/hash refs that contain objects may not work for Perl prior to
380 5.10.0.
381 .SH "SEE ALSO"
382 .IX Header "SEE ALSO"
383 Thread::Queue on MetaCPAN:
384 <https://metacpan.org/release/Thread\-Queue>
385 .PP
386 Code repository for CPAN distribution:
387 <https://github.com/Dual\-Life/Thread\-Queue>
388 .PP
389 threads, threads::shared
390 .PP
391 Sample code in the \fIexamples\fR directory of this distribution on CPAN.
392 .SH MAINTAINER
393 .IX Header "MAINTAINER"
394 Jerry D. Hedden, <jdhedden\ AT\ cpan\ DOT\ org>
395 .SH LICENSE
396 .IX Header "LICENSE"
397 This program is free software; you can redistribute it and/or modify it under
398 the same terms as Perl itself.