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