{
    "content": [
        {
            "type": "text",
            "text": "# Thread::Queue (perldoc)\n\n## NAME\n\nThread::Queue - Thread-safe queues\n\n## SYNOPSIS\n\nuse strict;\nuse warnings;\nuse threads;\nuse Thread::Queue;\nmy $q = Thread::Queue->new();    # A new empty queue\n# Worker thread\nmy $thr = threads->create(\nsub {\n# Thread will loop until no more work\nwhile (defined(my $item = $q->dequeue())) {\n# Do work on $item\n...\n}\n}\n);\n# Send work to the thread\n$q->enqueue($item1, ...);\n# Signal that there is no more work to be sent\n$q->end();\n# Join up with the thread when it finishes\n$thr->join();\n...\n# Count of items in the queue\nmy $left = $q->pending();\n# Non-blocking dequeue\nif (defined(my $item = $q->dequeuenb())) {\n# Work on $item\n}\n# Blocking dequeue with 5-second timeout\nif (defined(my $item = $q->dequeuetimed(5))) {\n# Work on $item\n}\n# Set a size for a queue\n$q->limit = 5;\n# Get the second item in the queue without dequeuing anything\nmy $item = $q->peek(1);\n# Insert two items into the queue just behind the head\n$q->insert(1, $item1, $item2);\n# Extract the last two items on the queue\nmy ($item1, $item2) = $q->extract(-2, 2);\n\n## DESCRIPTION\n\nThis module provides thread-safe FIFO queues that can be accessed safely by any number of\nthreads.\n\n## Sections\n\n- **NAME**\n- **VERSION**\n- **SYNOPSIS**\n- **DESCRIPTION**\n- **QUEUE CREATION**\n- **BASIC METHODS**\n- **ADVANCED METHODS**\n- **NOTES**\n- **LIMITATIONS**\n- **SEE ALSO**\n- **MAINTAINER**\n- **LICENSE**\n\nUse structuredContent.sections for detailed options, examples, and full documentation.\n"
        }
    ],
    "structuredContent": {
        "command": "Thread::Queue",
        "section": "",
        "mode": "perldoc",
        "summary": "Thread::Queue - Thread-safe queues",
        "synopsis": "use strict;\nuse warnings;\nuse threads;\nuse Thread::Queue;\nmy $q = Thread::Queue->new();    # A new empty queue\n# Worker thread\nmy $thr = threads->create(\nsub {\n# Thread will loop until no more work\nwhile (defined(my $item = $q->dequeue())) {\n# Do work on $item\n...\n}\n}\n);\n# Send work to the thread\n$q->enqueue($item1, ...);\n# Signal that there is no more work to be sent\n$q->end();\n# Join up with the thread when it finishes\n$thr->join();\n...\n# Count of items in the queue\nmy $left = $q->pending();\n# Non-blocking dequeue\nif (defined(my $item = $q->dequeuenb())) {\n# Work on $item\n}\n# Blocking dequeue with 5-second timeout\nif (defined(my $item = $q->dequeuetimed(5))) {\n# Work on $item\n}\n# Set a size for a queue\n$q->limit = 5;\n# Get the second item in the queue without dequeuing anything\nmy $item = $q->peek(1);\n# Insert two items into the queue just behind the head\n$q->insert(1, $item1, $item2);\n# Extract the last two items on the queue\nmy ($item1, $item2) = $q->extract(-2, 2);",
        "tldr_summary": null,
        "tldr_examples": [],
        "tldr_source": null,
        "flags": [],
        "examples": [],
        "see_also": [],
        "section_outline": [
            {
                "name": "NAME",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "VERSION",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "SYNOPSIS",
                "lines": 53,
                "subsections": []
            },
            {
                "name": "DESCRIPTION",
                "lines": 38,
                "subsections": []
            },
            {
                "name": "QUEUE CREATION",
                "lines": 6,
                "subsections": []
            },
            {
                "name": "BASIC METHODS",
                "lines": 65,
                "subsections": []
            },
            {
                "name": "ADVANCED METHODS",
                "lines": 82,
                "subsections": []
            },
            {
                "name": "NOTES",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "LIMITATIONS",
                "lines": 5,
                "subsections": []
            },
            {
                "name": "SEE ALSO",
                "lines": 8,
                "subsections": []
            },
            {
                "name": "MAINTAINER",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "LICENSE",
                "lines": 3,
                "subsections": []
            }
        ],
        "sections": {
            "NAME": {
                "content": "Thread::Queue - Thread-safe queues\n",
                "subsections": []
            },
            "VERSION": {
                "content": "This document describes Thread::Queue version 3.14\n",
                "subsections": []
            },
            "SYNOPSIS": {
                "content": "use strict;\nuse warnings;\n\nuse threads;\nuse Thread::Queue;\n\nmy $q = Thread::Queue->new();    # A new empty queue\n\n# Worker thread\nmy $thr = threads->create(\nsub {\n# Thread will loop until no more work\nwhile (defined(my $item = $q->dequeue())) {\n# Do work on $item\n...\n}\n}\n);\n\n# Send work to the thread\n$q->enqueue($item1, ...);\n# Signal that there is no more work to be sent\n$q->end();\n# Join up with the thread when it finishes\n$thr->join();\n\n...\n\n# Count of items in the queue\nmy $left = $q->pending();\n\n# Non-blocking dequeue\nif (defined(my $item = $q->dequeuenb())) {\n# Work on $item\n}\n\n# Blocking dequeue with 5-second timeout\nif (defined(my $item = $q->dequeuetimed(5))) {\n# Work on $item\n}\n\n# Set a size for a queue\n$q->limit = 5;\n\n# Get the second item in the queue without dequeuing anything\nmy $item = $q->peek(1);\n\n# Insert two items into the queue just behind the head\n$q->insert(1, $item1, $item2);\n\n# Extract the last two items on the queue\nmy ($item1, $item2) = $q->extract(-2, 2);\n",
                "subsections": []
            },
            "DESCRIPTION": {
                "content": "This module provides thread-safe FIFO queues that can be accessed safely by any number of\nthreads.\n\nAny data types supported by threads::shared can be passed via queues:\n\nOrdinary scalars\nArray refs\nHash refs\nScalar refs\nObjects based on the above\n\nOrdinary scalars are added to queues as they are.\n\nIf not already thread-shared, the other complex data types will be cloned (recursively, if\nneeded, and including any \"bless\"ings and read-only settings) into thread-shared structures\nbefore being placed onto a queue.\n\nFor example, the following would cause Thread::Queue to create a empty, shared array reference\nvia \"&shared([])\", copy the elements 'foo', 'bar' and 'baz' from @ary into it, and then place\nthat shared reference onto the queue:\n\nmy @ary = qw/foo bar baz/;\n$q->enqueue(\\@ary);\n\nHowever, for the following, the items are already shared, so their references are added directly\nto the queue, and no cloning takes place:\n\nmy @ary :shared = qw/foo bar baz/;\n$q->enqueue(\\@ary);\n\nmy $obj = &shared({});\n$$obj{'foo'} = 'bar';\n$$obj{'qux'} = 99;\nbless($obj, 'My::Class');\n$q->enqueue($obj);\n\nSee \"LIMITATIONS\" for caveats related to passing objects via queues.\n",
                "subsections": []
            },
            "QUEUE CREATION": {
                "content": "->new()\nCreates a new empty queue.\n\n->new(LIST)\nCreates a new queue pre-populated with the provided list of items.\n",
                "subsections": []
            },
            "BASIC METHODS": {
                "content": "The following methods deal with queues on a FIFO basis.\n\n->enqueue(LIST)\nAdds a list of items onto the end of the queue.\n\n->dequeue()\n->dequeue(COUNT)\nRemoves the requested number of items (default is 1) from the head of the queue, and returns\nthem. If the queue contains fewer than the requested number of items, then the thread will\nbe blocked until the requisite number of items are available (i.e., until other threads\n\"enqueue\" more items).\n\n->dequeuenb()\n->dequeuenb(COUNT)\nRemoves the requested number of items (default is 1) from the head of the queue, and returns\nthem. If the queue contains fewer than the requested number of items, then it immediately\n(i.e., non-blocking) returns whatever items there are on the queue. If the queue is empty,\nthen \"undef\" is returned.\n\n->dequeuetimed(TIMEOUT)\n->dequeuetimed(TIMEOUT, COUNT)\nRemoves the requested number of items (default is 1) from the head of the queue, and returns\nthem. If the queue contains fewer than the requested number of items, then the thread will\nbe blocked until the requisite number of items are available, or until the timeout is\nreached. If the timeout is reached, it returns whatever items there are on the queue, or\n\"undef\" if the queue is empty.\n\nThe timeout may be a number of seconds relative to the current time (e.g., 5 seconds from\nwhen the call is made), or may be an absolute timeout in *epoch* seconds the same as would\nbe used with condtimedwait(). Fractional seconds (e.g., 2.5 seconds) are also supported (to\nthe extent of the underlying implementation).\n\nIf \"TIMEOUT\" is missing, \"undef\", or less than or equal to 0, then this call behaves the\nsame as \"dequeuenb\".\n\n->pending()\nReturns the number of items still in the queue. Returns \"undef\" if the queue has been ended\n(see below), and there are no more items in the queue.\n\n->limit\nSets the size of the queue. If set, calls to \"enqueue()\" will block until the number of\npending items in the queue drops below the \"limit\". The \"limit\" does not prevent enqueuing\nitems beyond that count:\n\nmy $q = Thread::Queue->new(1, 2);\n$q->limit = 4;\n$q->enqueue(3, 4, 5);   # Does not block\n$q->enqueue(6);         # Blocks until at least 2 items are\n# dequeued\nmy $size = $q->limit;   # Returns the current limit (may return\n# 'undef')\n$q->limit = 0;          # Queue size is now unlimited\n\nCalling any of the dequeue methods with \"COUNT\" greater than a queue's \"limit\" will generate\nan error.\n\n->end()\nDeclares that no more items will be added to the queue.\n\nAll threads blocking on \"dequeue()\" calls will be unblocked with any remaining items in the\nqueue and/or \"undef\" being returned. Any subsequent calls to \"dequeue()\" will behave like\n\"dequeuenb()\".\n\nOnce ended, no more items may be placed in the queue.\n",
                "subsections": []
            },
            "ADVANCED METHODS": {
                "content": "The following methods can be used to manipulate items anywhere in a queue.\n\nTo prevent the contents of a queue from being modified by another thread while it is being\nexamined and/or changed, lock the queue inside a local block:\n\n{\nlock($q);   # Keep other threads from changing the queue's contents\nmy $item = $q->peek();\nif ($item ...) {\n...\n}\n}\n# Queue is now unlocked\n\n->peek()\n->peek(INDEX)\nReturns an item from the queue without dequeuing anything. Defaults to the head of queue (at\nindex position 0) if no index is specified. Negative index values are supported as with\narrays (i.e., -1 is the end of the queue, -2 is next to last, and so on).\n\nIf no items exists at the specified index (i.e., the queue is empty, or the index is beyond\nthe number of items on the queue), then \"undef\" is returned.\n\nRemember, the returned item is not removed from the queue, so manipulating a \"peek\"ed at\nreference affects the item on the queue.\n\n->insert(INDEX, LIST)\nAdds the list of items to the queue at the specified index position (0 is the head of the\nlist). Any existing items at and beyond that position are pushed back past the newly added\nitems:\n\n$q->enqueue(1, 2, 3, 4);\n$q->insert(1, qw/foo bar/);\n# Queue now contains:  1, foo, bar, 2, 3, 4\n\nSpecifying an index position greater than the number of items in the queue just adds the\nlist to the end.\n\nNegative index positions are supported:\n\n$q->enqueue(1, 2, 3, 4);\n$q->insert(-2, qw/foo bar/);\n# Queue now contains:  1, 2, foo, bar, 3, 4\n\nSpecifying a negative index position greater than the number of items in the queue adds the\nlist to the head of the queue.\n\n->extract()\n->extract(INDEX)\n->extract(INDEX, COUNT)\nRemoves and returns the specified number of items (defaults to 1) from the specified index\nposition in the queue (0 is the head of the queue). When called with no arguments, \"extract\"\noperates the same as \"dequeuenb\".\n\nThis method is non-blocking, and will return only as many items as are available to fulfill\nthe request:\n\n$q->enqueue(1, 2, 3, 4);\nmy $item  = $q->extract(2)     # Returns 3\n# Queue now contains:  1, 2, 4\nmy @items = $q->extract(1, 3)  # Returns (2, 4)\n# Queue now contains:  1\n\nSpecifying an index position greater than the number of items in the queue results in\n\"undef\" or an empty list being returned.\n\n$q->enqueue('foo');\nmy $nada = $q->extract(3)      # Returns undef\nmy @nada = $q->extract(1, 3)   # Returns ()\n\nNegative index positions are supported. Specifying a negative index position greater than\nthe number of items in the queue may return items from the head of the queue (similar to\n\"dequeuenb\") if the count overlaps the head of the queue from the specified position (i.e.\nif queue size + index + count is greater than zero):\n\n$q->enqueue(qw/foo bar baz/);\nmy @nada = $q->extract(-6, 2);  # Returns ()      - (3+(-6)+2) <= 0\nmy @some = $q->extract(-6, 4);  # Returns (foo)   - (3+(-6)+4) > 0\n# Queue now contains:  bar, baz\nmy @rest = $q->extract(-3, 4);  # Returns (bar, baz) -\n#                   (2+(-3)+4) > 0\n",
                "subsections": []
            },
            "NOTES": {
                "content": "Queues created by Thread::Queue can be used in both threaded and non-threaded applications.\n",
                "subsections": []
            },
            "LIMITATIONS": {
                "content": "Passing objects on queues may not work if the objects' classes do not support sharing. See \"BUGS\nAND LIMITATIONS\" in threads::shared for more.\n\nPassing array/hash refs that contain objects may not work for Perl prior to 5.10.0.\n",
                "subsections": []
            },
            "SEE ALSO": {
                "content": "Thread::Queue on MetaCPAN: <https://metacpan.org/release/Thread-Queue>\n\nCode repository for CPAN distribution: <https://github.com/Dual-Life/Thread-Queue>\n\nthreads, threads::shared\n\nSample code in the *examples* directory of this distribution on CPAN.\n",
                "subsections": []
            },
            "MAINTAINER": {
                "content": "Jerry D. Hedden, <jdhedden AT cpan DOT org>\n",
                "subsections": []
            },
            "LICENSE": {
                "content": "This program is free software; you can redistribute it and/or modify it under the same terms as\nPerl itself.\n",
                "subsections": []
            }
        }
    }
}