perldoc > Thread::Semaphore

🏷️ NAME

Thread::Semaphore - Thread-safe semaphores

🚀 Quick Reference

Use CaseCommandDescription
Create semaphoremy $s = Thread::Semaphore->new(); or ->new($n)Creates semaphore with count 1 (default) or integer $n
Wait (decrement)$s->down(); or $s->down($n)Blocks until count >= $n, then decrements by $n (default 1)
Signal (increment)$s->up(); or $s->up($n)Increments count by $n (default 1), may unblock waiting threads
Non-blocking wait$s->down_nb(); or $s->down_nb($n)Decrements only if count >= $n; returns true/false
Forceful wait$s->down_force(); or $s->down_force($n)Decrements even if count goes negative; does not block
Timed wait$s->down_timed($timeout, $n)Blocks until count >= $n or timeout (seconds); returns true/false

📌 VERSION

This document describes Thread::Semaphore version 2.13

📋 SYNOPSIS

use Thread::Semaphore;
my $s = Thread::Semaphore->new();
$s->down();   # Also known as the semaphore P operation.
# The guarded section is here
$s->up();     # Also known as the semaphore V operation.

# Decrement the semaphore only if it would immediately succeed.
if ($s->down_nb()) {
    # The guarded section is here
    $s->up();
}

# Forcefully decrement the semaphore even if its count goes below 0.
$s->down_force();

# The default value for semaphore operations is 1
my $s = Thread::Semaphore->new($initial_value);
$s->down($down_value);
$s->up($up_value);
if ($s->down_nb($down_value)) {
    ...
    $s->up($up_value);
}
$s->down_force($down_value);

📖 DESCRIPTION

Semaphores provide a mechanism to regulate access to resources. Unlike locks, semaphores aren't tied to particular scalars, and so may be used to control access to anything you care to use them for.

Semaphores don't limit their values to zero and one, so they can be used to control access to some resource that there may be more than one of (e.g., filehandles). Increment and decrement amounts aren't fixed at one either, so threads can reserve or return multiple resources at once.

🛠️ METHODS

📝 NOTES

Semaphores created by Thread::Semaphore can be used in both threaded and non-threaded applications. This allows you to write modules and packages that potentially make use of semaphores, and that will function in either environment.

🔗 SEE ALSO

👤 MAINTAINER

Jerry D. Hedden, <jdhedden AT cpan DOT org>

⚖️ LICENSE

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

Thread::Semaphore
🏷️ NAME 🚀 Quick Reference 📌 VERSION 📋 SYNOPSIS 📖 DESCRIPTION 🛠️ METHODS 📝 NOTES 🔗 SEE ALSO 👤 MAINTAINER ⚖️ LICENSE

Generated by phpman v4.10.0-7-g98e9fd5 · Markdown · JSON · MCP Author: Che Dong Under GNU General Public License
2026-09-17 15:03 @216.73.216.37
CrawledBy Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)
Valid XHTML 1.0 Transitional!Valid CSS!

^_top_^