1Thread::Semaphore(3pm) Perl Programmers Reference Guide Thread::Semaphore(3pm)
2
3
4
6 Thread::Semaphore - Thread-safe semaphores
7
9 This document describes Thread::Semaphore version 2.13
10
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
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
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 ->down_timed(TIMEOUT)
89 ->down_timed(TIMEOUT, NUMBER)
90 The "down_timed" method attempts to decrease the semaphore's
91 count by 1 or by the specified number within the specified
92 timeout period given in seconds (which must be an integer >=
93 0).
94
95 If the semaphore's count would drop below zero, this method
96 will block until either the semaphore's count is greater than
97 or equal to the amount you're "down"ing the semaphore's count
98 by, or until the timeout is reached.
99
100 If the timeout is reached, this method will return false, and
101 the semaphore's count remains unchanged. Otherwise, the
102 semaphore's count is decremented and this method returns true.
103
104 ->up()
105 ->up(NUMBER)
106 The "up" method increases the semaphore's count by the number
107 specified (which must be an integer >= 1), or by one if no
108 number is specified.
109
110 This will unblock any thread that is blocked trying to "down"
111 the semaphore if the "up" raises the semaphore's count above
112 the amount that the "down" is trying to decrement it by. For
113 example, if three threads are blocked trying to "down" a
114 semaphore by one, and another thread "up"s the semaphore by
115 two, then two of the blocked threads (which two is
116 indeterminate) will become unblocked.
117
118 This is the semaphore "V operation" (the name derives from the
119 Dutch word "vrij", which means "release").
120
122 Semaphores created by Thread::Semaphore can be used in both threaded
123 and non-threaded applications. This allows you to write modules and
124 packages that potentially make use of semaphores, and that will
125 function in either environment.
126
128 Thread::Semaphore on MetaCPAN:
129 <https://metacpan.org/release/Thread-Semaphore>
130
131 Code repository for CPAN distribution:
132 <https://github.com/Dual-Life/Thread-Semaphore>
133
134 threads, threads::shared
135
136 Sample code in the examples directory of this distribution on CPAN.
137
139 Jerry D. Hedden, <jdhedden AT cpan DOT org>
140
142 This program is free software; you can redistribute it and/or modify it
143 under the same terms as Perl itself.
144
145
146
147perl v5.32.1 2021-03-31 Thread::Semaphore(3pm)