THREAD::SEMAPHORE(3perl)

πŸ’– Thread::Semaphore β€” the 3perl manual, but make it bestie-friendly πŸ’–

Ok so hi, welcome to Thread::Semaphore, a 3perl page, and lemme tell you β€” semaphores are lowkey the moment. Sit down, hydrate, we're learning thread-safe resource wrangling. πŸ¦‹


✨ NAME ✨

Thread::Semaphore β€” thread-safe semaphores, periodt. πŸ’…


🌸 VERSION 🌸

This doc describes Thread::Semaphore version 2.13. Yes, that exact one. Not 2.12, not 2.14. 2.13. πŸ’–


πŸŽ€ SYNOPSIS πŸŽ€

Here's the vibe, verbatim, no cap:

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);

See how clean?? Chef's kiss. 🀌


πŸ’œ DESCRIPTION πŸ’œ

So, what is a semaphore. Semaphores provide a mechanism to regulate access to resources. And here's the part that makes them iconic: unlike locks, semaphores aren't tied to particular scalars, so you can use them to control access to literally anything you care to use them for. They're the social butterfly of synchronization primitives. πŸ¦‹

And they don't limit their values to zero and one either, bestie. Semaphores can be used to control access to some resource that there may be more than one of (e.g., filehandles β€” yes, multiple). Increment and decrement amounts aren't fixed at one either, so threads can reserve or return multiple resources at once. Multiple. At. Once. πŸ”₯


πŸ‘‘ METHODS πŸ‘‘

Okay this is the main character section. Here's every method, no skips, no summarizing, we keep it all. πŸ’―

🧚 ->new()

🧚 ->new(NUMBER)

new creates a new semaphore, and initializes its count to the specified number β€” and that number must be an integer, okay, don't try anything cute. If no number is specified, the semaphore's count defaults to 1. So generous of it. 🫢

πŸ“ ->down()

πŸ“ ->down(NUMBER)

The down method decreases the semaphore's count by the specified number (which must be an integer >= 1), or by one if no number is specified.

If the semaphore's count would drop below zero, this method will block until such time as the semaphore's count is greater than or equal to the amount you're downing the semaphore's count by. So it just waits there, patiently, like a good friend holding your spot in line. 😭

This is the semaphore "P operation" β€” and the name derives from the Dutch word "pak", which means "capture". The semaphore operations were named by the late Dijkstra, who was Dutch. Fun fact of the day, you're welcome. ✨

πŸ¦‹ ->down_nb()

πŸ¦‹ ->down_nb(NUMBER)

The down_nb method attempts to decrease the semaphore's count by the specified number (which must be an integer >= 1), or by one if no number is specified.

If the semaphore's count would drop below zero, this method will return false, and the semaphore's count remains unchanged. Otherwise, the semaphore's count is decremented and this method returns true. It's the non-blocking one. It does not wait for anybody. It simply checks and reports back. πŸ’…

🌈 ->down_force()

🌈 ->down_force(NUMBER)

The down_force method decreases the semaphore's count by the specified number (which must be an integer >= 1), or by one if no number is specified. This method does not block, and may cause the semaphore's count to drop below zero. Yes, below zero. It's breaking the rules on purpose and we respect it. πŸ’€

⭐ ->down_timed(TIMEOUT)

⭐ ->down_timed(TIMEOUT, NUMBER)

The down_timed method attempts to decrease the semaphore's count by 1 or by the specified number within the specified timeout period given in seconds (which must be an integer >= 0).

If the semaphore's count would drop below zero, this method will block until either the semaphore's count is greater than or equal to the amount you're downing the semaphore's count by, or until the timeout is reached. Whichever comes first. It's an "I'll wait, but only for so long" situation. πŸ™ˆ

If the timeout is reached, this method will return false, and the semaphore's count remains unchanged. Otherwise, the semaphore's count is decremented and this method returns true.

πŸŽ€ ->up()

πŸŽ€ ->up(NUMBER)

The up method increases the semaphore's count by the number specified (which must be an integer >= 1), or by one if no number is specified.

This will unblock any thread that is blocked trying to down the semaphore if the up raises the semaphore's count above the amount that the down is trying to decrement it by. Example, bestie, listen: if three threads are blocked trying to down a semaphore by one, and another thread ups the semaphore by two, then two of the blocked threads β€” and which two is indeterminate, so don't come for me β€” will become unblocked. πŸ€·β™€οΈ

This is the semaphore "V operation" β€” the name derives from the Dutch word "vrij", which means "release". Dijkstra really said "Dutch vocab lesson AND synchronization", iconic behavior. πŸ‘‘


πŸ’… 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. Versatile! Flexible! Everything! 🌸


πŸ”₯ SEE ALSO πŸ”₯

Go read them, fr fr. πŸ“š


🫢 MAINTAINER 🫢

Jerry D. Hedden, <jdhedden AT cpan DOT org>. Thank you Jerry, we love you. πŸ’–


πŸ’– LICENSE πŸ’–

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


That's the whole page, start to finish, nothing skipped, nothing made up. And before you go β€” this lil doc is from perl v5.44.0, dated 2026-10-17, and it lives under Thread...maphore(3perl). Slay, go use your semaphores responsibly bestie πŸ’…βœ¨πŸ¦‹

πŸ“¦ where this page comes from
  • arch rolling: perl 5.44.0-1 Β· Artistic-1.0-Perl AND GPL-1.0-or-later
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms