1Thread::Semaphore(3pm) Perl Programmers Reference Guide Thread::Semaphore(3pm)
2
3
4
6 Thread::Semaphore - thread-safe semaphores
7
9 use Thread::Semaphore;
10 my $s = new Thread::Semaphore;
11 $s->down; # Also known as the semaphore P operation.
12 # The guarded section is here
13 $s->up; # Also known as the semaphore V operation.
14
15 # The default semaphore value is 1.
16 my $s = new Thread::Semaphore($initial_value);
17 $s->down($down_value);
18 $s->up($up_value);
19
21 Semaphores provide a mechanism to regulate access to resources. Sema‐
22 phores, unlike locks, aren't tied to particular scalars, and so may be
23 used to control access to anything you care to use them for.
24
25 Semaphores don't limit their values to zero or one, so they can be used
26 to control access to some resource that there may be more than one of.
27 (For example, filehandles.) Increment and decrement amounts aren't
28 fixed at one either, so threads can reserve or return multiple
29 resources at once.
30
32 new
33 new NUMBER
34 "new" creates a new semaphore, and initializes its count to the
35 passed number. If no number is passed, the semaphore's count is
36 set to one.
37
38 down
39 down NUMBER
40 The "down" method decreases the semaphore's count by the speci‐
41 fied number, or by one if no number has been specified. If the
42 semaphore's count would drop below zero, this method will block
43 until such time that the semaphore's count is equal to or
44 larger than the amount you're "down"ing the semaphore's count
45 by.
46
47 This is the semaphore "P operation" (the name derives from the
48 Dutch word "pak", which means "capture" -- the semaphore opera‐
49 tions were named by the late Dijkstra, who was Dutch).
50
51 up
52 up NUMBER
53 The "up" method increases the semaphore's count by the number
54 specified, or by one if no number has been specified. This will
55 unblock any thread blocked trying to "down" the semaphore if
56 the "up" raises the semaphore count above the amount that the
57 "down"s are trying to decrement it by.
58
59 This is the semaphore "V operation" (the name derives from the
60 Dutch word "vrij", which means "release").
61
62
63
64perl v5.8.8 2001-09-21 Thread::Semaphore(3pm)