{
    "content": [
        {
            "type": "text",
            "text": "# Thread::Semaphore (perldoc)\n\n**Summary:** Thread::Semaphore - Thread-safe semaphores\n\n**Synopsis:** use Thread::Semaphore;\nmy $s = Thread::Semaphore->new();\n$s->down();   # Also known as the semaphore P operation.\n# The guarded section is here\n$s->up();     # Also known as the semaphore V operation.\n# Decrement the semaphore only if it would immediately succeed.\nif ($s->downnb()) {\n# The guarded section is here\n$s->up();\n}\n# Forcefully decrement the semaphore even if its count goes below 0.\n$s->downforce();\n# The default value for semaphore operations is 1\nmy $s = Thread::Semaphore->new($initialvalue);\n$s->down($downvalue);\n$s->up($upvalue);\nif ($s->downnb($downvalue)) {\n...\n$s->up($upvalue);\n}\n$s->downforce($downvalue);\n\n## Section Outline\n\n- **NAME** (2 lines)\n- **VERSION** (2 lines)\n- **SYNOPSIS** (25 lines)\n- **DESCRIPTION** (9 lines)\n- **METHODS** (71 lines)\n- **NOTES** (5 lines)\n- **SEE ALSO** (10 lines)\n- **MAINTAINER** (2 lines)\n- **LICENSE** (3 lines)\n\n## Full Content\n\n### NAME\n\nThread::Semaphore - Thread-safe semaphores\n\n### VERSION\n\nThis document describes Thread::Semaphore version 2.13\n\n### SYNOPSIS\n\nuse Thread::Semaphore;\nmy $s = Thread::Semaphore->new();\n$s->down();   # Also known as the semaphore P operation.\n# The guarded section is here\n$s->up();     # Also known as the semaphore V operation.\n\n# Decrement the semaphore only if it would immediately succeed.\nif ($s->downnb()) {\n# The guarded section is here\n$s->up();\n}\n\n# Forcefully decrement the semaphore even if its count goes below 0.\n$s->downforce();\n\n# The default value for semaphore operations is 1\nmy $s = Thread::Semaphore->new($initialvalue);\n$s->down($downvalue);\n$s->up($upvalue);\nif ($s->downnb($downvalue)) {\n...\n$s->up($upvalue);\n}\n$s->downforce($downvalue);\n\n### DESCRIPTION\n\nSemaphores provide a mechanism to regulate access to resources. Unlike\nlocks, semaphores aren't tied to particular scalars, and so may be used\nto control access to anything you care to use them for.\n\nSemaphores don't limit their values to zero and one, so they can be used\nto control access to some resource that there may be more than one of\n(e.g., filehandles). Increment and decrement amounts aren't fixed at one\neither, so threads can reserve or return multiple resources at once.\n\n### METHODS\n\n->new()\n->new(NUMBER)\n\"new\" creates a new semaphore, and initializes its count to the\nspecified number (which must be an integer). If no number is\nspecified, the semaphore's count defaults to 1.\n\n->down()\n->down(NUMBER)\nThe \"down\" method decreases the semaphore's count by the\nspecified number (which must be an integer >= 1), or by one if\nno number is specified.\n\nIf the semaphore's count would drop below zero, this method will\nblock until such time as the semaphore's count is greater than\nor equal to the amount you're \"down\"ing the semaphore's count\nby.\n\nThis is the semaphore \"P operation\" (the name derives from the\nDutch word \"pak\", which means \"capture\" -- the semaphore\noperations were named by the late Dijkstra, who was Dutch).\n\n->downnb()\n->downnb(NUMBER)\nThe \"downnb\" method attempts to decrease the semaphore's count\nby the specified number (which must be an integer >= 1), or by\none if no number is specified.\n\nIf the semaphore's count would drop below zero, this method will\nreturn *false*, and the semaphore's count remains unchanged.\nOtherwise, the semaphore's count is decremented and this method\nreturns *true*.\n\n->downforce()\n->downforce(NUMBER)\nThe \"downforce\" method decreases the semaphore's count by the\nspecified number (which must be an integer >= 1), or by one if\nno number is specified. This method does not block, and may\ncause the semaphore's count to drop below zero.\n\n->downtimed(TIMEOUT)\n->downtimed(TIMEOUT, NUMBER)\nThe \"downtimed\" method attempts to decrease the semaphore's\ncount by 1 or by the specified number within the specified\ntimeout period given in seconds (which must be an integer >= 0).\n\nIf the semaphore's count would drop below zero, this method will\nblock until either the semaphore's count is greater than or\nequal to the amount you're \"down\"ing the semaphore's count by,\nor until the timeout is reached.\n\nIf the timeout is reached, this method will return *false*, and\nthe semaphore's count remains unchanged. Otherwise, the\nsemaphore's count is decremented and this method returns *true*.\n\n->up()\n->up(NUMBER)\nThe \"up\" method increases the semaphore's count by the number\nspecified (which must be an integer >= 1), or by one if no\nnumber is specified.\n\nThis will unblock any thread that is blocked trying to \"down\"\nthe semaphore if the \"up\" raises the semaphore's count above the\namount that the \"down\" is trying to decrement it by. For\nexample, if three threads are blocked trying to \"down\" a\nsemaphore by one, and another thread \"up\"s the semaphore by two,\nthen two of the blocked threads (which two is indeterminate)\nwill become unblocked.\n\nThis is the semaphore \"V operation\" (the name derives from the\nDutch word \"vrij\", which means \"release\").\n\n### NOTES\n\nSemaphores created by Thread::Semaphore can be used in both threaded and\nnon-threaded applications. This allows you to write modules and packages\nthat potentially make use of semaphores, and that will function in\neither environment.\n\n### SEE ALSO\n\nThread::Semaphore on MetaCPAN:\n<https://metacpan.org/release/Thread-Semaphore>\n\nCode repository for CPAN distribution:\n<https://github.com/Dual-Life/Thread-Semaphore>\n\nthreads, threads::shared\n\nSample code in the *examples* directory of this distribution on CPAN.\n\n### MAINTAINER\n\nJerry D. Hedden, <jdhedden AT cpan DOT org>\n\n### LICENSE\n\nThis program is free software; you can redistribute it and/or modify it\nunder the same terms as Perl itself.\n\n"
        }
    ],
    "structuredContent": {
        "command": "Thread::Semaphore",
        "section": "",
        "mode": "perldoc",
        "summary": "Thread::Semaphore - Thread-safe semaphores",
        "synopsis": "use Thread::Semaphore;\nmy $s = Thread::Semaphore->new();\n$s->down();   # Also known as the semaphore P operation.\n# The guarded section is here\n$s->up();     # Also known as the semaphore V operation.\n# Decrement the semaphore only if it would immediately succeed.\nif ($s->downnb()) {\n# The guarded section is here\n$s->up();\n}\n# Forcefully decrement the semaphore even if its count goes below 0.\n$s->downforce();\n# The default value for semaphore operations is 1\nmy $s = Thread::Semaphore->new($initialvalue);\n$s->down($downvalue);\n$s->up($upvalue);\nif ($s->downnb($downvalue)) {\n...\n$s->up($upvalue);\n}\n$s->downforce($downvalue);",
        "flags": [],
        "examples": [],
        "see_also": [],
        "section_outline": [
            {
                "name": "NAME",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "VERSION",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "SYNOPSIS",
                "lines": 25,
                "subsections": []
            },
            {
                "name": "DESCRIPTION",
                "lines": 9,
                "subsections": []
            },
            {
                "name": "METHODS",
                "lines": 71,
                "subsections": []
            },
            {
                "name": "NOTES",
                "lines": 5,
                "subsections": []
            },
            {
                "name": "SEE ALSO",
                "lines": 10,
                "subsections": []
            },
            {
                "name": "MAINTAINER",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "LICENSE",
                "lines": 3,
                "subsections": []
            }
        ]
    }
}