1Thread::Semaphore(3pm) Perl Programmers Reference Guide Thread::Semaphore(3pm)
2
3
4

NAME

6       Thread::Semaphore - Thread-safe semaphores
7

VERSION

9       This document describes Thread::Semaphore version 2.12
10

SYNOPSIS

12           use Thread::Semaphore;
13           my $s = Thread::Semaphore->new();
14           $s->down();   # Also known as the semaphore P operation.
15           # The guarded section is here
16           $s->up();     # Also known as the semaphore V operation.
17
18           # Decrement the semaphore only if it would immediately succeed.
19           if ($s->down_nb()) {
20               # The guarded section is here
21               $s->up();
22           }
23
24           # Forcefully decrement the semaphore even if its count goes below 0.
25           $s->down_force();
26
27           # The default value for semaphore operations is 1
28           my $s = Thread::Semaphore->new($initial_value);
29           $s->down($down_value);
30           $s->up($up_value);
31           if ($s->down_nb($down_value)) {
32               ...
33               $s->up($up_value);
34           }
35           $s->down_force($down_value);
36

DESCRIPTION

38       Semaphores provide a mechanism to regulate access to resources.  Unlike
39       locks, semaphores aren't tied to particular scalars, and so may be used
40       to control access to anything you care to use them for.
41
42       Semaphores don't limit their values to zero and one, so they can be
43       used to control access to some resource that there may be more than one
44       of (e.g., filehandles).  Increment and decrement amounts aren't fixed
45       at one either, so threads can reserve or return multiple resources at
46       once.
47

METHODS

49       ->new()
50       ->new(NUMBER)
51               "new" creates a new semaphore, and initializes its count to the
52               specified number (which must be an integer).  If no number is
53               specified, the semaphore's count defaults to 1.
54
55       ->down()
56       ->down(NUMBER)
57               The "down" method decreases the semaphore's count by the
58               specified number (which must be an integer >= 1), or by one if
59               no number is specified.
60
61               If the semaphore's count would drop below zero, this method
62               will block until such time as the semaphore's count is greater
63               than or equal to the amount you're "down"ing the semaphore's
64               count by.
65
66               This is the semaphore "P operation" (the name derives from the
67               Dutch word "pak", which means "capture" -- the semaphore
68               operations were named by the late Dijkstra, who was Dutch).
69
70       ->down_nb()
71       ->down_nb(NUMBER)
72               The "down_nb" method attempts to decrease the semaphore's count
73               by the specified number (which must be an integer >= 1), or by
74               one if no number is specified.
75
76               If the semaphore's count would drop below zero, this method
77               will return false, and the semaphore's count remains unchanged.
78               Otherwise, the semaphore's count is decremented and this method
79               returns true.
80
81       ->down_force()
82       ->down_force(NUMBER)
83               The "down_force" method decreases the semaphore's count by the
84               specified number (which must be an integer >= 1), or by one if
85               no number is specified.  This method does not block, and may
86               cause the semaphore's count to drop below zero.
87
88       ->up()
89       ->up(NUMBER)
90               The "up" method increases the semaphore's count by the number
91               specified (which must be an integer >= 1), or by one if no
92               number is specified.
93
94               This will unblock any thread that is blocked trying to "down"
95               the semaphore if the "up" raises the semaphore's count above
96               the amount that the "down" is trying to decrement it by.  For
97               example, if three threads are blocked trying to "down" a
98               semaphore by one, and another thread "up"s the semaphore by
99               two, then two of the blocked threads (which two is
100               indeterminate) will become unblocked.
101
102               This is the semaphore "V operation" (the name derives from the
103               Dutch word "vrij", which means "release").
104

NOTES

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

SEE ALSO

112       Thread::Semaphore Discussion Forum on CPAN:
113       http://www.cpanforum.com/dist/Thread-Semaphore
114       <http://www.cpanforum.com/dist/Thread-Semaphore>
115
116       threads, threads::shared
117

MAINTAINER

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

LICENSE

122       This program is free software; you can redistribute it and/or modify it
123       under the same terms as Perl itself.
124
125
126
127perl v5.16.3                      2013-03-04            Thread::Semaphore(3pm)
Impressum